github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/docs/reference/commandline/build.md (about) 1 --- 2 title: "build" 3 description: "The build command description and usage" 4 keywords: "build, docker, image" 5 --- 6 7 <!-- This file is maintained within the docker/cli GitHub 8 repository at https://github.com/docker/cli/. Make all 9 pull requests against that repo. If you see this file in 10 another repository, consider it read-only there, as it will 11 periodically be overwritten by the definitive file. Pull 12 requests which include edits to this file in other repositories 13 will be rejected. 14 --> 15 16 # build 17 18 ```markdown 19 Usage: docker build [OPTIONS] PATH | URL | - 20 21 Build an image from a Dockerfile 22 23 Options: 24 --add-host value Add a custom host-to-IP mapping (host:ip) (default []) 25 --build-arg value Set build-time variables (default []) 26 --cache-from value Images to consider as cache sources (default []) 27 --cgroup-parent string Optional parent cgroup for the container 28 --compress Compress the build context using gzip 29 --cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period 30 --cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota 31 -c, --cpu-shares int CPU shares (relative weight) 32 --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) 33 --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) 34 --disable-content-trust Skip image verification (default true) 35 -f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile') 36 --force-rm Always remove intermediate containers 37 --help Print usage 38 --iidfile string Write the image ID to the file 39 --isolation string Container isolation technology 40 --label value Set metadata for an image (default []) 41 -m, --memory string Memory limit 42 --memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap 43 --network string Set the networking mode for the RUN instructions during build 44 'bridge': use default Docker bridge 45 'none': no networking 46 'container:<name|id>': reuse another container's network stack 47 'host': use the Docker host network stack 48 '<network-name>|<network-id>': connect to a user-defined network 49 --no-cache Do not use cache when building the image 50 --pull Always attempt to pull a newer version of the image 51 --progress Set type of progress output (only if BuildKit enabled) (auto, plain, tty). 52 Use plain to show container output 53 -q, --quiet Suppress the build output and print image ID on success 54 --rm Remove intermediate containers after a successful build (default true) 55 --secret Secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret" 56 --security-opt value Security Options (default []) 57 --shm-size bytes Size of /dev/shm 58 The format is `<number><unit>`. `number` must be greater than `0`. 59 Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes), 60 or `g` (gigabytes). If you omit the unit, the system uses bytes. 61 --squash Squash newly built layers into a single new layer (**Experimental Only**) 62 -t, --tag value Name and optionally a tag in the 'name:tag' format (default []) 63 --target string Set the target build stage to build. 64 --ulimit value Ulimit options (default []) 65 ``` 66 67 ## Description 68 69 The `docker build` command builds Docker images from a Dockerfile and a 70 "context". A build's context is the set of files located in the specified 71 `PATH` or `URL`. The build process can refer to any of the files in the 72 context. For example, your build can use a [*COPY*](../builder.md#copy) 73 instruction to reference a file in the context. 74 75 The `URL` parameter can refer to three kinds of resources: Git repositories, 76 pre-packaged tarball contexts and plain text files. 77 78 ### Git repositories 79 80 When the `URL` parameter points to the location of a Git repository, the 81 repository acts as the build context. The system recursively fetches the 82 repository and its submodules. The commit history is not preserved. A 83 repository is first pulled into a temporary directory on your local host. After 84 that succeeds, the directory is sent to the Docker daemon as the context. 85 Local copy gives you the ability to access private repositories using local 86 user credentials, VPN's, and so forth. 87 88 > **Note:** 89 > If the `URL` parameter contains a fragment the system will recursively clone 90 > the repository and its submodules using a `git clone --recursive` command. 91 92 Git URLs accept context configuration in their fragment section, separated by a 93 colon `:`. The first part represents the reference that Git will check out, 94 and can be either a branch, a tag, or a remote reference. The second part 95 represents a subdirectory inside the repository that will be used as a build 96 context. 97 98 For example, run this command to use a directory called `docker` in the branch 99 `container`: 100 101 ```bash 102 $ docker build https://github.com/docker/rootfs.git#container:docker 103 ``` 104 105 The following table represents all the valid suffixes with their build 106 contexts: 107 108 Build Syntax Suffix | Commit Used | Build Context Used 109 --------------------------------|-----------------------|------------------- 110 `myrepo.git` | `refs/heads/master` | `/` 111 `myrepo.git#mytag` | `refs/tags/mytag` | `/` 112 `myrepo.git#mybranch` | `refs/heads/mybranch` | `/` 113 `myrepo.git#pull/42/head` | `refs/pull/42/head` | `/` 114 `myrepo.git#:myfolder` | `refs/heads/master` | `/myfolder` 115 `myrepo.git#master:myfolder` | `refs/heads/master` | `/myfolder` 116 `myrepo.git#mytag:myfolder` | `refs/tags/mytag` | `/myfolder` 117 `myrepo.git#mybranch:myfolder` | `refs/heads/mybranch` | `/myfolder` 118 119 120 ### Tarball contexts 121 122 If you pass an URL to a remote tarball, the URL itself is sent to the daemon: 123 124 ```bash 125 $ docker build http://server/context.tar.gz 126 ``` 127 128 The download operation will be performed on the host the Docker daemon is 129 running on, which is not necessarily the same host from which the build command 130 is being issued. The Docker daemon will fetch `context.tar.gz` and use it as the 131 build context. Tarball contexts must be tar archives conforming to the standard 132 `tar` UNIX format and can be compressed with any one of the 'xz', 'bzip2', 133 'gzip' or 'identity' (no compression) formats. 134 135 ### Text files 136 137 Instead of specifying a context, you can pass a single `Dockerfile` in the 138 `URL` or pipe the file in via `STDIN`. To pipe a `Dockerfile` from `STDIN`: 139 140 ```bash 141 $ docker build - < Dockerfile 142 ``` 143 144 With Powershell on Windows, you can run: 145 146 ```powershell 147 Get-Content Dockerfile | docker build - 148 ``` 149 150 If you use `STDIN` or specify a `URL` pointing to a plain text file, the system 151 places the contents into a file called `Dockerfile`, and any `-f`, `--file` 152 option is ignored. In this scenario, there is no context. 153 154 By default the `docker build` command will look for a `Dockerfile` at the root 155 of the build context. The `-f`, `--file`, option lets you specify the path to 156 an alternative file to use instead. This is useful in cases where the same set 157 of files are used for multiple builds. The path must be to a file within the 158 build context. If a relative path is specified then it is interpreted as 159 relative to the root of the context. 160 161 In most cases, it's best to put each Dockerfile in an empty directory. Then, 162 add to that directory only the files needed for building the Dockerfile. To 163 increase the build's performance, you can exclude files and directories by 164 adding a `.dockerignore` file to that directory as well. For information on 165 creating one, see the [.dockerignore file](../builder.md#dockerignore-file). 166 167 If the Docker client loses connection to the daemon, the build is canceled. 168 This happens if you interrupt the Docker client with `CTRL-c` or if the Docker 169 client is killed for any reason. If the build initiated a pull which is still 170 running at the time the build is cancelled, the pull is cancelled as well. 171 172 ## Return code 173 174 On a successful build, a return code of success `0` will be returned. When the 175 build fails, a non-zero failure code will be returned. 176 177 There should be informational output of the reason for failure output to 178 `STDERR`: 179 180 ```bash 181 $ docker build -t fail . 182 183 Sending build context to Docker daemon 2.048 kB 184 Sending build context to Docker daemon 185 Step 1/3 : FROM busybox 186 ---> 4986bf8c1536 187 Step 2/3 : RUN exit 13 188 ---> Running in e26670ec7a0a 189 INFO[0000] The command [/bin/sh -c exit 13] returned a non-zero code: 13 190 $ echo $? 191 1 192 ``` 193 194 See also: 195 196 [*Dockerfile Reference*](../builder.md). 197 198 ## Examples 199 200 ### Build with PATH 201 202 ```bash 203 $ docker build . 204 205 Uploading context 10240 bytes 206 Step 1/3 : FROM busybox 207 Pulling repository busybox 208 ---> e9aa60c60128MB/2.284 MB (100%) endpoint: https://cdn-registry-1.docker.io/v1/ 209 Step 2/3 : RUN ls -lh / 210 ---> Running in 9c9e81692ae9 211 total 24 212 drwxr-xr-x 2 root root 4.0K Mar 12 2013 bin 213 drwxr-xr-x 5 root root 4.0K Oct 19 00:19 dev 214 drwxr-xr-x 2 root root 4.0K Oct 19 00:19 etc 215 drwxr-xr-x 2 root root 4.0K Nov 15 23:34 lib 216 lrwxrwxrwx 1 root root 3 Mar 12 2013 lib64 -> lib 217 dr-xr-xr-x 116 root root 0 Nov 15 23:34 proc 218 lrwxrwxrwx 1 root root 3 Mar 12 2013 sbin -> bin 219 dr-xr-xr-x 13 root root 0 Nov 15 23:34 sys 220 drwxr-xr-x 2 root root 4.0K Mar 12 2013 tmp 221 drwxr-xr-x 2 root root 4.0K Nov 15 23:34 usr 222 ---> b35f4035db3f 223 Step 3/3 : CMD echo Hello world 224 ---> Running in 02071fceb21b 225 ---> f52f38b7823e 226 Successfully built f52f38b7823e 227 Removing intermediate container 9c9e81692ae9 228 Removing intermediate container 02071fceb21b 229 ``` 230 231 This example specifies that the `PATH` is `.`, and so all the files in the 232 local directory get `tar`d and sent to the Docker daemon. The `PATH` specifies 233 where to find the files for the "context" of the build on the Docker daemon. 234 Remember that the daemon could be running on a remote machine and that no 235 parsing of the Dockerfile happens at the client side (where you're running 236 `docker build`). That means that *all* the files at `PATH` get sent, not just 237 the ones listed to [*ADD*](../builder.md#add) in the Dockerfile. 238 239 The transfer of context from the local machine to the Docker daemon is what the 240 `docker` client means when you see the "Sending build context" message. 241 242 If you wish to keep the intermediate containers after the build is complete, 243 you must use `--rm=false`. This does not affect the build cache. 244 245 ### Build with URL 246 247 ```bash 248 $ docker build github.com/creack/docker-firefox 249 ``` 250 251 This will clone the GitHub repository and use the cloned repository as context. 252 The Dockerfile at the root of the repository is used as Dockerfile. You can 253 specify an arbitrary Git repository by using the `git://` or `git@` scheme. 254 255 ```bash 256 $ docker build -f ctx/Dockerfile http://server/ctx.tar.gz 257 258 Downloading context: http://server/ctx.tar.gz [===================>] 240 B/240 B 259 Step 1/3 : FROM busybox 260 ---> 8c2e06607696 261 Step 2/3 : ADD ctx/container.cfg / 262 ---> e7829950cee3 263 Removing intermediate container b35224abf821 264 Step 3/3 : CMD /bin/ls 265 ---> Running in fbc63d321d73 266 ---> 3286931702ad 267 Removing intermediate container fbc63d321d73 268 Successfully built 377c409b35e4 269 ``` 270 271 This sends the URL `http://server/ctx.tar.gz` to the Docker daemon, which 272 downloads and extracts the referenced tarball. The `-f ctx/Dockerfile` 273 parameter specifies a path inside `ctx.tar.gz` to the `Dockerfile` that is used 274 to build the image. Any `ADD` commands in that `Dockerfile` that refers to local 275 paths must be relative to the root of the contents inside `ctx.tar.gz`. In the 276 example above, the tarball contains a directory `ctx/`, so the `ADD 277 ctx/container.cfg /` operation works as expected. 278 279 ### Build with - 280 281 ```bash 282 $ docker build - < Dockerfile 283 ``` 284 285 This will read a Dockerfile from `STDIN` without context. Due to the lack of a 286 context, no contents of any local directory will be sent to the Docker daemon. 287 Since there is no context, a Dockerfile `ADD` only works if it refers to a 288 remote URL. 289 290 ```bash 291 $ docker build - < context.tar.gz 292 ``` 293 294 This will build an image for a compressed context read from `STDIN`. Supported 295 formats are: bzip2, gzip and xz. 296 297 ### Use a .dockerignore file 298 299 ```bash 300 $ docker build . 301 302 Uploading context 18.829 MB 303 Uploading context 304 Step 1/2 : FROM busybox 305 ---> 769b9341d937 306 Step 2/2 : CMD echo Hello world 307 ---> Using cache 308 ---> 99cc1ad10469 309 Successfully built 99cc1ad10469 310 $ echo ".git" > .dockerignore 311 $ docker build . 312 Uploading context 6.76 MB 313 Uploading context 314 Step 1/2 : FROM busybox 315 ---> 769b9341d937 316 Step 2/2 : CMD echo Hello world 317 ---> Using cache 318 ---> 99cc1ad10469 319 Successfully built 99cc1ad10469 320 ``` 321 322 This example shows the use of the `.dockerignore` file to exclude the `.git` 323 directory from the context. Its effect can be seen in the changed size of the 324 uploaded context. The builder reference contains detailed information on 325 [creating a .dockerignore file](../builder.md#dockerignore-file) 326 327 ### Tag an image (-t) 328 329 ```bash 330 $ docker build -t vieux/apache:2.0 . 331 ``` 332 333 This will build like the previous example, but it will then tag the resulting 334 image. The repository name will be `vieux/apache` and the tag will be `2.0`. 335 [Read more about valid tags](tag.md). 336 337 You can apply multiple tags to an image. For example, you can apply the `latest` 338 tag to a newly built image and add another tag that references a specific 339 version. 340 For example, to tag an image both as `whenry/fedora-jboss:latest` and 341 `whenry/fedora-jboss:v2.1`, use the following: 342 343 ```bash 344 $ docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 . 345 ``` 346 347 ### Specify a Dockerfile (-f) 348 349 ```bash 350 $ docker build -f Dockerfile.debug . 351 ``` 352 353 This will use a file called `Dockerfile.debug` for the build instructions 354 instead of `Dockerfile`. 355 356 ```bash 357 $ curl example.com/remote/Dockerfile | docker build -f - . 358 ``` 359 360 The above command will use the current directory as the build context and read 361 a Dockerfile from stdin. 362 363 ```bash 364 $ docker build -f dockerfiles/Dockerfile.debug -t myapp_debug . 365 $ docker build -f dockerfiles/Dockerfile.prod -t myapp_prod . 366 ``` 367 368 The above commands will build the current build context (as specified by the 369 `.`) twice, once using a debug version of a `Dockerfile` and once using a 370 production version. 371 372 ```bash 373 $ cd /home/me/myapp/some/dir/really/deep 374 $ docker build -f /home/me/myapp/dockerfiles/debug /home/me/myapp 375 $ docker build -f ../../../../dockerfiles/debug /home/me/myapp 376 ``` 377 378 These two `docker build` commands do the exact same thing. They both use the 379 contents of the `debug` file instead of looking for a `Dockerfile` and will use 380 `/home/me/myapp` as the root of the build context. Note that `debug` is in the 381 directory structure of the build context, regardless of how you refer to it on 382 the command line. 383 384 > **Note:** 385 > `docker build` will return a `no such file or directory` error if the 386 > file or directory does not exist in the uploaded context. This may 387 > happen if there is no context, or if you specify a file that is 388 > elsewhere on the Host system. The context is limited to the current 389 > directory (and its children) for security reasons, and to ensure 390 > repeatable builds on remote Docker hosts. This is also the reason why 391 > `ADD ../file` will not work. 392 393 ### Use a custom parent cgroup (--cgroup-parent) 394 395 When `docker build` is run with the `--cgroup-parent` option the containers 396 used in the build will be run with the [corresponding `docker run` 397 flag](../run.md#specifying-custom-cgroups). 398 399 ### Set ulimits in container (--ulimit) 400 401 Using the `--ulimit` option with `docker build` will cause each build step's 402 container to be started using those [`--ulimit` 403 flag values](./run.md#set-ulimits-in-container-ulimit). 404 405 ### Set build-time variables (--build-arg) 406 407 You can use `ENV` instructions in a Dockerfile to define variable 408 values. These values persist in the built image. However, often 409 persistence is not what you want. Users want to specify variables differently 410 depending on which host they build an image on. 411 412 A good example is `http_proxy` or source versions for pulling intermediate 413 files. The `ARG` instruction lets Dockerfile authors define values that users 414 can set at build-time using the `--build-arg` flag: 415 416 ```bash 417 $ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 --build-arg FTP_PROXY=http://40.50.60.5:4567 . 418 ``` 419 420 This flag allows you to pass the build-time variables that are 421 accessed like regular environment variables in the `RUN` instruction of the 422 Dockerfile. Also, these values don't persist in the intermediate or final images 423 like `ENV` values do. You must add `--build-arg` for each build argument. 424 425 Using this flag will not alter the output you see when the `ARG` lines from the 426 Dockerfile are echoed during the build process. 427 428 For detailed information on using `ARG` and `ENV` instructions, see the 429 [Dockerfile reference](../builder.md). 430 431 You may also use the `--build-arg` flag without a value, in which case the value 432 from the local environment will be propagated into the Docker container being 433 built: 434 435 ```bash 436 $ export HTTP_PROXY=http://10.20.30.2:1234 437 $ docker build --build-arg HTTP_PROXY . 438 ``` 439 440 This is similar to how `docker run -e` works. Refer to the [`docker run` documentation](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file) 441 for more information. 442 443 ### Optional security options (--security-opt) 444 445 This flag is only supported on a daemon running on Windows, and only supports 446 the `credentialspec` option. The `credentialspec` must be in the format 447 `file://spec.txt` or `registry://keyname`. 448 449 ### Specify isolation technology for container (--isolation) 450 451 This option is useful in situations where you are running Docker containers on 452 Windows. The `--isolation=<value>` option sets a container's isolation 453 technology. On Linux, the only supported is the `default` option which uses 454 Linux namespaces. On Microsoft Windows, you can specify these values: 455 456 457 | Value | Description | 458 |-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------| 459 | `default` | Use the value specified by the Docker daemon's `--exec-opt` . If the `daemon` does not specify an isolation technology, Microsoft Windows uses `process` as its default value. | 460 | `process` | Namespace isolation only. | 461 | `hyperv` | Hyper-V hypervisor partition-based isolation. | 462 463 Specifying the `--isolation` flag without a value is the same as setting `--isolation="default"`. 464 465 ### Add entries to container hosts file (--add-host) 466 467 You can add other hosts into a container's `/etc/hosts` file by using one or 468 more `--add-host` flags. This example adds a static address for a host named 469 `docker`: 470 471 $ docker build --add-host=docker:10.180.0.1 . 472 473 ### Specifying target build stage (--target) 474 475 When building a Dockerfile with multiple build stages, `--target` can be used to 476 specify an intermediate build stage by name as a final stage for the resulting 477 image. Commands after the target stage will be skipped. 478 479 ```Dockerfile 480 FROM debian AS build-env 481 ... 482 483 FROM alpine AS production-env 484 ... 485 ``` 486 487 ```bash 488 $ docker build -t mybuildimage --target build-env . 489 ``` 490 491 ### Squash an image's layers (--squash) (experimental) 492 493 #### Overview 494 495 Once the image is built, squash the new layers into a new image with a single 496 new layer. Squashing does not destroy any existing image, rather it creates a new 497 image with the content of the squashed layers. This effectively makes it look 498 like all `Dockerfile` commands were created with a single layer. The build 499 cache is preserved with this method. 500 501 The `--squash` option is an experimental feature, and should not be considered 502 stable. 503 504 505 Squashing layers can be beneficial if your Dockerfile produces multiple layers 506 modifying the same files, for example, file that are created in one step, and 507 removed in another step. For other use-cases, squashing images may actually have 508 a negative impact on performance; when pulling an image consisting of multiple 509 layers, layers can be pulled in parallel, and allows sharing layers between 510 images (saving space). 511 512 For most use cases, multi-stage are a better alternative, as they give more 513 fine-grained control over your build, and can take advantage of future 514 optimizations in the builder. Refer to the [use multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/) 515 section in the userguide for more information. 516 517 518 #### Known limitations 519 520 The `--squash` option has a number of known limitations: 521 522 - When squashing layers, the resulting image cannot take advantage of layer 523 sharing with other images, and may use significantly more space. Sharing the 524 base image is still supported. 525 - When using this option you may see significantly more space used due to 526 storing two copies of the image, one for the build cache with all the cache 527 layers in tact, and one for the squashed version. 528 - While squashing layers may produce smaller images, it may have a negative 529 impact on performance, as a single layer takes longer to extract, and 530 downloading a single layer cannot be parallelized. 531 - When attempting to squash an image that does not make changes to the 532 filesystem (for example, the Dockerfile only contains `ENV` instructions), 533 the squash step will fail (see [issue #33823](https://github.com/moby/moby/issues/33823) 534 535 #### Prerequisites 536 537 The example on this page is using experimental mode in Docker 1.13. 538 539 Experimental mode can be enabled by using the `--experimental` flag when starting the Docker daemon or setting `experimental: true` in the `daemon.json` configuration file. 540 541 By default, experimental mode is disabled. To see the current configuration, use the `docker version` command. 542 543 ```none 544 Server: 545 Version: 1.13.1 546 API version: 1.26 (minimum version 1.12) 547 Go version: go1.7.5 548 Git commit: 092cba3 549 Built: Wed Feb 8 06:35:24 2017 550 OS/Arch: linux/amd64 551 Experimental: false 552 553 [...] 554 ``` 555 556 To enable experimental mode, users need to restart the docker daemon with the experimental flag enabled. 557 558 #### Enable Docker experimental 559 560 Experimental features are now included in the standard Docker binaries as of version 1.13.0. For enabling experimental features, you need to start the Docker daemon with `--experimental` flag. You can also enable the daemon flag via /etc/docker/daemon.json. e.g. 561 562 ```json 563 { 564 "experimental": true 565 } 566 ``` 567 568 Then make sure the experimental flag is enabled: 569 570 ```bash 571 $ docker version -f '{{.Server.Experimental}}' 572 true 573 ``` 574 575 #### Build an image with `--squash` argument 576 577 The following is an example of docker build with `--squash` argument 578 579 ```Dockerfile 580 FROM busybox 581 RUN echo hello > /hello 582 RUN echo world >> /hello 583 RUN touch remove_me /remove_me 584 ENV HELLO world 585 RUN rm /remove_me 586 ``` 587 588 An image named `test` is built with `--squash` argument. 589 590 ```bash 591 $ docker build --squash -t test . 592 593 [...] 594 ``` 595 596 If everything is right, the history will look like this: 597 598 ```bash 599 $ docker history test 600 601 IMAGE CREATED CREATED BY SIZE COMMENT 602 4e10cb5b4cac 3 seconds ago 12 B merge sha256:88a7b0112a41826885df0e7072698006ee8f621c6ab99fca7fe9151d7b599702 to sha256:47bcc53f74dc94b1920f0b34f6036096526296767650f223433fe65c35f149eb 603 <missing> 5 minutes ago /bin/sh -c rm /remove_me 0 B 604 <missing> 5 minutes ago /bin/sh -c #(nop) ENV HELLO=world 0 B 605 <missing> 5 minutes ago /bin/sh -c touch remove_me /remove_me 0 B 606 <missing> 5 minutes ago /bin/sh -c echo world >> /hello 0 B 607 <missing> 6 minutes ago /bin/sh -c echo hello > /hello 0 B 608 <missing> 7 weeks ago /bin/sh -c #(nop) CMD ["sh"] 0 B 609 <missing> 7 weeks ago /bin/sh -c #(nop) ADD file:47ca6e777c36a4cfff 1.113 MB 610 ``` 611 612 We could find that all layer's name is `<missing>`, and there is a new layer with COMMENT `merge`. 613 614 Test the image, check for `/remove_me` being gone, make sure `hello\nworld` is in `/hello`, make sure the `HELLO` envvar's value is `world`.