格式: #comment注释 INSTRUCTION argument FROM eddy/centos_nginx:latestMAINTAINER eddy "eddy@qq.com"RUN yum install -y vimEXPOSE 80 FROMFROMFROM:必须是存在的镜像,必须是第一条非注释的指令MAINTAINER指定镜像作者信息,包含镜像所有者和联系信息RUN指定当前镜像构建时的命令RUN shell模式RUN echo helloRUN [executeable,param1,param2] exec模式RUN ["/bin/bash" "-c","echo hello"]这里的RUN命令可合并使用&&构建过程可少一步yum install -y nginx && yum install -y vim EXPOSE指定运行该镜像的容器使用端口,可以使用多个运行容器时候任然要指定端口root@eddy:~/dockfile/df_test1# docker build -t "eddy/df_test1" .Sending build context to Docker daemon 2.048 kBStep 1 : FROM eddy/centos_nginx:latest ---> 72a842011cc1Step 2 : MAINTAINER eddy "eddy@qq.com" ---> Using cache ---> a16ca9a40f4fStep 3 : RUN yum install -y vim ---> Using cache ---> a27db230d99cStep 4 : EXPOSE 80 ---> Using cache ---> 058c96b40d77Successfully built 058c96b40d77root@eddy:~/dockfile/df_test1# docker run -d --name nginx_web2 -p 80 eddy/df_test1 nginx -g "daemon off;"CMD容器运行时执行的命令CMD command param1 param2 shell模式CMD echo helloCMD [executeable,param1,param2] exec模式CMD ["/bin/bash" "-c","echo hello"]CMD ["/usr/sbin/nginx" "-g","daemon off"]当在启动容器的时候添加了命令,dockerfile中的指令会被覆盖CMD [param1,param2]做为ENTERYPOINT指令的默认参数ENTERYPOINT和CMD很类似,区别就是不会被启动镜像的命令所覆盖ENTERYPOINT [executeable,param1,param2] exec模式ENTERYPOINT command param1 param2 shell模式ENTERYPOINT ["/usr/sbin/nginx" "-g","daemon off"]组合使用CMD指定参数,ENTERYPOINT执行命令ENTERYPOINT ["/usr/sbin/nginx"]CMD ["-h"]这样在容器启动的时候执行命令会覆盖CMD并以参数的形式传递给ENTERYPOINTADDADD ["src"..."dest"]包含类似tar解压功能COPYCOPY ["src"..."dest"]单纯复制文件docker推荐使用copyCOPY index.html /usr/share/nginx/html/VOLUMEVOLUME["/data"]基于镜像的容器创建卷WORKDIRWORKDIR /path/to/workdir通常不使用绝对路径WORKDIR aWORKDIR bWORKDIR cRUN pwd/a/b/cENVENV ENV = USER镜像以什么用户启动USER userUSER uidUSER user:groupUSER user:gidUSER uid:gidUSER uid:group默认rootONBUILD镜像触发器当一个镜像被其他镜像做为基础镜像执行时会在构建过程中插入指令一个完整的dockerfile文件内容FROM eddy/centos_nginx:latestMAINTAINER eddy "eddy@qq.com"RUN yum clean allRUN yum install -y vimONBUILD COPY index.html /var/share/nginx/html/EXPOSE 80 ENTERYPOINT ["/usr/bin/nginx","-g","daemon off"]构建镜像docker build -t "eddy/web01"这里是不会执行COPY的以这个镜像会基础镜像构建FROM eddy/web01MAINTAINER eddy "eddy@qq.com"EXPOSE 80 ENTERYPOINT ["/usr/bin/nginx","-g","daemon off"]这里会执行CPOY从基础镜像运行一个容器执行一条指令,对容器进行修改执行类似docker commit的操作,提交一个新的镜像层再基于刚提交的镜像运行一个新的容器执行dockerfile中的下一条指令,直至所有指令执行完毕FROM eddy/centos_nginx:latestMAINTAINER eddy "eddy@qq.com"RUN yum install -y vim && yum install -y nginxEXPOSE 80root@eddy:~/dockfile/df_test1# docker build -t "eddy/df_test1" .Sending build context to Docker daemon 2.048 kBStep 1 : FROM eddy/centos_nginx:latest ---> 72a842011cc1Step 2 : MAINTAINER eddy "eddy@qq.com" ---> Using cache ---> a16ca9a40f4fStep 3 : RUN yum install -y vim ---> Using cache ---> a27db230d99cStep 4 : EXPOSE 80 ---> Using cache ---> 058c96b40d77 不会删除中间层镜像可以进入看是否执行完毕,查找错误 docker run -it a27db230d99c /bin/bash 进入查看nginx是否安装完毕构建缓存每一个结果都会提交为镜像会把之前的镜像作为缓存不使用缓存docker build --no-cache使用ENV=REFRESH_DATE=2016-01-24修改这个时间会在dockerfile中下面的命令不使用缓存查看镜像构建过程docker history eddy/web01root@eddy:~/dockfile/df_test1# docker history eddy/df_test1IMAGE CREATED CREATED BY SIZE COMMENT058c96b40d77 6 hours ago /bin/sh -c #(nop) EXPOSE 80/tcp 0 B a27db230d99c 6 hours ago /bin/sh -c yum install -y vim 36.32 MB a16ca9a40f4f 6 hours ago /bin/sh -c #(nop) MAINTAINER eddy "eddy@qq.co 0 B 72a842011cc1 7 hours ago /bin/bash 163.2 MB nginx9a769720073e 2 days ago 289.6 MB Imported from -