docker build


命令:docker build

[root@iZ943kh74qgZ ~]# 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 stringSlice     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
      --help                       Print usage
      --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 stringSlice   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 [])

使用這個命令來搭配Dockerfile 創建一個image

 PATH or URL  在這2項中的文件被當作資源上下文. 創建image過程中,所有的文件都可能會被標記,比如說執行 ADD (link is external) 項. 當一個Dockerfile只有 URL STDIN (docker build - < Dockerfile), 那么就沒有上下文了.

如果 URL 中指定了一個git repo,那么這個git repo也會被使用。 這個git repo會被當作子目錄 (git clone -recursive). A fresh git clone occurs in a temporary directory on your local host, and then this is sent to the Docker daemon as the context. 反正如果使用git你必須處理好git的憑證和假如需要的VPN設置.

 .dockerignore 在 PATH 項的根目錄,這提供一種指定忽略的方式. 符合忽略規則的文件或目錄將被忽略。

 

從官網文檔來看https://docs.docker.com/engine/reference/builder/#usage

首先建議一個新的目錄,這個文件下面是你需要創建這個鏡像時所用到的文件,還有一個叫“Dockerfile”的文件,而這個文件就是我們構建鏡像文檔文。我們要寫指令就是放在這個文件里。

我們可以用-f 參數來指定其它地方的dockerfile

$ docker build -f /path/to/a/Dockerfile .

有一個比較搞笑的插曲,當我學習的時候,一直在糾結為什么docker build 不成功,一直報錯。原來我錯誤的認為,這個Dockerfile是一個文件夾,里面放指令文件。還折騰了好幾個小時,不斷各種百度。

我們可以使用-t來指定repository 和tag

$ docker build -t shykes/myapp .

 

其它 docker build 的用法

直接用 Git repo 進行構建

或許你已經注意到了,docker build 還支持從 URL 構建,比如可以直接從 Git repo 中構建:

$ docker build https://github.com/twang2218/gitlab-ce-zh.git#:8.14
docker build https://github.com/twang2218/gitlab-ce-zh.git\#:8.14
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM gitlab/gitlab-ce:8.14.0-ce.0
8.14.0-ce.0: Pulling from gitlab/gitlab-ce
aed15891ba52: Already exists
773ae8583d14: Already exists
...

這行命令指定了構建所需的 Git repo,並且指定默認的 master 分支,構建目錄為 /8.14/,然后 Docker 就會自己去 git clone 這個項目、切換到指定分支、並進入到指定目錄后開始構建。

用給定的 tar 壓縮包構建

$ docker build http://server/context.tar.gz

如果所給出的 URL 不是個 Git repo,而是個 tar 壓縮包,那么 Docker 引擎會下載這個包,並自動解壓縮,以其作為上下文,開始構建。

從標准輸入中讀取 Dockerfile 進行構建

docker build - < Dockerfile
或
cat Dockerfile | docker build -

如果標准輸入傳入的是文本文件,則將其視為 Dockerfile,並開始構建。這種形式由於直接從標准輸入中讀取 Dockerfile 的內容,它沒有上下文,因此不可以像其他方法那樣可以將本地文件 COPY 進鏡像之類的事情。

從標准輸入中讀取上下文壓縮包進行構建

$ docker build - < context.tar.gz

如果發現標准輸入的文件格式是 gzip、bzip2 以及 xz 的話,將會使其為上下文壓縮包,直接將其展開,將里面視為上下文,並開始構建。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM