一、Dockerfile的條條框框
1、什么是Dockerfile?
Dockerfile是一種被Docker程序解釋的腳本。Dockerfile由一條一條的指令組成,并且有自己的書(shū)寫(xiě)格式和支持的命令。當(dāng)我們需要指定自己額外的需求時(shí),只需在Dockerfile上添加或修改指令,然后通過(guò)docker build生成我們自定義的鏡像(image)。

2、Dockerfile的注意事項(xiàng)
- Dockerfile的指令是忽略大小寫(xiě)的,建議使用大寫(xiě)
- 每一行只支持一條指令
- 每條指令可以攜帶多個(gè)參數(shù)
- 支持以#開(kāi)頭的注釋
3、Dockerfile指令(instruction)
構(gòu)建指令:用于構(gòu)建image。其指定的操作不會(huì)在運(yùn)行image的容器上執(zhí)行(FROM、MAINTAINER、RUN、ENV、ADD、COPY)
設(shè)置指令:用于設(shè)置image的屬性。其指定的操作將在運(yùn)行image的容器中執(zhí)行(CMD、ENTRYPOINT、USER 、EXPOSE、VOLUME、WORKDIR、ONBUILD)
4、Dockerfile的基本結(jié)構(gòu)
基礎(chǔ)鏡像信息、維護(hù)者信息、鏡像操作指令、容器啟動(dòng)時(shí)執(zhí)行指令
二、Dockerfile指令(instruction)實(shí)操

1、FROM(指定基礎(chǔ)image,必須為第一個(gè)命令)
格式:
FROM <image> FROM <image>:<tag>
例:
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest [root@localhost docker]# docker build -t my_test . #注意有個(gè)“.” Sending build context to Docker daemon 2.048kB Step 1/1 : FROM centos:latest ---> 9f38484d220f Successfully built 9f38484d220f Successfully tagged my_test:latest
2、MAINTAINER(維護(hù)者,用來(lái)指定創(chuàng)建者信息)
格式:
MAINTAINER <name>
例:(通過(guò)inspect驗(yàn)證)
[root@localhost docker]# vim Dockerfile
#FROM centos
FROM centos:latest
MAINTAINER Xuyun Liu <[email protected]>
[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 <[email protected]>
---> Using cache
---> be97dff9636f
Successfully built be97dff9636f
Successfully tagged my_test:latest
[root@localhost docker]# docker inspect -f {{.Author}} my_test
Xuyun Liu <[email protected]>
3、RUN(安裝軟件用)
格式:
RUN command param1 param2 #使用shell終端運(yùn)行命令(/bin/sh、/bin/bash等) RUN ['executable(可執(zhí)行的))','param1(參數(shù))','param2'...] #不通過(guò)shell執(zhí)行,而是通過(guò)內(nèi)建命令exec去執(zhí)行
例:
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest MAINTAINER Xuyun Liu <[email protected]> RUN touch /home/a.txt RUN ["touch","/home/b.txt"] #這里雙引號(hào)和單引號(hào)也是有講究的,單引號(hào)會(huì)報(bào)錯(cuò) [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : MAINTAINER Xuyun Liu <[email protected]> ---> Using cache ---> be97dff9636f Step 3/4 : RUN touch /home/a.txt ---> Using cache ---> 5af84bebdda1 Step 4/4 : RUN ["touch","/home/b.txt"] ---> Running in 541b7c7da76a Removing intermediate container 541b7c7da76a ---> 6e19811bbf18 Successfully built 6e19811bbf18 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls /home a.txt b.txt
4、CMD(設(shè)置container啟動(dòng)時(shí)執(zhí)行的操作)
格式:
CMD command param1 param2 CMD ["executable","param1","param2"...] CMD ["param1","param2"...] #當(dāng)CMD和ENTRYPOINT配合使用時(shí)的寫(xiě)法
例1:CMD指令會(huì)被docker run提供的參數(shù)覆蓋
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <[email protected]> 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.048kB Step 1/3 : FROM centos:latest ---> 9f38484d220f Step 2/3 : RUN touch /home/a.txt ---> Running in 384f2554526a Removing intermediate container 384f2554526a ---> cfb30d89ad46 Step 3/3 : CMD ls /dev ---> Running in b568167236d3 Removing intermediate container b568167236d3 ---> 1e1971815006 Successfully built 1e1971815006 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test console fd mqueue ptmx random stderr stdout urandom core full null pts shm stdin tty zero [root@localhost docker]# docker run -it my_test ls /home a.txt
例2:當(dāng)一個(gè)Dockfile文件中有兩個(gè)或多個(gè)CMD指令時(shí),只執(zhí)行最后一條CMD指令
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <[email protected]> RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] CMD ls /dev CMD ["ls","/usr","/usr/local"] [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46 Step 3/4 : CMD ls /dev ---> Using cache ---> 1e1971815006 Step 4/4 : CMD ["ls","/usr","/usr/local"] ---> Running in ccfabb87efc5 ^[[ARemoving intermediate container ccfabb87efc5 ---> e439603840ef Successfully built e439603840ef Successfully 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
5、ENTRYPOINT(設(shè)置container啟動(dòng)時(shí)執(zhí)行的操作)
格式:
ENTRYPOINT command param1 param2 ENTRYPOINT ["executable","param1","param2"]
例1:ENTRYPOINT指令不會(huì)被docker run提供的參數(shù)覆蓋
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <[email protected]> RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] ENTRYPOINT ls /root /sys [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/3 : FROM centos:latest ---> 9f38484d220f Step 2/3 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46 Step 3/3 : ENTRYPOINT ls /root /sys ---> Running in 67be0ac881cb Removing intermediate container 67be0ac881cb ---> e51f45c181ac Successfully built e51f45c181ac Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test /root: anaconda-ks.cfg /sys: block class devices fs kernel power bus dev firmware hypervisor module [root@localhost docker]# docker run -it my_test ls /home /root: anaconda-ks.cfg /sys: block class devices fs kernel power bus dev firmware hypervisor module
例2:當(dāng)一個(gè)Dockerfile文件中有兩個(gè)或多個(gè)ENTRYPOINT指令時(shí),只執(zhí)行最后一條ENTRYPOINT指令
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <[email protected]> RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] ENTRYPOINT ls /root /sys ENTRYPOINT ["ls","/home"] [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46 Step 3/4 : ENTRYPOINT ls /root /sys ---> Using cache ---> e51f45c181ac Step 4/4 : ENTRYPOINT ["ls","/home"] ---> Running in 95c2491f9b3b Removing intermediate container 95c2491f9b3b ---> 6196eaff66d3 Successfully built 6196eaff66d3 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test a.txt
例3:當(dāng)一個(gè)Dockerfile文件中既有CMD指令又有ENTRYPOINT指令時(shí),CMD指令將被作為ENTRYPOINT指令的參數(shù)
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <[email protected]> RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys CMD ["-l"] ENTRYPOINT ["ls","/home"] [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : RUN touch /home/a.txt ---> Using cache ---> cfb30d89ad46 Step 3/4 : CMD ["-l"] ---> Using cache ---> 4c5cf79c279d Step 4/4 : ENTRYPOINT ["ls","/home"] ---> Using cache ---> 14d643d73d81 Successfully built 14d643d73d81 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test total 0 -rw-r--r-- 1 root root 0 Aug 29 17:09 a.txt
6、USER(設(shè)置container的用戶(hù),默認(rèn)是root用戶(hù),這個(gè)root不是那個(gè)root)
格式:
USER username/uid
例:
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <[email protected]> #RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys #CMD ["-l"] #ENTRYPOINT ["ls","/home"] RUN useradd admin #需要先創(chuàng)建一個(gè)用戶(hù),不然會(huì)提示用戶(hù)不存在 USER admin [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 2.048kB Step 1/3 : FROM centos:latest ---> 9f38484d220f Step 2/3 : RUN useradd admin ---> Running in a4847a39ab06 Removing intermediate container a4847a39ab06 ---> 3759bdc0589c Step 3/3 : USER admin ---> Running in 80e367ffe699 Removing intermediate container 80e367ffe699 ---> c95196915358 Successfully built c95196915358 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test /bin/bash [admin@f0f565e8da5a /]$
7、EXPOSE(暴露,指定容器需要映射到宿主機(jī)的端口)
格式:
EXPOSE port1 [port2 ...]
例:
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <[email protected]> #RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys #CMD ["-l"] #ENTRYPOINT ["ls","/home"] #RUN useradd admin #USER admin EXPOSE 80 [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 : EXPOSE 80 ---> Running in 75dda1ff5393 Removing intermediate container 75dda1ff5393 ---> f6759e9df32d Successfully built f6759e9df32d Successfully tagged my_test:latest [root@localhost docker]# docker run -dit -p 80 my_test /bin/bash #隨機(jī)映射 5b790387fd758115204ce0a1f7ed1daa17a983108004948cb26b81f8fa8dc1f6 #隨機(jī)映射(大P) [root@localhost docker]# docker run -dit -P my_test /bin/bash #映射到宿主機(jī)的80 7207f9ae8a2c2932df727854cecb6b16d6b09ffecd1411bf42d03ffb1c271803 [root@localhost docker]# docker run -dit -p 80:80 my_test /bin/bash 83c40826648c13d23c706db3474d1eb541261638e5c1415abaebfb7a37d758ca [root@localhost docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 83c40826648c my_test "/bin/bash" About a minute ago Up About a minute 0.0.0.0:80->80/tcp silly_snyder 7207f9ae8a2c my_test "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:32769->80/tcp youthful_kirch 5b790387fd75 my_test "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:32768->80/tcp focused_bose
8、ENV(用于設(shè)置環(huán)境變量)
格式:
ENV <key> <value>
例:
[root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <[email protected]> #RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys #CMD ["-l"] #ENTRYPOINT ["ls","/home"] #RUN useradd admin #USER admin #EXPOSE 80 ENV a Hello World!! [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 : ENV a Hello World!! ---> Running in af33c5e13754 Removing intermediate container af33c5e13754 ---> dd22ab298385 Successfully built dd22ab298385 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test /bin/bash [root@7dca23648303 /]# echo $a Hello World!!
9、ADD(將源文件復(fù)制到container的目標(biāo)文件)
格式:
ADD <src> <dest>
注:src可以是Dockerfile所在目錄的相對(duì)路徑,也可以是一個(gè)絕對(duì)路徑;
src為tar文件時(shí),自動(dòng)減壓到dest中;
src為一個(gè)web的url時(shí),會(huì)自動(dòng)下載到dest中;
寫(xiě)dest路徑時(shí)注意結(jié)尾的“/”,結(jié)尾有“/”和沒(méi)“/”是有區(qū)別的
例:
[root@localhost docker]# pwd /root/docker [root@localhost docker]# echo "Hello World!!" > test.txt [root@localhost docker]# wget http://Nginx.org/download/nginx-1.16.1.tar.gz [root@localhost docker]# ls Dockerfile nginx-1.16.1.tar.gz test.txt [root@localhost docker]# vim Dockerfile #FROM centos FROM centos:latest #MAINTAINER Xuyun Liu <[email protected]> #RUN touch /home/a.txt #RUN ["touch","/home/b.txt"] #CMD ls /dev #CMD ["ls","/usr","/usr/local"] #ENTRYPOINT ls /root /sys #CMD ["-l"] #ENTRYPOINT ["ls","/home"] #RUN useradd admin #USER admin #EXPOSE 80 #ENV a Hello World!! ADD test.txt /tmp ADD nginx-1.16.1.tar.gz /tmp ADD http://nginx.org/download/nginx-1.16.1.tar.gz /home [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 1.036MB Step 1/4 : FROM centos:latest ---> 9f38484d220f Step 2/4 : ADD test.txt /tmp ---> 7fa385db5eef Step 3/4 : ADD nginx-1.16.1.tar.gz /tmp ---> 24c9c0e0877d Step 4/4 : ADD http://nginx.org/download/nginx-1.16.1.tar.gz /home Downloading 1.033MB/1.033MB ---> b842677c7912 Successfully built b842677c7912 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls /tmp /home /home: nginx-1.16.1.tar.gz /tmp: ks-script-eC059Y nginx-1.16.1 test.txt yum.log #ks-script-eC059Y和yum.log是原有文件
10、COPY(復(fù)制本地主機(jī)的<src>到容器中的<dest>,與ADD指令相似)
格式:
COPY <src> <dest>
注:COPY指令和ADD指令功能和使用方式類(lèi)似,但COPY指令只是復(fù)制,不會(huì)下載和解壓
例:
[root@localhost docker]# echo "This is a test." > test.html [root@localhost docker]# ls Dockerfile nginx-1.16.1.tar.gz test.html test.txt [root@localhost docker]# vim Dockerfile FROM centos:latest COPY test.html / [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 1.037MB Step 1/2 : FROM centos:latest ---> 9f38484d220f Step 2/2 : COPY test.html / ---> e48a7c46ded6 Successfully built e48a7c46ded6 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls -l /test.html -rw-r--r-- 1 root root 16 Aug 29 18:30 /test.html
11、VOLUME(卷,指定掛載點(diǎn))
格式:
VOLUME ["<mountpoint>"]
12、WORKDIR(切換目錄,可以多次切換,相當(dāng)于cd命令)
格式:
WORKDIR Directory_url
例:
[root@localhost docker]# vim Dockerfile FROM centos:latest WORKDIR /tmp RUN mkdir test WORKDIR test RUN ["touch","a.txt","b.txt"] [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 1.037MB Step 1/5 : FROM centos:latest ---> 9f38484d220f Step 2/5 : WORKDIR /tmp Removing intermediate container 9258f95093db ---> 6b8f6f6c69f6 Step 3/5 : RUN mkdir test ---> Running in e44f8d897714 Removing intermediate container e44f8d897714 ---> 1dc77dd96431 Step 4/5 : WORKDIR test Removing intermediate container df09ed12198d ---> 94cb324c6118 Step 5/5 : RUN ["touch","a.txt","b.txt"] ---> Running in 63f6bdcdd475 Removing intermediate container 63f6bdcdd475 ---> 1ae6996b6573 Successfully built 1ae6996b6573 Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls -l /tmp/test total 0 -rw-r--r-- 1 root root 0 Aug 29 18:39 a.txt -rw-r--r-- 1 root root 0 Aug 29 18:39 b.txt
13、ONBUILD(在子鏡像中執(zhí)行)
格式:
ONBUILD INSTRUCTION
例:
[root@localhost docker]# vim Dockerfile [root@localhost docker]# docker build -t my_test . Sending build context to Docker daemon 1.038MB Step 1/2 : FROM centos:latest ---> 9f38484d220f Step 2/2 : ONBUILD RUN touch /home/abc.txt ---> Running in b7f920c860a3 Removing intermediate container b7f920c860a3 ---> 991234fb1d8e Successfully built 991234fb1d8e Successfully tagged my_test:latest [root@localhost docker]# docker run -it my_test ls -l /home total 0 #并沒(méi)有創(chuàng)建abc.txt文件 [root@localhost docker]# mkdir test #需要再創(chuàng)建一個(gè)子Dockerfile文件,所以創(chuàng)建一個(gè)test目錄來(lái)存放子Dockerfile文件,和父鏡像的Dockerfile隔離開(kāi)來(lái) [root@localhost docker]# cd test [root@localhost test]# pwd /root/docker/test [root@localhost test]# vim Dockerfile FROM my_test:latest #寫(xiě)父鏡像 [root@localhost test]# docker build -t mytest2 . Sending build context to Docker daemon 2.048kB Step 1/1 : FROM my_test:latest # Executing 1 build trigger ---> Running in b035d0948b03 Removing intermediate container b035d0948b03 ---> 21e0f69c2eed Successfully built 21e0f69c2eed Successfully tagged mytest2:latest [root@localhost test]# docker run -it mytest2 ls -l /home total 0 -rw-r--r-- 1 root root 0 Aug 29 18:58 abc.txt #此時(shí)會(huì)創(chuàng)建abc.txt文件
最后用兩張張圖解釋常用指令的意義(有一說(shuō)一圖是從網(wǎng)上ba的,可能有點(diǎn)模糊不知道你們能不能看清,概括的挺好)

