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