Docker命令行参考(5) – docker build从Dockerfile构建镜像

  1. Usage:  docker build [OPTIONS] PATH | URL | -
  2.  
  3. Build an image from a Dockerfile
  4.  
  5. Options:
  6.       --build-arg value         Set build-time variables (default [])
  7.       --cgroup-parent string    Optional parent cgroup for the container
  8.       --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
  9.       --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  10.   -c, --cpu-shares int          CPU shares (relative weight)
  11.       --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
  12.       --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
  13.       --disable-content-trust   Skip image verification (default true)
  14.   -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
  15.       --force-rm                Always remove intermediate containers
  16.       --help                    Print usage
  17.       --isolation string        Container isolation technology
  18.       --label value             Set metadata for an image (default [])
  19.   -m, --memory string           Memory limit
  20.       --memory-swap string      Swap limit equal to memory plus swap: '-1' to enable unlimited swap
  21.       --no-cache                Do not use cache when building the image
  22.       --pull                    Always attempt to pull a newer version of the image
  23.   -q, --quiet                   Suppress the build output and print image ID on success
  24.       --rm                      Remove intermediate containers after a successful build (default true)
  25.       --shm-size string         Size of /dev/shm, default value is 64MB.
  26.                                 The format is `<number><unit>`. `number` must be greater than `0`.
  27.                                 Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes),
  28.                                 or `g` (gigabytes). If you omit the unit, the system uses bytes.
  29.   -t, --tag value               Name and optionally a tag in the 'name:tag' format (default [])
  30.       --ulimit value            Ulimit options (default [])

上下文

Docker从Dockerfile和“上下文”构建镜像。一个构建上下文是位于特定的PATH或URL的文件。构建过程可以引用上下文中的任何文件。例如,可以使用ADD指定引用上下文中的一个文件。
URL参数可以引用三种类型的资源:Git仓库,预打包的tarball上下文和纯文本文件。

Git仓库

当URL参数指向Git仓库的位置时,这个仓库就作为此次的构建上下文。系统使用git clone –depth 1 –recursive命令递归克隆这个仓库和它的子模块。此命令运行在本地主机的临时目录下。当命令执行成功后,这个目录作为上下文发送给Docker daemon。
在Git URL上可以配置上下文,使用冒号分隔。第一部分指定了将要下载的git仓库以及分支,tag或提交的SHA。第二部分指定一个子目录作为构建上下文。
例如,下面的命令使用了container分支的docker目录作为上下文。

  1. $ docker build https://github.com/docker/rootfs.git#container:docker

下面是Git URL可能的所有有效的方式:

构建语法后缀 使用的提交 使用的上下文
myrepo.git refs/heads/master /
myrepo.git#mytag refs/tags/mytag /
myrepo.git#mybranch refs/heads/mybranch /
myrepo.git#abcdef sha1 = abcdef /
myrepo.git#:myfolder refs/heads/master /myfolder
myrepo.git#master:myfolder refs/heads/master /myfolder
myrepo.git#mytag:myfolder refs/tags/mytag /myfolder
myrepo.git#mybranch:myfolder refs/heads/mybranch /myfolder
myrepo.git#abcdef:myfolder sha1 = abcdef /myfolder

Tarball上下文

如果指定一个远程的tarball文件URL,这个URL将发送给daemon。

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

daemon将在其所运行的主机完成这个URL的下载。docker daemon下载这个context.tar.gz并使用它作为构建上下文。Tarball必须是以标准的tar Unix格式打包并使用‘xz’, ‘bzip2’, ‘gzip’或可识别的格式中的其中一种压缩。

文本文件

不指定上下文的话,可以在URL中传递单个Dockerfile或通过STDIN管道传输文件。要从STDIN管道传输一个Dockerfile:

  1. $ docker build - < Dockerfile

Windows使用Powershell,运行:

  1. Get-Content Dockerfile | docker build -

如果使用STDIN或指定一个URL指向纯文本文件,系统把内容放置到称为Dockerfile的文件,其中的-f,–file选项将忽略。在这种情况下没有上下文。
默认下docker build命令在构建上下文的根目录查找Dockerfile文件。使用-f,–file选项可以指定一个其它的文件。这个当同一组文件用于多个构建时会有帮助。path必须是构建上下文内的一个文件。如果指定一个相对路径,那么这个路径就是相对于上下文根目录的。
在大多数情况下,最好把每个Dockerfile放置到一个空的目录。然后只添加Dockerfile中需要用到的文件。为了提高构建性能,可以使用.dockerignore来排除用不到的文件。
如果Docker客户端与daemon断开了连接,构建就取消了。这种情况发生在使用CTRL-c中断Docker客户端或Docker客户端被kill。

示例

使用PATH构建

  1. $ docker build .
  2.  
  3. Uploading context 10240 bytes
  4. Step 1 : FROM busybox
  5. Pulling repository busybox
  6.  ---> e9aa60c60128MB/2.284 MB (100%) endpoint: https://cdn-registry-1.docker.io/v1/
  7. Step 2 : RUN ls -lh /
  8.  ---> Running in 9c9e81692ae9
  9. total 24
  10. drwxr-xr-x    2 root     root        4.0K Mar 12  2013 bin
  11. drwxr-xr-x    5 root     root        4.0K Oct 19 00:19 dev
  12. drwxr-xr-x    2 root     root        4.0K Oct 19 00:19 etc
  13. drwxr-xr-x    2 root     root        4.0K Nov 15 23:34 lib
  14. lrwxrwxrwx    1 root     root           3 Mar 12  2013 lib64 -> lib
  15. dr-xr-xr-x  116 root     root           0 Nov 15 23:34 proc
  16. lrwxrwxrwx    1 root     root           3 Mar 12  2013 sbin -> bin
  17. dr-xr-xr-x   13 root     root           0 Nov 15 23:34 sys
  18. drwxr-xr-x    2 root     root        4.0K Mar 12  2013 tmp
  19. drwxr-xr-x    2 root     root        4.0K Nov 15 23:34 usr
  20.  ---> b35f4035db3f
  21. Step 3 : CMD echo Hello world
  22.  ---> Running in 02071fceb21b
  23.  ---> f52f38b7823e
  24. Successfully built f52f38b7823e
  25. Removing intermediate container 9c9e81692ae9
  26. Removing intermediate container 02071fceb21b

此示例指定PATH为.,所以将tar打包当前目录的所有文件并发送到Docker daemon。PATH用来指定构建上下文的位置。记住daemon可能运行在远程机器,在客户端侧不会解析Dockerfile(当执行docker build时)。意味着发送所有在PATH位置的文件,而不只是在Dockerfile的ADD指令中指定的文件。

使用URL构建

  1. $ docker build github.com/creack/docker-firefox

这将克隆GitHub的仓库并使用它作为上下文。仓库根目录的名为Dockerfile的文件作为用来构建镜像的Dockerfile。可以使用git://或git@scheme指定任意的git仓库。

  1. $ docker build -f ctx/Dockerfile http://server/ctx.tar.gz
  2.  
  3. Downloading context: http://server/ctx.tar.gz [===================>]    240 B/240 B
  4. Step 1 : FROM busybox
  5.  ---> 8c2e06607696
  6. Step 2 : ADD ctx/container.cfg /
  7.  ---> e7829950cee3
  8. Removing intermediate container b35224abf821
  9. Step 3 : CMD /bin/ls
  10.  ---> Running in fbc63d321d73
  11.  ---> 3286931702ad
  12. Removing intermediate container fbc63d321d73
  13. Successfully built 377c409b35e4

这个发送了http://server/ctx.tar.gz到docker daemon,然后daemon下载并解压这个tarball。-f ctx/Dockerfile参数指定在ctx.tar.gz的用于构建镜像的Dockerfile。在这个Dockerfile中的任何ADD命令引用本地路径的必须是相对于ctx.tar.gz内的根目录。在上面的示例中,tarball包含了一个目录ctx/,所以ADD ctx/container.cfg /能正常工作。

使用-构建

  1. $ docker build - < Dockerfile

这将从STDIN读取一个Dockerfile,没有提供上下文。由于缺少上下文,没有本地目录发送到docker daemon。因此没有上下文,所以Dockerfile ADD指令只能引用一个远程的URL。

  1. $ docker build - < context.tar.gz

这将从STDIN读取一个压缩文件作为上下文构建镜像。支持的格式有bzip2, gzip和xz.

Tag镜像(-t)

  1. $ docker build -t vieux/apache:2.0 .

这个构建出来的镜像名称为vieux/apache,tag为2.0。
可以应用多个tag到一个镜像。例如,可以应用latest tag到一个新建的镜像,再添加另一个tag来关联一个特定的版本。例如tag一个镜像为whenry/fedora-jboss:latest和whenry/fedora-jboss:v2.1,使用如下命令:

  1. $ docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .
标签:Docker 发布于:2019-11-19 15:47:21