美文网首页Docker容器部署运维GIS
跟我学Docker:dockerfile自动构建镜像(八)

跟我学Docker:dockerfile自动构建镜像(八)

作者: 为道日损_原创笔记 | 来源:发表于2019-10-11 16:41 被阅读0次

相对于手动制作的docker镜像,使用dockerfile构建的镜像有以下优点:
1:dockerfile只有几kb,便于传输
2:使用dockerfile构建出来的镜像,在运行容器的时候,不用指定容器的初始命令
3:支持更多的自定义操作

dockerfile常用指令:
FROM 这个镜像的妈妈是谁?(指定基础镜像)     
MAINTAINER 告诉别人,谁负责养它?(指定维护者信息,可以没有)     
RUN 你想让它干啥(在命令前面加上RUN即可)     
ADD 给它点创业资金(复制文件,会自动解压)     
WORKDIR 我是cd,今天刚化了妆(设置当前工作目录)     
VOLUME 给它一个存放行李的地方(设置卷,挂载主机目录)     
EXPOSE 它要打开的门是啥(指定对外的端口)(-P 随机端口)     
CMD 奔跑吧,兄弟!(指定容器启动后的要干的事情)(容易被替换)  
dockerfile其他指令:
COPY 复制文件     
ENV  环境变量    
ENTRYPOINT  容器启动后执行的命令(无法被替换,启容器的时候指定的命令,会被当成参数)
dockerfile实战1(注意dockerfile与传统手动构建docker镜像的区别)
  • 手动构建镜像
1:启动容器安装软件服务
docker run -it -p 1022:22 --name fxw centos:6.9 
######
yum install openssh-server -y
/etc/init.d/sshd start
echo 123456|passwd  --stdin root 
######

2:将安装好服务的容器commit提交为镜像
docker commit fxw centos6-ssh:v1

3:   启动新容器来测试新提交的镜像
docker run -d -p 2022:22 centos6-ssh:v1  /usr/sbin/sshd -D
  • dockerfile构建镜像
1:编写dockerfile
vi  dockerfile
FROM  centos:6.9
RUN     yum install openssh-server -y
RUN     /etc/init.d/sshd start
RUN     echo 123456|passwd --stdin root 
CMD     ["/usr/sbin/sshd","-D"]

2:docker build构建镜像
docker build -t centos6-ssh:v2  .

3:   启动新容器来测试新构建的镜像
docker run -d -p 1322:22 centos6-ssh:v2 
  • 实战

1.在/opt目录下创建dockerfile/centos6_ssh/dockerfile,然后编写dockerfile,如下操作:

[root@docker01 opt]# mkdir dockerfile
[root@docker01 opt]# cd dockerfile/
[root@docker01 dockerfile]# mkdir centos6_ssh
[root@docker01 dockerfile]# cd centos6_ssh/
[root@docker01 centos6_ssh]# vim dockerfile

FROM    centos:6.9
RUN     yum install openssh-server -y
RUN     /etc/init.d/sshd start
RUN     echo 123456|passwd --stdin root 
CMD     ["/usr/sbin/sshd","-D"]

2.构建docker镜像(docker image build -t centos6_ssh:v2 .),-t 指定镜像名称,注意后面有个点,表示当前位置:

[root@docker01 centos6_ssh]# docker image build -t centos6_ssh:v2 .
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM    centos:6.9
 ---> 2199b8eb8390
Step 2/5 : RUN     yum install openssh-server -y
 ---> Running in 7948c359c433
Loaded plugins: fastestmirror, ovl
Setting up Install Process
Resolving Dependencies
--> Running transaction check
   .
   .
   .
省略部分
   .
   .
   .
Complete!
Removing intermediate container 7948c359c433
 ---> 1b4c583de43a
Step 3/5 : RUN     /etc/init.d/sshd start
 ---> Running in 9f51bb8c01af
Generating SSH2 RSA host key: [  OK  ]
Generating SSH1 RSA host key: [  OK  ]
Generating SSH2 DSA host key: [  OK  ]
Starting sshd: [  OK  ]
Removing intermediate container 9f51bb8c01af
 ---> f8f1d7a51f3a
Step 4/5 : RUN     echo 123456|passwd --stdin root
 ---> Running in cf61b9584057
Changing password for user root.
passwd: all authentication tokens updated successfully.
Removing intermediate container cf61b9584057
 ---> acd69ba39d72
Step 5/5 : CMD     ["/usr/sbin/sshd","-D"]
 ---> Running in 2a34bf3a22e0
Removing intermediate container 2a34bf3a22e0
 ---> 023162318c9a
Successfully built 023162318c9a
Successfully tagged centos6_ssh:v2
[root@docker01 centos6_ssh]# 

查看镜像是否构建成功:

[root@docker01 centos6_ssh]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6_ssh         v2                  023162318c9a        4 minutes ago       309MB
[root@docker01 centos6_ssh]# 

3.启动新构建的镜像

[root@docker01 centos6_ssh]# docker run -d -p 1322:22 centos6_ssh:v2
03aae160a9dcf9a1aa6cd30576739df825fead46f8b1d46fc476bb0fedc5c36b
[root@docker01 centos6_ssh]# docker ps -a -l
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                  NAMES
03aae160a9dc        centos6_ssh:v2      "/usr/sbin/sshd -D"   10 seconds ago      Up 9 seconds        0.0.0.0:1322->22/tcp   silly_stonebraker
[root@docker01 centos6_ssh]#

4.测试ssh,连接成功。

[root@docker01 centos6_ssh]# ssh root@10.0.0.11 -p 1322
The authenticity of host '[10.0.0.11]:1322 ([10.0.0.11]:1322)' can't be established.
RSA key fingerprint is SHA256:fy+mmO6sGYcnPGLVmmKe7bMeIoVf8B1e6koFLMveZEM.
RSA key fingerprint is MD5:6f:f0:e4:4f:e2:2c:3a:af:75:38:9c:4c:9a:4f:24:62.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.0.0.11]:1322' (RSA) to the list of known hosts.
root@10.0.0.11's password: 
[root@03aae160a9dc ~]# 
  • 扩展1:配置并启动多个服务的docker容器,只需修改dockerfile,如下:(ADD file1 file2表示将file1从宿主机的当前目录复制到容器的木录下)
[root@docker01 centos6_ssh]# vim dockerfile 

FROM    centos:6.9
RUN     yum install openssh-server -y
RUN     /etc/init.d/sshd start
RUN     echo 123456|passwd --stdin root
RUN     yum install httpd -y
ADD     init.sh /init.sh
CMD     ["/bin/bash","/init.sh"]

并且需要在当前目录下添加init.sh:

[root@docker01 centos6_ssh]# vim init.sh

#!/bin/bash
/etc/init.d/httpd start
/usr/sbin/sshd -D

构建,启动,测试容器

[root@docker01 centos6_ssh]# docker build -t centos6.9_http_ssh:v2 .   #构建容器
Sending build context to Docker daemon  3.072kB
Step 1/7 : FROM    centos:6.9
 ---> 2199b8eb8390
Step 2/7 : RUN     yum install openssh-server -y
 ---> Using cache   #使用缓存
 ---> 1b4c583de43a
Step 3/7 : RUN     /etc/init.d/sshd start
 ---> Using cache   #使用缓存
 ---> f8f1d7a51f3a
Step 4/7 : RUN     echo 123456|passwd --stdin root
 ---> Using cache   #使用缓存
 ---> acd69ba39d72
Step 5/7 : RUN  yum install httpd -y
 ---> Running in 093a92bf39fe
Loaded plugins: fastestmirror, ovl
Setting up Install Process
Determining fastest mirrors
 * base: ftp.sjtu.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
Resolving Dependencies
   .
   .
   .
省略部分
   .
   .
   .                                
  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1                                        
  httpd-tools.x86_64 0:2.2.15-69.el6.centos                                     
  mailcap.noarch 0:2.1.31-2.el6                                                 

Complete!
Removing intermediate container 093a92bf39fe
 ---> a5bc747e0e9d
Step 6/7 : ADD  init.sh ./init.sh
 ---> 1bca69f9b863
Step 7/7 : CMD     ["/bin/bash","/init.sh"]
 ---> Running in 6f45ce7a47e6
Removing intermediate container 6f45ce7a47e6
 ---> c00fcc89d51e
Successfully built c00fcc89d51e
Successfully tagged centos6.9_http_ssh:v2
[root@docker01 centos6_ssh]# docker images    #查看容器
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos6.9_http_ssh   v2                  c00fcc89d51e        29 seconds ago      386MB
centos6_ssh          v2                  023162318c9a        27 minutes ago      309MB
[root@docker01 centos6_ssh]# docker run -d -p 1522:22 -p 85:80 centos6.9_http_ssh:v2 #启动容器
d213f7d980175b7f35eb6fffc90d48268bfe581e95ab1452c7380604ca01fc5c
[root@docker01 centos6_ssh]# curl -I 10.0.0.11:85  #测试httpd
HTTP/1.1 403 Forbidden
Date: Thu, 25 Jul 2019 07:51:05 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Content-Length: 4961
Connection: close
Content-Type: text/html; charset=UTF-8

[root@docker01 centos6_ssh]# ssh root@10.0.0.11 -p 1522   #测试sshd连接服务
The authenticity of host '[10.0.0.11]:1522 ([10.0.0.11]:1522)' can't be established.
RSA key fingerprint is SHA256:fy+mmO6sGYcnPGLVmmKe7bMeIoVf8B1e6koFLMveZEM.
RSA key fingerprint is MD5:6f:f0:e4:4f:e2:2c:3a:af:75:38:9c:4c:9a:4f:24:62.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.0.0.11]:1522' (RSA) to the list of known hosts.
root@10.0.0.11's password: 
[root@d213f7d98017 ~]# #成功
  • 扩展2:
    EXPOSE结合-P随机端口映射的使用
    EXPOSE 22 80 #指定开放映射端口,否则直接docier run -d -P centos6.9_http_ssh:v3无效
[root@docker01 centos6_ssh]# vim dockerfile 

FROM    centos:6.9
RUN     yum install openssh-server -y
RUN     /etc/init.d/sshd start
RUN     echo 123456|passwd --stdin root
RUN     yum install httpd -y
ADD     init.sh ./init.sh
EXPOSE  22 80   #指定开放映射端口,否则直接docier run -d -P centos6.9_http_ssh无效
CMD     ["/bin/bash","/init.sh"]
  • 扩展3:
    默认情况下,手动进入容器,将定位到容器的根目录:
[root@docker01 centos6_ssh]# docker build -t centos6.9_http_ssh:v4 .
Sending build context to Docker daemon  3.072kB
Step 1/9 : FROM    centos:6.9
 ---> 2199b8eb8390
Step 2/9 : RUN     yum install openssh-server -y
 ---> Using cache
 ---> 1b4c583de43a
Step 3/9 : RUN     /etc/init.d/sshd start
 ---> Using cache
 ---> f8f1d7a51f3a
Step 4/9 : RUN     echo 123456|passwd --stdin root
 ---> Using cache
 ---> acd69ba39d72
Step 5/9 : RUN  yum install httpd -y
 ---> Using cache
 ---> a5bc747e0e9d
Step 6/9 : ADD  init.sh ./init.sh
 ---> Using cache
 ---> 1bca69f9b863
Step 7/9 : EXPOSE   22 80
 ---> Using cache
 ---> 038d316a06e3
Step 8/9 : WORKDIR  /root
 ---> Using cache
 ---> 24544ae31045
Step 9/9 : CMD     ["/bin/bash","/init.sh"]
 ---> Using cache
 ---> 907fc318a171
Successfully built 907fc318a171
Successfully tagged centos6.9_http_ssh:v4
[root@docker01 centos6_ssh]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos6.9_http_ssh   v4                  907fc318a171        6 minutes ago       386MB
[root@docker01 centos6_ssh]# docker run -P -it 907fc318a171 /bin/bash
[root@858958ffa7f2 ~]# #当前目录为~,root目录
  • 扩展3:
    ENV 环境变量 ,实例(添加ENV,同时修改init.sh):
    vim dockerfile
[root@docker01 centos6_ssh]# vim dockerfile
FROM    centos:6.9
RUN     yum install openssh-server -y
RUN     /etc/init.d/sshd start
RUN     echo 123456|passwd --stdin root
RUN     yum install httpd -y
ADD     init.sh ./init.sh
EXPOSE  22 80
WORKDIR /root
ENV     SSH_PASSWD=123456  #新增
CMD     ["/bin/bash","/init.sh"]

vim init.sh

[root@docker01 centos6_ssh]# vim init.sh 

#!/bin/bash
echo $SSH_PASSWD|passwd --stdin root  #新增
/etc/init.d/httpd start
/usr/sbin/sshd -D

重新构建并连接ssh服务

[root@docker01 centos6_ssh]# docker build -t centos6.9_http_ssh:v5 .
Sending build context to Docker daemon  3.072kB
Step 1/10 : FROM    centos:6.9
 ---> 2199b8eb8390
Step 2/10 : RUN     yum install openssh-server -y
 ---> Using cache
 ---> 1b4c583de43a
Step 3/10 : RUN     /etc/init.d/sshd start
 ---> Using cache
 ---> f8f1d7a51f3a
Step 4/10 : RUN     echo 123456|passwd --stdin root
 ---> Using cache
 ---> acd69ba39d72
Step 5/10 : RUN yum install httpd -y
 ---> Using cache
 ---> a5bc747e0e9d
Step 6/10 : ADD init.sh ./init.sh
 ---> 3300442c4018
Step 7/10 : EXPOSE  22 80
 ---> Running in b6a585bd79dc
Removing intermediate container b6a585bd79dc
 ---> 2d3c85fd7725
Step 8/10 : WORKDIR /root
 ---> Running in afe5b48a9fa6
Removing intermediate container afe5b48a9fa6
 ---> 93861d4451e6
Step 9/10 : ENV SSH_PASSWD=123456
 ---> Running in 819d96bc4599
Removing intermediate container 819d96bc4599
 ---> f9a175cda34b
Step 10/10 : CMD     ["/bin/bash","/init.sh"]
 ---> Running in 23b365d3d5bc
Removing intermediate container 23b365d3d5bc
 ---> ea105d511658
Successfully built ea105d511658
Successfully tagged centos6.9_http_ssh:v5
[root@docker01 centos6_ssh]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
centos6.9_http_ssh   v5                  ea105d511658        5 seconds ago       386MB
[root@docker01 centos6_ssh]# docker run --env "SSH_PASSWD=231231" -d -P ea105d511658
c43e09f83c710d83dfb70258e6fb48ca80aae7be661e230f07dee5a83ec58c3d
[root@docker01 centos6_ssh]# docker ps -a |grep ea105d511658
c43e09f83c71        ea105d511658            "/bin/bash /init.sh"     About a minute ago   Up About a minute                0.0.0.0:32771->22/tcp, 0.0.0.0:32770->80/tcp   keen_villani
[root@docker01 centos6_ssh]# ssh root@10.0.0.11 -p 32771
The authenticity of host '[10.0.0.11]:32771 ([10.0.0.11]:32771)' can't be established.
RSA key fingerprint is SHA256:fy+mmO6sGYcnPGLVmmKe7bMeIoVf8B1e6koFLMveZEM.
RSA key fingerprint is MD5:6f:f0:e4:4f:e2:2c:3a:af:75:38:9c:4c:9a:4f:24:62.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.0.0.11]:32771' (RSA) to the list of known hosts.
root@10.0.0.11's password: #此处将不再是123456,而是231231
[root@c43e09f83c71 ~]# 
  • 扩展4:docker run --env -d -P ea105d511658 sleep 10,手动的初始命令sleep 10 会覆盖掉配置文件中的CMD命令(CMD命令将不会运行),如果想要CMD不被覆盖,并且运行手动添加的命令,则将CMD修改为ENTRYPOINT(初始命令不覆盖并随后运行手动添加的命令),如下:
FROM    centos:6.9
RUN     yum install openssh-server -y
RUN     /etc/init.d/sshd start
RUN     echo 123456|passwd --stdin root
RUN     yum install httpd -y
ADD     init.sh ./init.sh
EXPOSE  22 80
WORKDIR /root
ENV     SSH_PASSWD=123456
#CMD     ["/bin/bash","/init.sh"]  #删掉此行,用下行替换
ENTRYPOINT ["/bin/bash","/init.sh"]

相关文章

网友评论

    本文标题:跟我学Docker:dockerfile自动构建镜像(八)

    本文链接:https://www.haomeiwen.com/subject/umcxmctx.html