Docker 第七章 Dockerfile 詳解

 

[root@localhost Docker_iamge]# docker build --help

Usage:	docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --force-rm                Always remove intermediate containers
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build
                                (default "default")
      --no-cache                Do not use cache when building the image
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
      --rm                      Remove intermediate containers after a successful build (default true)
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
  -t, --tag list                Name and optionally a tag in the 'name:tag' format       #打標籤
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])
[root@localhost Docker_iamge]# 
[root@localhost Docker_iamge]# 

 

Dockerfile 格式編寫

[root@localhost Docker_iamge]# ls
Dockerfile  index.html
[root@localhost Docker_iamge]# cat Dockerfile
# Description :test image

FROM busybox:latest
#一個Dockerfile 必須用`FROM`指令啓動。該FROM指令指定您正在構建的基本映像
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines." 
#LABEL 標籤

COPY   index.html /www/html/index.html
#該指令從中複製新文件或目錄 ,並將它們添加到路徑中容器的文件系統中。 [root@localhost Docker_iamge]# COPY<src><dest>

  

 docker build 構建鏡像 

[root@localhost Docker_iamge]# docker build -t httpdtest:v1 .          
Sending build context to Docker daemon  15.87kB
Step 1/6 : FROM busybox:latest
 ---> 64f5d945efcc
Step 2/6 : LABEL "com.example.vendor"="ACME Incorporated"
 ---> Running in cfdd36a69097
Removing intermediate container cfdd36a69097
 ---> 6bbc54c2f4e4
Step 3/6 : LABEL com.example.label-with-value="foo"
 ---> Running in 3ae90f68ae1c
Removing intermediate container 3ae90f68ae1c
 ---> 92b662f0bfd9
Step 4/6 : LABEL version="1.0"
 ---> Running in 36194eff5eb8
Removing intermediate container 36194eff5eb8
 ---> 9e3a4c064dc7
Step 5/6 : LABEL description="This text illustrates that label-values can span multiple lines."
 ---> Running in cdfcef379b7a
Removing intermediate container cdfcef379b7a
 ---> 05ed662e3088
Step 6/6 : COPY   index.html /www/html/index.html
 ---> 6060a9cb9597
Successfully built 6060a9cb9597
Successfully tagged httpdtest:v1
[root@localhost Docker_iamge]# 
[root@localhost Docker_iamge]# docker image ls
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
httpdtest                          v1                  6060a9cb9597        5 minutes ago       1.2MB
test                               v2.0                51f46a949468        8 days ago          1.2MB
test                               v1.0                648f9e13a37c        9 days ago          1.2MB
zy/busybox                         v0.1                4c9fa4db9491        9 days ago          1.2MB
dockerpracticecn/docker_practice   latest              b6bfd54275de        4 weeks ago         41.8MB
busybox                            latest              64f5d945efcc        4 weeks ago         1.2MB
nginx                              latest              53f3fd8007f7        5 weeks ago         109MB
[root@localhost Docker_iamge]# 

 

 [root@localhost Docker_iamge]# docker run --name q2 --rm httpdtest:v3 cat /www/html/index.html #檢查是否包含添加的內容
  <h1> test copy line </h1>
  [root@localhost Docker_iamge]#

  

ADD 指令html

ADD有兩種形式:

ADD [--chown=<user>:<group>] <src>... <dest>
ADD [--chown=<user>:<group>] ["<src>",... "<dest>"] (包含空格的路徑須要此表單)
注意:該--chown功能僅在用於構建Linux容器的Dockerfiles上受支持,而且不適用於Windows容器。因爲用戶和組全部權概念不能在Linux和Windows之間進行轉換,所以使用/etc/passwd和/etc/group將用戶名和組名轉換爲ID會限制此功能僅適用於基於Linux OS的容器。

該ADD指令從中複製新文件,目錄或遠程文件URL <src> ,並將它們添加到路徑上圖像的文件系統中<dest>。

<src>能夠指定多
[root@localhost Docker_iamge]# cat Dockerfile 
# Description :test image

FROM busybox:latest
#一個Dockerfile 必須用`FROM`指令啓動。該FROM指令指定您正在構建的基本映像
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates that label-values can span multiple lines." 
#LABEL 標籤

COPY   index.html /www/html/index.html
#COPY   /etc/sysconfig/   /test/sysconfig/

ADD http://nginx.org/download/nginx-1.17.0.tar.gz /usr/local/nginx/
# [root@localhost Docker_iamge]#

  

WORKDIR   #指定默認的工做目錄,在後續不指定路徑時 WORKDIR 就是默認目錄
WORKDIR /path/to/workdir
該WORKDIR指令集的工做目錄對任何RUN,CMD, ENTRYPOINT,COPY和ADD它後面的說明Dockerfile。若是WORKDIR不存在,即便它未在任何後續Dockerfile指令中使用,也將建立它。

該WORKDIR指令能夠在a中屢次使用Dockerfile。若是提供了相對路徑,則它將相對於前一條WORKDIR指令的路徑 。例如:

WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd

  

VOLUME
VOLUME ["/data"]
該VOLUME指令建立具備指定名稱的安裝點,並將其標記爲從本機主機或其餘容器保存外部安裝的卷。該值能夠是JSON數組,VOLUME ["/var/log/"]或具備多個參數的普通字符串,例如VOLUME /var/log或VOLUME /var/log /var/db。有關經過Docker客戶端提供的更多信息/示例和安裝說明,請參閱 經過卷共享目錄 文檔。
[root@localhost Docker_iamge]# cat Dockerfile 
# Description :test image

FROM busybox:latest
#一個Dockerfile 必須用`FROM`指令啓動。該FROM指令指定您正在構建的基本映像
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates that label-values can span multiple lines." 
#LABEL 標籤

COPY   index.html /www/html/index.html
#COPY   /etc/sysconfig/   /test/sysconfig/


#ADD http://nginx.org/download/nginx-1.17.0.tar.gz /usr/local/nginx/
RUN mkdir -p /test/mysql/
VOLUME /test/mysql
#VOLUME 只能使用自動掛載的方法,在容器啓動後可以看到對應的掛載文件,不然 inspect image 只能看到掛載點,看不到主機指定源目錄
#指定Docker 的自動掛載 [root@localhost Docker_iamge]#

ENVmysql

ENV
ENV <key> <value>
ENV <key>=<value> ...
該ENV指令將環境變量<key>設置爲該值 <value>。此值將在構建階段中的全部後續指令的環境中,而且也能夠在許多內聯替換。

該ENV指令有兩種形式。第一種形式,ENV <key> <value>將單個變量設置爲一個值。第一個空格後的整個字符串將被視爲<value>- 包括空格字符。該值將針對其餘環境變量進行解釋,所以若是未對其進行轉義,則將刪除引號字符。

第二種形式ENV <key>=<value> ...容許一次設置多個變量。請注意,第二種形式在語法中使用等號(=),而第一種形式則否則。與命令行解析同樣,引號和反斜槓可用於在值內包含空格。

例如:

ENV myName="John Doe" myDog=Rex\ The\ Dog \
    myCat=fluffy
和

ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat fluffy

  

-e   : 運行鏡像時定義環境變量
[root@localhost Docker_iamge]# docker run -it --name aqd --rm -e tttt="ffffff" httptest:v8.0 printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=892d0430a927 TERM=xterm tttt=ffffff DOC_ROOT=/src/data/test1/ HOME=/root [root@localhost Docker_iamge]# [root@localhost Docker_iamge]#

  

ARG
ARG <name>[=<default value>]
該ARG指令定義了一個變量,用戶能夠docker build使用該--build-arg <varname>=<value> 標誌在構建時將該變量傳遞給構建器。若是用戶指定了未在Dockerfile中定義的構建參數,則構建會輸出警告。

[Warning] One or more build-args [foo] were not consumed.
Dockerfile能夠包括一個或多個ARG指令。例如,如下是有效的Dockerfile:

FROM busybox
ARG user1
ARG buildno
...
警告:建議不要使用構建時變量來傳遞github密鑰,用戶憑據等祕密docker history。使用該命令,構建時變量值對於映像的任何用戶都是可見的。

默認值
的ARG指令能夠可選地包括一個默認值:

FROM busybox
ARG user1=someuser
ARG buildno=1
...
若是ARG指令具備默認值,而且在構建時沒有傳遞值,則構建器將使用默認值。

範圍
相關文章
相關標籤/搜索