- Usage: docker build [OPTIONS] PATH | URL | -
-
- Build an image from a Dockerfile
-
- Options:
- --build-arg value Set build-time variables (default [])
- --cgroup-parent string Optional parent cgroup for the container
- --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 value Set metadata for an image (default [])
- -m, --memory string Memory limit
- --memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
- --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)
- --shm-size string Size of /dev/shm, default value is 64MB.
- The format is `<number><unit>`. `number` must be greater than `0`.
- Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes),
- or `g` (gigabytes). If you omit the unit, the system uses bytes.
- -t, --tag value Name and optionally a tag in the 'name:tag' format (default [])
- --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目录作为上下文。
- $ 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。
- $ 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:
- $ docker build - < Dockerfile
Windows使用Powershell,运行:
- 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构建
- $ docker build .
-
- Uploading context 10240 bytes
- Step 1 : FROM busybox
- Pulling repository busybox
- ---> e9aa60c60128MB/2.284 MB (100%) endpoint: https://cdn-registry-1.docker.io/v1/
- Step 2 : RUN ls -lh /
- ---> Running in 9c9e81692ae9
- total 24
- drwxr-xr-x 2 root root 4.0K Mar 12 2013 bin
- drwxr-xr-x 5 root root 4.0K Oct 19 00:19 dev
- drwxr-xr-x 2 root root 4.0K Oct 19 00:19 etc
- drwxr-xr-x 2 root root 4.0K Nov 15 23:34 lib
- lrwxrwxrwx 1 root root 3 Mar 12 2013 lib64 -> lib
- dr-xr-xr-x 116 root root 0 Nov 15 23:34 proc
- lrwxrwxrwx 1 root root 3 Mar 12 2013 sbin -> bin
- dr-xr-xr-x 13 root root 0 Nov 15 23:34 sys
- drwxr-xr-x 2 root root 4.0K Mar 12 2013 tmp
- drwxr-xr-x 2 root root 4.0K Nov 15 23:34 usr
- ---> b35f4035db3f
- Step 3 : CMD echo Hello world
- ---> Running in 02071fceb21b
- ---> f52f38b7823e
- Successfully built f52f38b7823e
- Removing intermediate container 9c9e81692ae9
- Removing intermediate container 02071fceb21b
此示例指定PATH为.,所以将tar打包当前目录的所有文件并发送到Docker daemon。PATH用来指定构建上下文的位置。记住daemon可能运行在远程机器,在客户端侧不会解析Dockerfile(当执行docker build时)。意味着发送所有在PATH位置的文件,而不只是在Dockerfile的ADD指令中指定的文件。
使用URL构建
- $ docker build github.com/creack/docker-firefox
这将克隆GitHub的仓库并使用它作为上下文。仓库根目录的名为Dockerfile的文件作为用来构建镜像的Dockerfile。可以使用git://或git@scheme指定任意的git仓库。
- $ docker build -f ctx/Dockerfile http://server/ctx.tar.gz
-
- Downloading context: http://server/ctx.tar.gz [===================>] 240 B/240 B
- Step 1 : FROM busybox
- ---> 8c2e06607696
- Step 2 : ADD ctx/container.cfg /
- ---> e7829950cee3
- Removing intermediate container b35224abf821
- Step 3 : CMD /bin/ls
- ---> Running in fbc63d321d73
- ---> 3286931702ad
- Removing intermediate container fbc63d321d73
- 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 /能正常工作。
使用-构建
- $ docker build - < Dockerfile
这将从STDIN读取一个Dockerfile,没有提供上下文。由于缺少上下文,没有本地目录发送到docker daemon。因此没有上下文,所以Dockerfile ADD指令只能引用一个远程的URL。
- $ docker build - < context.tar.gz
这将从STDIN读取一个压缩文件作为上下文构建镜像。支持的格式有bzip2, gzip和xz.
Tag镜像(-t)
- $ 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,使用如下命令:
- $ docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .