DockerFile简明教程

需求

由于在测试环境中使用了docker官网的centos 镜像,但是该镜像里面默认没有安装ssh服务,在做测试时又需要开启ssh。所以上网也查了查资料。下面详细的纪录下。在centos 容器内安装ssh后,转成新的镜像用于后期测试使用。

镜像定制

第一种方式(手动修改容器镜像)

1.先下载centos镜像

[root@docker ~]# docker pull centos:7

2.启动容器并进行配置

启动容器,

[root@docker ~]# docker run -it -d --name test-centos1 centos:7
d72250ecaa5e3e36226a1edd749f494d9f00eddc4143c81ac3565aa4e551791a

命令注释:-it : 进行交互式操作

-d : 等同于 -d=true,容器将会在后台运行,不然执行一次命令后,退出后,便是exit状态了。

–name : 容器启动后的名字,默认不指定,将会随机产生一个名字。或者使用 -name=“containers_name”

centos:使用的镜像名称

进入容器,安装ssh server,以及配置开机启动

[root@docker ~]# docker exec -it test-centos1 /bin/bash
[root@d72250ecaa5e /]# ifconfig
bash: ifconfig: command not found

注:命令最后参数 /bin/bash: 指进入容器时执行的命令(command)
我们检查了下容器,暂时安装以下必用的软件吧 net-tools,openssh-server

[root@d72250ecaa5e /]# yum install openssh-server net-tools -y

创建ssh 所需的目录,并在根目录创建sshd 启动脚本,输入/usr/sbin/sshd -D

[root@d72250ecaa5e /]# mkdir -pv /var/run/sshd
mkdir: created directory '/var/run/sshd'
[root@d72250ecaa5e /]# cat /auto_sshd.sh 
#!/bin/bash
/usr/sbin/sshd -D
[root@d72250ecaa5e /]# chmod +x /auto_sshd.sh

修改容器内root 的账户密码

[root@d72250ecaa5e /]# echo "root:iloveworld" | chpasswd 

生成ssh 主机dsa 密钥(不然ssh 该容器时,会出现错误。

[root@d72250ecaa5e /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
[root@d72250ecaa5e /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

我们加一个history记录的时间功能吧,这样方便后期查看

echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/profile

OK,配置基本完毕咯。清理命令历史纪录,之后退出容器。现在可以生成一个新的docker 镜像了。
3.配置完成后,进行打包成新的镜像

[root@docker ~]# docker commit test-centos1 centos_sshd:7.0
sha256:6e3330b30dfff5f029f102874e54cfffffbc37dcf2a4eb7304c817148fbc944d
[root@docker ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
centos_sshd                  7.0                 6e3330b30dff        8 seconds ago       310.1 MB
docker.io/ubuntu             latest              e4415b714b62        12 days ago         128.1 MB

命令注释:commit: 提交一个具有新配置的容器成为镜像,后面跟容器的name 或者容器Id ,最后是生成新镜像的名字

更新:这条命令更方便以后启动,如下:

[root@docker ~]# docker commit --change='CMD ["/auto_sshd.sh"]' -c "EXPOSE 22" test-centos1 centos_sshd:7.0
sha256:7bb4efd82c4ff1f241cbc57ee45aab1b05d214b1e9fcd51196696c67d480e70b

命令注释: --change : 将后期使用此镜像运行容器时的命令参数、开放的容器端口提前设置好。

4.验证

查看镜像,并启动新的容器

[root@docker ~]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
centos_sshd                  7.0                 7bb4efd82c4f        4 minutes ago       310.1 MB
docker.io/ubuntu             latest              e4415b714b62        12 days ago         128.1 MB[root@docker ~]# docker run -d -it --name centos_7.0-1 centos_sshd:7.0
ec17e553d5c4c60865afeb99df8dfd1f4e7d4ba6e1b0d5516f9127f09d1d6356
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS           PORTS          NAMES
ec17e553d5c4        centos_sshd:7.0           "/auto_sshd.sh"          6 seconds ago       Up 5 seconds     22/tcp         centos_7.0-1

进行ssh测试,先查看一下该容器的ip,之后ssh。ok,密码使用前面设置的iloveworld

[root@docker ~]# docker exec centos_7.0-1 hostname -i
172.17.0.4[root@docker ~]# ssh root@172.17.0.4
The authenticity of host '172.17.0.4 (172.17.0.4)' can't be established.
RSA key fingerprint is 87:88:07:12:ac:0a:90:28:10:e1:9e:eb:1f:d6:c9:9d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.4' (RSA) to the list of known hosts.
root@172.17.0.4's password: 
Last login: Tue Nov 29 16:00:49 2016 from gateway[root@ec17e553d5c4 ~]# w   16:34:17 up 63 days,  7:49,  1 user,  load average: 0.00, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    gateway          16:34    1.00s  0.00s  0.00s w
[root@ec17e553d5c4 ~]# ping gateway
PING gateway (172.17.0.1) 56(84) bytes of data.
64 bytes from gateway (172.17.0.1): icmp_seq=1 ttl=64 time=0.048 ms

第二种方式(推荐:利用Dockerfile文件)

Dockerfile包含创建镜像所需要的全部指令。基于在Dockerfile中的指令,我们可以使用Docker build命令来创建镜像。通过减少镜像和容器的创建过程来简化部署。

1.创建Dockerfile文件

新建一个目录,在里面新建一个dockerfile文件(新建一个的目录,主要是为了和以防和其它dockerfile混乱 )

[root@docker ~]# mkdir centos7-dockerfile[root@docker centos7-dockerfile]# cat Dockerfile 
# The dockerfile has Change add sshd services on Centos7.0
#centos7:latest image
FROM centos:7MAINTAINER Yifeng,http://www.cnblogs.com/hanyifeng#Install sshd net-tools
RUN yum install openssh-server net-tools -y
RUN mkdir /var/run/sshd#Set password for root
RUN echo 'root:iloveworld' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config#Set history record
ENV HISTTIMEFORMAT "%F %T  "#Fix sshd service:Read from socket failed: Connection reset by peer?
RUN ssh-keygen -A#Change timezone CST
RUN \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime#Open 22 port
EXPOSE 22#Auto running sshd service
CMD ["/usr/sbin/sshd","-D"]

上述文件内容就是一个dockerfile 常见的命令组合。开头带#号的为注释

文件解释:

FROM: 必不可少的命令,从某个镜像作为基。如 FROM <image_name> ,或者 FROM <image_name>:. 如果不加tag,默认为latest。先从本地镜像仓库去搜索基镜像,如过本地没有,在去网上docker registry去寻找。

MAINTAINER:标明该Dockerfile作者及联系方式,可忽略不写

RUN:建立新的镜像时,可以执行在系统里的命令,如安装特定的软件以及设置环境变量。

ENV:设置系统环境变量(注意:写在/etc/profile里的命令在dockerfile这里会不生效,所以为改成ENV的方式)

EXPOSE:开放容器内的端口,但不和宿主机进行映射。方便在宿主机上进行开发测试。(如需映射到宿主机端口,可在运行容器时使用 -p host_port:container_port)

CMD:设置执行的命令,经常用于容器启动时指定的某个操作。如执行自定义脚本服务,或者是执行系统命令。CMD 只能存在一条,如在Dockerfile中有多条CMD的话,只有最后一条CMD生效!

2.执行build 创建镜像

使用docker build命令来创建镜像

[root@docker centos7-dockerfile]# docker build -t centos_sshd_1 .

-t 选项来docker build新的镜像以便于标记构建的镜像,. 表示当前目录,也可以指定dockerfile 文件所在目录。

下面缩略的内容是构建镜像时的输出,可以看下。

[root@docker centos7-dockerfile]# docker build -t centos_sshd_1 .
Sending build context to Docker daemon 4.096 kB
Step 1 : FROM centos:latest---> 0584b3d2cf6d
Step 2 : MAINTAINER Yifeng,http://www.cnblogs.com/hanyifeng---> Running in da643b55dc77---> 1087074d44e4
Removing intermediate container da643b55dc77
Step 3 : RUN yum install openssh-server net-tools -y---> Running in 5626d8f0f892
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors* base: mirrors.btte.net* extras: mirrors.tuna.tsinghua.edu.cn* updates: mirrors.btte.net
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed
---> Package openssh-server.x86_64 0:6.6.1p1-25.el7_2 will be installed
--> Processing Dependency: openssh = 6.6.1p1-25.el7_2 for package: openssh-server-6.6.1p1-25.el7_2.x86_64
--> Processing Dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-server-6.6.1p1-25.el7_2.x86_64
--> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-6.6.1p1-25.el7_2.x86_64
--> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-server-6.6.1p1-25.el7_2.x86_64
--> Running transaction check
---> Package fipscheck-lib.x86_64 0:1.4.1-5.el7 will be installed
--> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-5.el7.x86_64
---> Package openssh.x86_64 0:6.6.1p1-25.el7_2 will be installed
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
--> Running transaction check
---> Package fipscheck.x86_64 0:1.4.1-5.el7 will be installed
--> Finished Dependency ResolutionDependencies Resolved================================================================================Package              Arch      Version                        Repository  Size
================================================================================
Installing:net-tools            x86_64    2.0-0.17.20131004git.el7       base       304 kopenssh-server       x86_64    6.6.1p1-25.el7_2               updates    436 k
Installing for dependencies:fipscheck            x86_64    1.4.1-5.el7                    base        21 kfipscheck-lib        x86_64    1.4.1-5.el7                    base        11 kopenssh              x86_64    6.6.1p1-25.el7_2               updates    435 ktcp_wrappers-libs    x86_64    7.6-77.el7                     base        66 kTransaction Summary
================================================================================
Install  2 Packages (+4 Dependent packages)Total download size: 1.2 M
Installed size: 3.4 M
Downloading packages:
Public key for fipscheck-lib-1.4.1-5.el7.x86_64.rpm is not installed
warning: /var/cache/yum/x86_64/7/base/packages/fipscheck-lib-1.4.1-5.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for openssh-6.6.1p1-25.el7_2.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total                                              593 kB/s | 1.2 MB  00:02     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5Package    : centos-release-7-2.1511.el7.centos.2.10.x86_64 (@CentOS)From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transactionInstalling : fipscheck-1.4.1-5.el7.x86_64                                 1/6 Installing : fipscheck-lib-1.4.1-5.el7.x86_64                             2/6 Installing : openssh-6.6.1p1-25.el7_2.x86_64                              3/6 Installing : tcp_wrappers-libs-7.6-77.el7.x86_64                          4/6 Installing : openssh-server-6.6.1p1-25.el7_2.x86_64                       5/6 Installing : net-tools-2.0-0.17.20131004git.el7.x86_64                    6/6 Verifying  : openssh-6.6.1p1-25.el7_2.x86_64                              1/6 Verifying  : openssh-server-6.6.1p1-25.el7_2.x86_64                       2/6 Verifying  : net-tools-2.0-0.17.20131004git.el7.x86_64                    3/6 Verifying  : tcp_wrappers-libs-7.6-77.el7.x86_64                          4/6 Verifying  : fipscheck-lib-1.4.1-5.el7.x86_64                             5/6 Verifying  : fipscheck-1.4.1-5.el7.x86_64                                 6/6 Installed:net-tools.x86_64 0:2.0-0.17.20131004git.el7                                   openssh-server.x86_64 0:6.6.1p1-25.el7_2                                      Dependency Installed:fipscheck.x86_64 0:1.4.1-5.el7       fipscheck-lib.x86_64 0:1.4.1-5.el7      openssh.x86_64 0:6.6.1p1-25.el7_2    tcp_wrappers-libs.x86_64 0:7.6-77.el7   Complete!---> 7b249ed8cb54
Removing intermediate container 5626d8f0f892
Step 4 : RUN mkdir /var/run/sshd---> Running in fc94a139d438---> ea2826eccc91
Removing intermediate container fc94a139d438
Step 5 : RUN echo 'root:iloveworld' | chpasswd---> Running in ba53283081a7---> 7ce1ddb5d9c0
Removing intermediate container ba53283081a7
Step 6 : RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config---> Running in 4112281a5bf0---> be21fb6b5b1e
Removing intermediate container 4112281a5bf0
Step 7 : ENV HISTTIMEFORMAT "%F %T  "---> Running in f2081726e403---> f3fafca42170
Removing intermediate container f2081726e403
Step 8 : RUN ssh-keygen -A---> Running in 2ca9e743dee7
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA ED25519 ---> 1a927943bee7
Removing intermediate container 2ca9e743dee7
Step 9 : RUN \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime---> Running in afd43cc6d4d6---> 4a0cacf6cd72
Removing intermediate container afd43cc6d4d6
Step 10 : EXPOSE 22---> Running in a03551bc3bcb---> 3af544106bf4
Removing intermediate container a03551bc3bcb
Step 11 : CMD /usr/sbin/sshd -D---> Running in f45fe5eb5561---> d4620c9949b8
Removing intermediate container f45fe5eb5561
Successfully built d4620c9949b8
docker build stdout

3.查看镜像列表,并创建容器

[root@docker centos7-dockerfile]# docker images
REPOSITORY                                     TAG                 IMAGE ID            CREATED             SIZE
centos_sshd_1                                  latest              d4620c9949b8        4 minutes ago       308.4 MB
centos_sshd                                    7.0                 7bb4efd82c4f        2 days ago          310.1 MB

我们刚刚新建的容器已经存在了,现在用它来创建容器

[root@docker centos7-dockerfile]# docker run -d -it --name centos-two centos_sshd_1
7ae51091c138d249b5e97f6957073e748db278c0f1cf856e968ca78a4aec1a5b查看容器
[root@docker centos7-dockerfile]# docker ps   
CONTAINER ID        IMAGE                    COMMAND               CREATED             STATUS              PORTS             NAMES
7ae51091c138        centos_sshd_1            "/usr/sbin/sshd -D"   16 seconds ago      Up 15 seconds       22/tcp            centos-two

👌,可以看到容器的command 就是我们之前定义启动ssh 服务的,并且开放了22端口。

现在我们在宿主机上查看下该容器的ip,然后用ssh 链接进去。

[root@docker ~]# docker exec centos-two hostname -I
172.17.0.7[root@docker ~]# ssh root@172.17.0.7
The authenticity of host '172.17.0.7 (172.17.0.7)' can't be established.
ECDSA key fingerprint is 7a:38:69:d7:5e:f4:db:e8:3c:ea:92:a4:1a:a1:7b:9a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.7' (ECDSA) to the list of known hosts.
root@172.17.0.7's password: 
[root@7ae51091c138 ~]# w11:19:34 up 65 days, 18:34,  1 user,  load average: 0.01, 0.04, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    gateway          11:19    6.00s  0.00s  0.00s w

OK。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/69072.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

电气工程中重要的测量术语:“kVRMS” | 百能云芯

在电气工程和电子领域&#xff0c;术语“kVRMS”至关重要。它是工程师和技术人员用来准确评估电气系统电压的关键测量方法。在这篇综合文章中&#xff0c;我们将深入探讨 kVRMS 的含义、其意义、应用。 kVRMS 代表“千伏均方根”。为了理解这个术语&#xff0c;我们来分解一下&…

Linux的服务器日志分析及性能调优

作为网络安全和数据传输的重要环节&#xff0c;代理服务器在现代互联网中扮演着至关重要的角色。然而&#xff0c;在高负载情况下&#xff0c;代理服务器可能面临性能瓶颈和效率问题。本文将介绍如何利用Linux系统对代理服务器进行日志分析&#xff0c;并提供一些实用技巧来优化…

rpm打包

文章目录 rpm打包 1. rpm打包步骤0&#xff09;准备工作&#xff1a;安装打包工具rpm-build和rpmdevtools&#xff08;1&#xff09;在线安装&#xff08;2&#xff09;离线安装 1&#xff09;创建初始化目录2&#xff09;准备打包内容3&#xff09;编写打包脚本 spec文件4&…

测试需求分析

什么是软件测试需求&#xff1a; 灰度测试&#xff1a;先发布部分功能&#xff0c;然后看用户的反馈&#xff0c;再去发布另外一部分的更新 A/B测试&#xff1a;先发布的功能先让A部分的用户进行更新&#xff0c;再根据用户的犯困再更新B用户的功能 需求测试&#xff1a; 功…

hive 基础知识

一 hive 是什么 在本节前我们需要明确 hive 是什么 上面两个代码块&#xff0c;左边的是 mapreduce 的代码块&#xff0c;右边的是hive 的代码块 很容易看出来&#xff0c;右边的 hive 写起来要更容易更快些&#xff0c;而执行效率&#xff0c;右边的 hive 只比左边多一个翻译…

Redis 7 第六讲 主从模式(replica)架构篇

🌹🌹🌹 此篇开始进入架构篇范围(❤艸`❤) 理论 即主从复制,master以写为主,Slave以读为主。当master数据变化的时候,自动将新的数据异步同步到其它slave数据库。 使用场景 读写分离 容灾备份数据备份水平扩容主从架构 演示案例 注:masterauth、replicaof主…

直线导轨的替换方法

目前&#xff0c;直线导轨的使用率持续上升&#xff0c;已广泛应用在各种各样的行业中&#xff0c;可替换性高是其广泛使用的重要原因之一&#xff01;直线导轨的替换指的就是导轨和滑块可以单出&#xff0c;不用整套替换。 市面上使用率最高的直线导轨品牌应该就是台湾*银了&a…

优先级队列priority_queue以及仿函数的使用

目录 优先级队列priority_queuepriority_queue的模拟实现仿函数 优先级队列priority_queue 优先级队列priority_queue是一种容器适配器&#xff0c;根据严格的弱排序标准&#xff0c;它默认第一个元素总是它所包含的元素中最大的 优先级队列默认使用vector作为底层存储数据的…

Apollo在Java中的使用

本节主要讲解在普通的 Java 项目和 Spring Boot 中如何使用 Apollo。 普通 Java 项目中使用 加入 Apollo Client 的 Maven 依赖&#xff0c;代码如下所示。 <dependency><groupId>com.ctrip.framework.apollo</groupId><artifactId>apollo-client<…

kali 安装cpolar内网穿透实现 ssh 远程连接

文章目录 1. 启动kali ssh 服务2. kali 安装cpolar 内网穿透3. 配置kali ssh公网地址4. 远程连接5. 固定连接SSH公网地址6. SSH固定地址连接测试 简单几步通过cpolar 内网穿透软件实现ssh 远程连接kali! 1. 启动kali ssh 服务 默认新安装的kali系统会关闭ssh 连接服务,我们通…

【3】单着色器文件读取

Basic.shader文件&#xff0c;可以发现顶点着色器和片段着色器是写在一个文件里的&#xff0c;这里我们将他们读取出来&#xff0c;而不是上一篇使用string的方式。 #shader vertex #version 330 corelayout(location 0) in vec4 position;void main() {gl_Position positio…

TTransportException SASL authentication not complete

今天遇见了一个异常&#xff0c;但是发现是自己智障了 但还是记录一下 在close的时候先close了conn再close的statement导致报的这个错

概念解析 | 无线智能空口:打造下一代无线通讯网络的关键技术

注1:本文系“概念解析”系列之一,致力于简洁清晰地解释、辨析复杂而专业的概念。本次辨析的概念是:无线智能空口。 无线智能空口:打造下一代无线通讯网络的关键技术 前序 “空口"是无线通信中的一个专业术语,它主要用于描述无线通信设备与设备之间的通信接口。我们可…

Level-based Foraging 多智能体游戏仿真环境

游戏场景测试 参考链接&#xff1a; https://kgithub.com/semitable/lb-foraging

CNN详细讲解

CNN(Convolutional Neural Network) 本文主要来讲解卷积神经网络。所讲解的思路借鉴的是李宏毅老师的课程。 CNN&#xff0c;它是专门被用在影像上的。 Image Classification 我们从影像分类开始说起。 我们举例来说&#xff0c;它固定的输入大小是100*100的解析度&#x…

FastAPI 参数的作用

FastAPI是一个现代化的Python web框架&#xff0c;其参数具有重要的作用。在FastAPI中&#xff0c;参数被用于接收HTTP请求中的数据及其它相关信息。 FastAPI支持的参数类型包括&#xff1a; 查询参数&#xff08;query parameters&#xff09; 查询参数是指将参数附加到URL末…

《Python魔法大冒险》007 被困的精灵:数据类型的解救

小鱼和魔法师深入魔法森林&#xff0c;树木之间流淌着神秘的光芒&#xff0c;每一片叶子都似乎在低语着古老的咒语。不久&#xff0c;他们来到了一个小湖旁&#xff0c;湖中央有一个小岛&#xff0c;岛上困着一个透明的泡泡&#xff0c;里面有一个悲伤的精灵。 小鱼看着那个精…

软件行业25年技术发展史

语言时代 -> 框架时代 -> 分布式架构时代 -> 微服务架构时代 25年开发、管理&#xff0c;11年教培&#xff08;教研总监&#xff09;技术总结&#xff1a; 1997年 VB 1999年 ASPCOM 2004年 C# / JAVA、j2ee、ejb、struts1hibernate 2008年 旧三大框架 Struts2Spr…

MySQL 是如何实现事务的四大特性的?

分析&回答 如果你不知道事务更不知道四大特性请先看看&#xff1a;说说什么是事务 原子性 语句要么都执行&#xff0c;要么都不执行&#xff0c;是事务最核心的特性&#xff0c;事务本身来说就是以原子性来定义的&#xff0c;实现主要是基于undo log undo log&#xff…

探索ClickHouse——安装和测试

我们在Ubuntu 20 Server版虚拟机上对ClickHouse进行探索。 安装 检测环境 grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 supported" || echo "SSE 4.2 not supported"SSE 4.2 supported 可以看到我们的环境支持编译版本的。如果不支持的环境…