1. 程式人生 > 其它 >Docker構建映象

Docker構建映象

一、概念

1、基於容器生成映象

通過 docker commit 命令將現有的容器提交來生成新的映象。
原理:容器啟動後的修改都儲存在可寫層,通過對可寫層的修改生成新的映象。

[root@hqs docker-hello]# docker commit --help
Usage:  docker commit [OPTIONS選項] CONTAINER容器 [REPOSITORY倉庫名[:TAG標籤]]
Create a new image from a containers changes
Options:
  -a, --author string    Author (e.g., "John Hannibal Smith <[email protected]>")    # 指定作者
  -c, --change list      Apply Dockerfile instruction to the created image             # 允許使用dockerfile的指令
  -m, --message string   Commit message                                  # 提交資訊
  -p, --pause            Pause container during commit (default true)    # 建立映象過程中容器暫停(掛起)

基於容器生成映象無法重複、構建缺乏透明性和體積偏大的問題,因此不推薦使用這種方式構建映象。

2、Dockerfile 構建映象

Dockerfile是由一系列指令和引數構成的指令碼每一條指令構建一層,因此每一條指令的內容就是描述該層應當如何構建,一個Dockerfile包含了構建映象的完整指令。

Dockerfile就是一個指令碼來構建和定製映象,把每一層的修改、安裝、構建、操作都寫入指令碼。以此來解決體積、映象構建透明等問題。

Dockerfile是一個文字檔案,包含一條條指令(Instruction),每一條指令構建一層,每一條指令的內容,就是描述該層應當如何構建。

3、docker build命令

基於 dockerfile 構建映象使用 docker build 命令。命令是通過 Dockerfile檔案構建上下文(Build Context) 來構建映象的。
注意:

  1. 不要把多餘的檔案放到構建上下文中————一般就要建立一個空目錄作為構建上下文
  2. 一般將 Dockerfile直接命名為 Dockerfile,並放在上下文根目錄,否則構建找不到(可以用-f指定)
  3. Dockerfile的每個指令都被獨立執行並建立一個新映象。
# 語法
[root@localhost docker]# 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 CFS週期
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota          # 限制 CPU CFS配額
  -c, --cpu-shares int          CPU shares (relative weight)                                 # 設定cpu使用權重
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)                  # 指定使用的CPU id
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)                  # 指定使用的記憶體 id
      --disable-content-trust   Skip image verification (default true)                       # 忽略校驗,預設開啟
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')        # 設定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     # 設定Swap的最大值為記憶體+swap,"-1"表示不限swap
      --network string          Set the networking mode for the RUN instructions during build (default "default")    # 在構建期間設定RUN指令網路模式
      --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      # 安靜模式,成功後只打印映象ID
      --rm                      Remove intermediate containers after a successful build (default true)      # 構建成功後刪除中間容器
      --security-opt strings    Security options                                             # 安全選項
      --shm-size bytes          Size of /dev/shm                                             # /dev/shm大小
  -t, --tag list                Name and optionally a tag in the 'name:tag' format           # 映象的名字及標籤
      --target string           Set the target build stage to build.                         # 目標構建階段設為build
      --ulimit ulimit           Ulimit options (default [])                                  # ?

# 最簡案例
docker build .

4、Dockerfile格式

指令:

  1. 指令不區分大小寫,建議大寫。
  2. 指令可以指定若干引數。
  3. Docker按順序執行指令。
  4. Dockerfile檔案必須以 FROM 指令開頭。

註釋:

  1. # 號開頭的行都將被視為註釋。解析器指令除外。
  2. 行中其他位置的 # 視為引數的一部分(不視為註釋)。

解析器指令:

  1. 不新增映象,也不會出現在構建步驟。
  2. 語法:# 指令 = 值,e.g:escape=\
  3. 一旦註釋、空行、解析器指令被處理,不再搜尋解析器指令,全格式化為註釋。————》解析器指令必須在Dockerfile的首部。

5、.dockerignore檔案

可以新增.dockerignore 檔案到構建上下文中來定義要排除的檔案和目錄。
用途:構建映象時,在將構建上下文傳給docker daemon程序時,命令列介面就修改了上下文以排除匹配的檔案或目錄。有助於避免傳送大型檔案或敏感檔案。

使用要點:

  1. 解釋為換行符分隔的模式列表(一行一行讀取)