日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

一、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 centos
FROM centos:latest
[root@localhost docker]# docker build -t my_test . #注意有個“.”
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(維護者,用來指定創建者信息)

格式:

MAINTAINER <name>

例:(通過inspect驗證)

[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終端運行命令(/bin/sh、/bin/bash等)
RUN ['executable(可執行的))','param1(參數)','param2'...] #不通過shell執行,而是通過內建命令exec去執行

例:

[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"] #這里雙引號和單引號也是有講究的,單引號會報錯
[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(設置container啟動時執行的操作)

格式:

CMD command param1 param2
CMD ["executable","param1","param2"...]
CMD ["param1","param2"...] #當CMD和ENTRYPOINT配合使用時的寫法

例1:CMD指令會被docker run提供的參數覆蓋

[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:當一個Dockfile文件中有兩個或多個CMD指令時,只執行最后一條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(設置container啟動時執行的操作)

格式:

ENTRYPOINT command param1 param2
ENTRYPOINT ["executable","param1","param2"]

例1:ENTRYPOINT指令不會被docker run提供的參數覆蓋

[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:當一個Dockerfile文件中有兩個或多個ENTRYPOINT指令時,只執行最后一條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:當一個Dockerfile文件中既有CMD指令又有ENTRYPOINT指令時,CMD指令將被作為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
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(設置container的用戶,默認是root用戶,這個root不是那個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 #需要先創建一個用戶,不然會提示用戶不存在
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(暴露,指定容器需要映射到宿主機的端口)

格式:

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 #隨機映射
5b790387fd758115204ce0a1f7ed1daa17a983108004948cb26b81f8fa8dc1f6 #隨機映射(大P)
[root@localhost docker]# docker run -dit -P my_test /bin/bash #映射到宿主機的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(用于設置環境變量)

格式:

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(將源文件復制到container的目標文件)

格式:

ADD <src> <dest>

注:src可以是Dockerfile所在目錄的相對路徑,也可以是一個絕對路徑;

src為tar文件時,自動減壓到dest中;

src為一個web的url時,會自動下載到dest中;

寫dest路徑時注意結尾的“/”,結尾有“/”和沒“/”是有區別的

例:

[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(復制本地主機的<src>到容器中的<dest>,與ADD指令相似)

格式:

COPY <src> <dest>

注:COPY指令和ADD指令功能和使用方式類似,但COPY指令只是復制,不會下載和解壓

例:

[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(卷,指定掛載點)

格式:

VOLUME ["<mountpoint>"]

12、WORKDIR(切換目錄,可以多次切換,相當于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(在子鏡像中執行)

格式:

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 #并沒有創建abc.txt文件
[root@localhost docker]# mkdir test #需要再創建一個子Dockerfile文件,所以創建一個test目錄來存放子Dockerfile文件,和父鏡像的Dockerfile隔離開來
[root@localhost docker]# cd test
[root@localhost test]# pwd
/root/docker/test
[root@localhost test]# vim Dockerfile 
FROM my_test:latest #寫父鏡像
[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 #此時會創建abc.txt文件

最后用兩張張圖解釋常用指令的意義(有一說一圖是從網上ba的,可能有點模糊不知道你們能不能看清,概括的挺好)

Dockerfile構建鏡像

 


Dockerfile構建鏡像

 

分享到:
標簽:Dockerfile
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定