Dockerfile自动构建docker镜像
类似ansible剧本,大小几kb
手动做镜像:大小几百M+
类似ansible剧本,大小几kb
手动做镜像:大小几百M+
dockerfile 支持自定义容器的初始命令
dockerfile主要组成部分:
基础镜像信息 FROM centos:6.9
制作镜像操作指令 RUN yum install openssh-server -y
容器启动时执行指令 CMD ["/bin/bash"]
dockerfile常用指令:
FROM 这个镜像的妈妈是谁?(指定基础镜像)
MAINTAINER 告诉别人,谁负责养它?(指定维护者信息,可以没有)
LABLE 描述,标签
RUN 你想让它干啥(在命令前面加上RUN即可)
ADD 给它点创业资金(会自动解压tar) 制作docker基础的系统镜像
WORKDIR 我是cd,今天刚化了妆(设置当前工作目录)
VOLUME 给它一个存放行李的地方(设置卷,挂载主机目录)
EXPOSE 它要打开的门是啥(指定对外的端口)(-P 随机端口)
CMD 奔跑吧,兄弟!(指定容器启动后的要干的事情)(容易被替换)
dockerfile其他指令:
COPY 复制文件(不会解压)rootfs.tar.gz
ENV 环境变量
ENTRYPOINT 容器启动后执行的命令(无法被替换,启容器的时候指定的命令,会被当成参数)
参考其他的dockerfile
官方dockerfile或者时速云镜像广场
官方dockerfile或者时速云镜像广场
[root@localhost centos6.9_ssh_nginx]# pwd /opt/dockerfile/centos6.9_ssh_nginx [root@localhost centos6.9_ssh_nginx]# cat dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx unzip RUN service sshd start RUN echo "123456"|passwd --stdin root RUN rm -rf /usr/share/nginx/html/index.html ADD xiaoniaofeifei.zip /usr/share/nginx/html/ RUN unzip /usr/share/nginx/html/xiaoniaofeifei.zip -d /usr/share/nginx/html/ ADD init.sh /init.sh CMD ["/bin/bash","/init.sh"] [root@localhost centos6.9_ssh_nginx]# cat init.sh #!/bin/bash service sshd restart nginx -g 'daemon off;' 创建镜像 docker build --network=host -t centos6.9_nginx_ssh:v1 . [root@localhost centos6.9_ssh_nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6.9_nginx_ssh v1 f01eeebf918a 9 minutes ago 431MB
优化上述dockerfile文件:
FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx unzip RUN service sshd start RUN echo "123456"|passwd --stdin root WORKDIR /usr/share/nginx/html 这里指的是之后所有命令都在这个路径下面执行 RUN rm -rf * ADD xiaoniaofeifei.zip . 这里最后是点不能忘记了,表示在当前目录 RUN unzip xiaoniaofeifei.zip ADD init.sh /init.sh CMD ["/bin/bash","/init.sh"]
执行创建案之后的命令结果是:
[root@localhost centos6.9_ssh_nginx]# docker build --network=host -t centos6.9_nginx_ssh:v2 . Sending build context to Docker daemon 94.72kB Step 1/12 : FROM centos:6.9 ---> adf829198a7f Step 2/12 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo ---> Using cache ---> bcfa68985b33 Step 3/12 : RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ---> Using cache ---> de91b8df3cdc Step 4/12 : RUN yum -y install openssh-server nginx unzip ---> Using cache ---> 66bb9530d868 Step 5/12 : RUN service sshd start ---> Using cache ---> 5974df83e839 Step 6/12 : RUN echo "123456"|passwd --stdin root ---> Using cache ---> d8cbe019d081 Step 7/12 : WORKDIR /usr/share/nginx/html ---> Using cache ---> e4bf78b38489 Step 8/12 : RUN rm -rf * ---> Using cache ---> a7040fa17ab7 Step 9/12 : ADD xiaoniaofeifei.zip . ---> dcc79248a09f Step 10/12 : RUN unzip xiaoniaofeifei.zip ---> Running in cbca6d5ca592 Archive: xiaoniaofeifei.zip inflating: sound1.mp3 creating: img/ inflating: img/bg1.jpg inflating: img/bg2.jpg inflating: img/number1.png inflating: img/number2.png inflating: img/s1.png inflating: img/s2.png inflating: 21.js inflating: 2000.png inflating: icon.png inflating: index.html Removing intermediate container cbca6d5ca592 ---> f4a99a6a7128 Step 11/12 : ADD init.sh /init.sh ---> 455bf5fe6902 Step 12/12 : CMD ["/bin/bash","/init.sh"] ---> Running in e5caf5424a18 Removing intermediate container e5caf5424a18 ---> 6307e23ee16d Successfully built 6307e23ee16d Successfully tagged centos6.9_nginx_ssh:v2 [root@localhost centos6.9_ssh_nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6.9_nginx_ssh v2 6307e23ee16d About a minute ago 431MB centos6.9_nginx_ssh v1 f01eeebf918a 16 minutes ago 431MB [localhost centos6.9_ssh_nginx]# docker run -d -p 80:80 -p 2222:22 centos6.9_nginx_ssh:v2 277d3714590b9b9faaba1b682790c694bcc96f0868293d5505b69848f8d03421 [root@localhost centos6.9_ssh_nginx]# docker exec -it 277d37145 /bin/bash [root@277d3714590b html]# pwd /usr/share/nginx/html 从这里就可以看出目录由于设定了WORKDIR,所以钉死在了这个目录下面
在上面可以看出任然还有可以优化的地方,所以继续,使用ADD命令可以直接解压tar包
FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx RUN service sshd start RUN echo "123456"|passwd --stdin root WORKDIR /usr/share/nginx/html RUN rm -rf * ADD xiaoniaofeifei.tar.gz . ADD可以直接解压tar包 ADD init.sh /init.sh CMD ["/bin/bash","/init.sh"] [root@localhost centos6.9_ssh_nginx]# docker build --network=host -t centos6.9_nginx_ssh:v3 . Sending build context to Docker daemon 186.9kB Step 1/11 : FROM centos:6.9 ---> adf829198a7f Step 2/11 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo ---> Using cache ---> bcfa68985b33 Step 3/11 : RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ---> Using cache ---> de91b8df3cdc Step 4/11 : RUN yum -y install openssh-server nginx ---> Using cache ---> 16a434cf89be Step 5/11 : RUN service sshd start ---> Using cache ---> 60f03276a66c Step 6/11 : RUN echo "123456"|passwd --stdin root ---> Using cache ---> b4dc176a311b Step 7/11 : WORKDIR /usr/share/nginx/html ---> Using cache ---> 209c253860e4 Step 8/11 : RUN rm -rf * ---> Using cache ---> d8ca4eff7ee9 Step 9/11 : ADD xiaoniaofeifei.tar.gz . ---> 139914b243cb Step 10/11 : ADD init.sh /init.sh ---> 482f2582dd02 Step 11/11 : CMD ["/bin/bash","/init.sh"] ---> Running in 66b1794dbe6e Removing intermediate container 66b1794dbe6e ---> 92b1087df3f5 Successfully built 92b1087df3f5 Successfully tagged centos6.9_nginx_ssh:v3 [root@localhost centos6.9_ssh_nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6.9_nginx_ssh v3 92b1087df3f5 About a minute ago 431MB centos6.9_nginx_ssh v2 6307e23ee16d 19 minutes ago 431MB centos6.9_nginx_ssh v1 f01eeebf918a 35 minutes ago 431MB
企业标准化EXPOSE
FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx RUN service sshd start RUN echo "123456"|passwd --stdin root WORKDIR /usr/share/nginx/html RUN rm -rf * ADD xiaoniaofeifei.tar.gz . ADD init.sh /init.sh EXPOSE 80/tcp 22/tcp CMD ["/bin/bash","/init.sh"] [root@localhost centos6.9_ssh_nginx]# docker build --network=host -t centos6.9_nginx_ssh:v4 . Sending build context to Docker daemon 186.9kB Step 1/12 : FROM centos:6.9 ---> adf829198a7f Step 2/12 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo ---> Using cache ---> bcfa68985b33 Step 3/12 : RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ---> Using cache ---> de91b8df3cdc Step 4/12 : RUN yum -y install openssh-server nginx ---> Using cache ---> 16a434cf89be Step 5/12 : RUN service sshd start ---> Using cache ---> 60f03276a66c Step 6/12 : RUN echo "123456"|passwd --stdin root ---> Using cache ---> b4dc176a311b Step 7/12 : WORKDIR /usr/share/nginx/html ---> Using cache ---> 209c253860e4 Step 8/12 : RUN rm -rf * ---> Using cache ---> d8ca4eff7ee9 Step 9/12 : ADD xiaoniaofeifei.tar.gz . ---> Using cache ---> 139914b243cb Step 10/12 : ADD init.sh /init.sh ---> Using cache ---> 482f2582dd02 Step 11/12 : EXPOSE 80/tcp 22/tcp ---> Running in 4a9a9c270afc Removing intermediate container 4a9a9c270afc ---> b980eead4bdc Step 12/12 : CMD ["/bin/bash","/init.sh"] ---> Running in fc73bab98352 Removing intermediate container fc73bab98352 ---> ac9eca43049a Successfully built ac9eca43049a Successfully tagged centos6.9_nginx_ssh:v4 [root@localhost centos6.9_ssh_nginx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos6.9_nginx_ssh v4 ac9eca43049a 45 seconds ago 431MB centos6.9_nginx_ssh v3 92b1087df3f5 11 minutes ago 431MB centos6.9_nginx_ssh v2 6307e23ee16d 29 minutes ago 431MB centos6.9_nginx_ssh v1 f01eeebf918a 45 minutes ago 431MB [root@localhost centos6.9_ssh_nginx]# docker run -d -P centos6.9_nginx_ssh:v4 697030125c669ee2e92ae09111833eec38cc00ba9ee367140de97ebcf95fbe00 [root@localhost centos6.9_ssh_nginx]# docker ps -al CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 697030125c66 centos6.9_nginx_ssh:v4 "/bin/bash /init.sh" 2 minutes ago Up 2 minutes 0.0.0.0:32769->22/tcp, 0.0.0.0:32768->80/tcp flamboyant_hoover
由上可知,加入了EXPOSE以后就能看到容器的端口出现随机映射的现象
volume的意义:
[root@localhost centos6.9_ssh_nginx]# vim dockerfile FROM centos:6.9 RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo RUN yum -y install openssh-server nginx RUN service sshd start RUN echo "123456"|passwd --stdin root WORKDIR /usr/share/nginx/html RUN rm -rf * ADD xiaoniaofeifei.tar.gz . ADD init.sh /init.sh EXPOSE 80/tcp 22/tcp VOLUME /usr/share/nginx/html CMD ["/bin/bash","/init.sh"] [root@localhost centos6.9_ssh_nginx]# docker run -d -P centos6.9_nginx_ssh:v5 .
[root@localhost centos6.9_ssh_nginx]# docker volume ls DRIVER VOLUME NAME local b56d4c1f5042c2f7c2e8afd4e53f4f6c0ac9a1c09d12c5dd81ab40c783e09f07 产生一个随机的卷