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