命令: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 的话,将会使其为上下文压缩包,直接将其展开,将里面视为上下文,并开始构建。