Docker:dockerfile自动构建镜像 [六]

一、手动docker镜像的缺点

相对于手动制作的docker镜像,使用dockerfile构建的镜像有以下优点:


1、dockerfile只有几kb,便于传输

2、使用dockerfile构建出来的镜像,在运行容器的时候,不用指定容器的初始命令

3、支持更多的自定义操作

二、dockerfile常用指令

1、dockerfile常用指令:

FROM 这个镜像的妈妈是谁?(指定基础镜像)
MAINTAINER 告诉别人,谁负责养它?(指定维护者信息,可以没有)
RUN 你想让它干啥(在命令前面加上RUN即可)
ADD 给它点创业资金(COPY文件,会自动解压)
WORKDIR 我是cd,今天刚化了妆(设置当前工作目录)
VOLUME 给它一个存放行李的地方(设置卷,挂载主机目录)
EXPOSE 它要打开的门是啥(指定对外的端口)(-P 随机端口)
CMD 奔跑吧,兄弟!(指定容器启动后的要干的事情)(容易被替换)

2、dockerfile其他指令:

COPY 复制文件
ENV 环境变量
ENTRYPOINT 容器启动后执行的命令(无法被替换,启容器的时候指定的命令,会被当成参数

三、dockerfile实战1

1、手动制作docker镜像步骤:

1:启动容器安装软件服务

docker run -it -p 1022:22 --name luoahong centos:6.9 
######
yum install openssh-server -y
/etc/init.d/sshd start
echo 123456|passwd --stdin root 
######

2:将安装好服务的容器commit提交为镜像

docker commit luoahong centos6-ssh:v1

3: 启动新容器来测试新提交的镜像

docker run -d -p 2022:22 centos6-ssh:v1 /usr/sbin/sshd -D


2、dockerfile制作docker镜像步骤:

1:编写dockerfile

[root@luoahong centos6_ssh]# pwd
/opt/dockerfile/centos6_ssh
[root@luoahong centos6_ssh]# ll
total 4
-rw-r--r-- 1 root root 159 Jan 15 18:42 dockerfile
[root@luoahong centos6_ssh]# cat 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构建镜像

[root@luoahong centos6_ssh]# docker build -t centos6-ssh:v2 .

3: 启动新容器来测试新构建的镜像

[root@luoahong centos6_ssh]# docker run -d -p 1322:22 centos6-ssh:v2
dc6ed1878218e7f13ac753cd5eec290eca1b1c06a1c4da6125b37e4d4f849c97
[root@luoahong centos6_ssh]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS                         PORTS     NAMES
dc6ed1878218        centos6-ssh:v2         "/usr/sbin/sshd -D"    5 seconds ago       Up 4 seconds                   0.0.0.0:1322->22/tcp     cranky_colden
ea82b9583081        centos6-ssh-httpd:v1   "/bin/bash /init.sh"   About an hour ago   Up About an hour               0.0.0.0:1122->22/tcp, 0.0.0.0:8080->80/tcp   stupefied_rosalind
0d09d3a1591a        centos:6.9             "/bin/bash"            About an hour ago   Exited (0) About an hour ago     luoahong
156d80283acf        centos6-ssh:v1         "/usr/sbin/sshd -D"    About an hour ago   Up About an hour               0.0.0.0:2022->22/tcp     romantic_chandrasekhar
0485b9ecd1ad        centos:6.9             "/bin/bash"            2 hours ago         Exited (0) About an hour ago     luoahong4

4、测试

[root@luoahong ~]# ssh root@192.168.228.134 -p 1322
The authenticity of host '[192.168.228.134]:1322 ([192.168.228.134]:1322)' can't be established.
RSA key fingerprint is SHA256:F6zybEUDFDsU7O33NYhVZrwhSkANZvKhSEXDwc6+lVY.
RSA key fingerprint is MD5:04:47:c1:09:1c:0f:46:48:1c:7c:7c:d7:22:71:54:7a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.228.134]:1322' (RSA) to the list of known hosts.
root@192.168.228.134's password:
[root@dc6ed1878218 ~]#

四、dockerfile实战2

1、手动制作docker镜像步骤:

1:启动容器安装软件服务

docker run -it -p 1022:22 --name luoahong centos:6.9 
######
yum install openssh-server -y
/etc/init.d/sshd start
echo 123456|passwd --stdin root 
######

2:将安装好服务的容器commit提交为镜像

docker commit luoahong centos6-ssh:v1

3: 启动新容器来测试新提交的镜像

docker run -d -p 2022:22 centos6-ssh:v1 /usr/sbin/sshd -D

2、dockerfile制作docker镜像步骤:

1:编写dockerfile

[root@luoahong centos6_ssh_http]# pwd
/opt/dockerfile/centos6_ssh_http
[root@luoahong centos6_ssh_http]# ll
total 8
-rw-r--r-- 1 root root 193 Jan 15 18:57 dockerfile
-rw-r--r-- 1 root root 54 Jan 15 19:02 init.sh
[root@luoahong centos6_ssh_http]# cat dockerfile
FROM centos:6.9
RUN yum install openssh-server httpd -y
RUN /etc/init.d/sshd start
RUN echo 123456|passwd --stdin root
ADD init.sh /init.sh
CMD [/bin/bash","/init.sh"]

编写容器启动脚本

[root@luoahong centos6_ssh_http]# cat init.sh
#!/bin/bash
/etc/init.d/httpd start
/usr/sbin/sshd -D

2:docker build构建镜像

[root@luoahong centos6_ssh_http]# docker build -t centos6-ssh-httpd:v2

3: 启动新容器来测试新构建的镜像

[root@luoahong centos6_ssh_http]# docker run -d -p 8080:80 -p 2122:22 centos6-ssh-httpd:v2
a5ff3069084c82a9b7b5030d4ee21e172fdedb2160b71ccbfb3e63b6cb979552
docker: Error response from daemon: driver failed programming external connectivity on endpoint xenodochial_swartz (78dc457d720cb6611f611b12690499acbe168aae92eb7913b23950e4d71ba860): Bind for 0.0.0.0:8080 failed: port is already allocated.
[root@luoahong centos6_ssh_http]# docker ps -a -l
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS               NAMES
a5ff3069084c        centos6-ssh-httpd:v2   "/bin/sh -c [/bin/ba鈥?   4 seconds ago       Created                                 xenodochial_swartz

4、测试

 

 

 

  

posted @ 2019-01-15 19:21  活的潇洒80  阅读(2057)  评论(0编辑  收藏  举报