Dockerfile构建镜像

一、Dockerfile的条条框框
1、什么是Dockerfile?
Dockerfile是一种被Docker程序解释的脚本 。Dockerfile由一条一条的指令组成,并且有自己的书写格式和支持的命令 。当我们需要指定自己额外的需求时,只需在Dockerfile上添加或修改指令,然后通过docker build生成我们自定义的镜像(image) 。

Dockerfile构建镜像

文章插图
 
2、Dockerfile的注意事项
  • Dockerfile的指令是忽略大小写的,建议使用大写
  • 每一行只支持一条指令
  • 每条指令可以携带多个参数
  • 支持以#开头的注释
3、Dockerfile指令(instruction)
构建指令:用于构建image 。其指定的操作不会在运行image的容器上执行(FROM、MAINTAINER、RUN、ENV、ADD、COPY)
设置指令:用于设置image的属性 。其指定的操作将在运行image的容器中执行(CMD、ENTRYPOINT、USER 、EXPOSE、VOLUME、WORKDIR、ONBUILD)
4、Dockerfile的基本结构
基础镜像信息、维护者信息、镜像操作指令、容器启动时执行指令
二、Dockerfile指令(instruction)实操
Dockerfile构建镜像

文章插图
 
1、FROM(指定基础image,必须为第一个命令)
格式:
FROM <image>FROM <image>:<tag>例:
[root@localhost docker]# vim Dockerfile#FROM centosFROM centos:latest[root@localhost docker]# docker build -t my_test . #注意有个“.”Sending build context to Docker daemon 2.048kBStep 1/1 : FROM centos:latest ---> 9f38484d220fSuccessfully built 9f38484d220fSuccessfully tagged my_test:latest2、MAINTAINER(维护者,用来指定创建者信息)
格式:
MAINTAINER <name>例:(通过inspect验证)
[root@localhost docker]# vim Dockerfile
#FROM centos
FROM centos:latest
MAINTAINER Xuyun Liu <1972163777@qq.com>
[root@localhost docker]# docker build -t my_test .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM centos:latest
---> 9f38484d220f
Step 2/2 : MAINTAINER Xuyun Liu <1972163777@qq.com>
---> Using cache
---> be97dff9636f
Successfully built be97dff9636f
Successfully tagged my_test:latest
[root@localhost docker]# docker inspect -f {{.Author}} my_test
Xuyun Liu <1972163777@qq.com>
3、RUN(安装软件用)
格式:
RUN command param1 param2 #使用shell终端运行命令(/bin/sh、/bin/bash等)RUN ['executable(可执行的))','param1(参数)','param2'...] #不通过shell执行,而是通过内建命令exec去执行例:
[root@localhost docker]# vim Dockerfile#FROM centosFROM centos:latestMAINTAINER Xuyun Liu <1972163777@qq.com>RUN touch /home/a.txtRUN ["touch","/home/b.txt"] #这里双引号和单引号也是有讲究的,单引号会报错[root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 2.048kBStep 1/4 : FROM centos:latest ---> 9f38484d220fStep 2/4 : MAINTAINER Xuyun Liu <1972163777@qq.com> ---> Using cache ---> be97dff9636fStep 3/4 : RUN touch /home/a.txt ---> Using cache ---> 5af84bebdda1Step 4/4 : RUN ["touch","/home/b.txt"] ---> Running in 541b7c7da76aRemoving intermediate container 541b7c7da76a ---> 6e19811bbf18Successfully built 6e19811bbf18Successfully tagged my_test:latest[root@localhost docker]# docker run -it my_test ls /homea.txt b.txt4、CMD(设置container启动时执行的操作)
格式:
CMD command param1 param2CMD ["executable","param1","param2"...]CMD ["param1","param2"...] #当CMD和ENTRYPOINT配合使用时的写法例1:CMD指令会被docker run提供的参数覆盖
[root@localhost docker]# vim Dockerfile#FROM centosFROM centos:latest#MAINTAINER Xuyun Liu <1972163777@qq.com>RUN touch /home/a.txt#RUN ["touch","/home/b.txt"]CMD ls /dev[root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 2.048kBStep 1/3 : FROM centos:latest ---> 9f38484d220fStep 2/3 : RUN touch /home/a.txt ---> Running in 384f2554526aRemoving intermediate container 384f2554526a ---> cfb30d89ad46Step 3/3 : CMD ls /dev ---> Running in b568167236d3Removing intermediate container b568167236d3 ---> 1e1971815006Successfully built 1e1971815006Successfully tagged my_test:latest[root@localhost docker]# docker run -it my_testconsole fd mqueue ptmx random stderr stdout urandomcore full null pts shm stdin tty zero[root@localhost docker]# docker run -it my_test ls /homea.txt例2:当一个Dockfile文件中有两个或多个CMD指令时,只执行最后一条CMD指令
[root@localhost docker]# vim Dockerfile#FROM centosFROM centos:latest#MAINTAINER Xuyun Liu <1972163777@qq.com>RUN touch /home/a.txt#RUN ["touch","/home/b.txt"]CMD ls /devCMD ["ls","/usr","/usr/local"][root@localhost docker]# docker build -t my_test .Sending build context to Docker daemon 2.048kBStep 1/4 : FROM centos:latest ---> 9f38484d220fStep 2/4 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46Step 3/4 : CMD ls /dev ---> Using cache ---> 1e1971815006Step 4/4 : CMD ["ls","/usr","/usr/local"] ---> Running in ccfabb87efc5^[[ARemoving intermediate container ccfabb87efc5 ---> e439603840efSuccessfully built e439603840efSuccessfully tagged my_test:latest[root@localhost docker]# docker run -it my_test/usr:bin etc games include lib lib64 libexec local sbin share src tmp/usr/local:bin etc games include lib lib64 libexec sbin share src


推荐阅读