github.com/xeptore/docker-cli@v20.10.14+incompatible/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](../builder.md#buildkit), `docker build` searches
   327  for a `.dockerignore` file relative to the Dockerfile name. For example, running
   328  `docker build -f myapp.Dockerfile .` will first look for an ignore file named
   329  `myapp.Dockerfile.dockerignore`. If such a file is not found, the `.dockerignore`
   330  file is used if present. Using a Dockerfile based `.dockerignore` is useful if a
   331  project contains multiple Dockerfiles that expect to ignore different sets of
   332  files.
   333  
   334  
   335  ### Tag an image (-t)
   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  ### Specify a Dockerfile (-f)
   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  ### 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  ### 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#set-ulimits-in-container---ulimit).
   411  
   412  ### 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](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file)
   448  for more information.
   449  
   450  ### 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  ### 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  ### 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  ### 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  ### Custom build outputs
   499  
   500  By default, a local container image is created from the build result. The
   501  `--output` (or `-o`) flag allows you to override this behavior, and a specify a
   502  custom exporter. For example, custom exporters allow you to export the build
   503  artifacts as files on the local filesystem instead of a Docker image, which can
   504  be useful for generating local binaries, code generation etc.
   505  
   506  The value for `--output` is a CSV-formatted string defining the exporter type
   507  and options. Currently, `local` and `tar` exporters are supported. The `local`
   508  exporter writes the resulting build files to a directory on the client side. The
   509  `tar` exporter is similar but writes the files as a single tarball (`.tar`).
   510  
   511  If no type is specified, the value defaults to the output directory of the local
   512  exporter. Use a hyphen (`-`) to write the output tarball to standard output
   513  (`STDOUT`).
   514  
   515  The following example builds an image using the current directory (`.`) as build
   516  context, and exports the files to a directory named `out` in the current directory.
   517  If the directory does not exist, Docker creates the directory automatically:
   518  
   519  ```console
   520  $ docker build -o out .
   521  ```
   522  
   523  The example above uses the short-hand syntax, omitting the `type` options, and
   524  thus uses the default (`local`) exporter. The example below shows the equivalent
   525  using the long-hand CSV syntax, specifying both `type` and `dest` (destination
   526  path):
   527  
   528  ```console
   529  $ docker build --output type=local,dest=out .
   530  ```
   531  
   532  Use the `tar` type to export the files as a `.tar` archive:
   533  
   534  ```console
   535  $ docker build --output type=tar,dest=out.tar .
   536  ```
   537  
   538  The example below shows the equivalent when using the short-hand syntax. In this
   539  case, `-` is specified as destination, which automatically selects the `tar` type,
   540  and writes the output tarball to standard output, which is then redirected to
   541  the `out.tar` file:
   542  
   543  ```console
   544  $ docker build -o - . > out.tar
   545  ```
   546  
   547  The `--output` option exports all files from the target stage. A common pattern
   548  for exporting only specific files is to do multi-stage builds and to copy the
   549  desired files to a new scratch stage with [`COPY --from`](../builder.md#copy).
   550  
   551  The example `Dockerfile` below uses a separate stage to collect the
   552  build-artifacts for exporting:
   553  
   554  ```dockerfile
   555  FROM golang AS build-stage
   556  RUN go get -u github.com/LK4D4/vndr
   557  
   558  FROM scratch AS export-stage
   559  COPY --from=build-stage /go/bin/vndr /
   560  ```
   561  
   562  When building the Dockerfile with the `-o` option, only the files from the final
   563  stage are exported to the `out` directory, in this case, the `vndr` binary:
   564  
   565  ```console
   566  $ docker build -o out .
   567  
   568  [+] Building 2.3s (7/7) FINISHED
   569   => [internal] load build definition from Dockerfile                                                                          0.1s
   570   => => transferring dockerfile: 176B                                                                                          0.0s
   571   => [internal] load .dockerignore                                                                                             0.0s
   572   => => transferring context: 2B                                                                                               0.0s
   573   => [internal] load metadata for docker.io/library/golang:latest                                                              1.6s
   574   => [build-stage 1/2] FROM docker.io/library/golang@sha256:2df96417dca0561bf1027742dcc5b446a18957cd28eba6aa79269f23f1846d3f   0.0s
   575   => => resolve docker.io/library/golang@sha256:2df96417dca0561bf1027742dcc5b446a18957cd28eba6aa79269f23f1846d3f               0.0s
   576   => CACHED [build-stage 2/2] RUN go get -u github.com/LK4D4/vndr                                                              0.0s
   577   => [export-stage 1/1] COPY --from=build-stage /go/bin/vndr /                                                                 0.2s
   578   => exporting to client                                                                                                       0.4s
   579   => => copying files 10.30MB                                                                                                  0.3s
   580  
   581  $ ls ./out
   582  vndr
   583  ```
   584  
   585  > **Note**
   586  >
   587  > This feature requires the BuildKit backend. You can either
   588  > [enable BuildKit](../builder.md#buildkit) or use the [buildx](https://github.com/docker/buildx)
   589  > plugin which provides more output type options.
   590  
   591  ### Specifying external cache sources
   592  
   593  In addition to local build cache, the builder can reuse the cache generated from
   594  previous builds with the `--cache-from` flag pointing to an image in the registry.
   595  
   596  To use an image as a cache source, cache metadata needs to be written into the
   597  image on creation. This can be done by setting `--build-arg BUILDKIT_INLINE_CACHE=1`
   598  when building the image. After that, the built image can be used as a cache source
   599  for subsequent builds.
   600  
   601  Upon importing the cache, the builder will only pull the JSON metadata from the
   602  registry and determine possible cache hits based on that information. If there
   603  is a cache hit, the matched layers are pulled into the local environment.
   604  
   605  In addition to images, the cache can also be pulled from special cache manifests
   606  generated by [`buildx`](https://github.com/docker/buildx) or the BuildKit CLI
   607  (`buildctl`). These manifests (when built with the `type=registry` and `mode=max`
   608  options) allow pulling layer data for intermediate stages in multi-stage builds.
   609  
   610  The following example builds an image with inline-cache metadata and pushes it
   611  to a registry, then uses the image as a cache source on another machine:
   612  
   613  ```console
   614  $ docker build -t myname/myapp --build-arg BUILDKIT_INLINE_CACHE=1 .
   615  $ docker push myname/myapp
   616  ```
   617  
   618  After pushing the image, the image is used as cache source on another machine.
   619  BuildKit automatically pulls the image from the registry if needed.
   620  
   621  On another machine:
   622  
   623  ```console
   624  $ docker build --cache-from myname/myapp .
   625  ```
   626  
   627  > **Note**
   628  >
   629  > This feature requires the BuildKit backend. You can either
   630  > [enable BuildKit](../builder.md#buildkit) or use the [buildx](https://github.com/docker/buildx)
   631  > plugin. The previous builder has limited support for reusing cache from
   632  > pre-pulled images.
   633  
   634  ### Squash an image's layers (--squash) (experimental)
   635  
   636  #### Overview
   637  
   638  Once the image is built, squash the new layers into a new image with a single
   639  new layer. Squashing does not destroy any existing image, rather it creates a new
   640  image with the content of the squashed layers. This effectively makes it look
   641  like all `Dockerfile` commands were created with a single layer. The build
   642  cache is preserved with this method.
   643  
   644  The `--squash` option is an experimental feature, and should not be considered
   645  stable.
   646  
   647  
   648  Squashing layers can be beneficial if your Dockerfile produces multiple layers
   649  modifying the same files, for example, files that are created in one step, and
   650  removed in another step. For other use-cases, squashing images may actually have
   651  a negative impact on performance; when pulling an image consisting of multiple
   652  layers, layers can be pulled in parallel, and allows sharing layers between
   653  images (saving space).
   654  
   655  For most use cases, multi-stage builds are a better alternative, as they give more
   656  fine-grained control over your build, and can take advantage of future
   657  optimizations in the builder. Refer to the [use multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/)
   658  section in the userguide for more information.
   659  
   660  
   661  #### Known limitations
   662  
   663  The `--squash` option has a number of known limitations:
   664  
   665  - When squashing layers, the resulting image cannot take advantage of layer
   666    sharing with other images, and may use significantly more space. Sharing the
   667    base image is still supported.
   668  - When using this option you may see significantly more space used due to
   669    storing two copies of the image, one for the build cache with all the cache
   670    layers intact, and one for the squashed version.
   671  - While squashing layers may produce smaller images, it may have a negative
   672    impact on performance, as a single layer takes longer to extract, and
   673    downloading a single layer cannot be parallelized.
   674  - When attempting to squash an image that does not make changes to the
   675    filesystem (for example, the Dockerfile only contains `ENV` instructions),
   676    the squash step will fail (see [issue #33823](https://github.com/moby/moby/issues/33823)).
   677  
   678  #### Prerequisites
   679  
   680  The example on this page is using experimental mode in Docker 19.03.
   681  
   682  Experimental mode can be enabled by using the `--experimental` flag when starting
   683  the Docker daemon or setting `experimental: true` in the `daemon.json` configuration
   684  file.
   685  
   686  By default, experimental mode is disabled. To see the current configuration of
   687  the docker daemon, use the `docker version` command and check the `Experimental`
   688  line in the `Engine` section:
   689  
   690  ```console
   691  Client: Docker Engine - Community
   692   Version:           19.03.8
   693   API version:       1.40
   694   Go version:        go1.12.17
   695   Git commit:        afacb8b
   696   Built:             Wed Mar 11 01:21:11 2020
   697   OS/Arch:           darwin/amd64
   698   Experimental:      false
   699  
   700  Server: Docker Engine - Community
   701   Engine:
   702    Version:          19.03.8
   703    API version:      1.40 (minimum version 1.12)
   704    Go version:       go1.12.17
   705    Git commit:       afacb8b
   706    Built:            Wed Mar 11 01:29:16 2020
   707    OS/Arch:          linux/amd64
   708    Experimental:     true
   709   [...]
   710  ```
   711  
   712  To enable experimental mode, users need to restart the docker daemon with the
   713  experimental flag enabled.
   714  
   715  #### Enable Docker experimental
   716  
   717  To enable experimental features, you need to start the Docker daemon with
   718  `--experimental` flag. You can also enable the daemon flag via
   719  `/etc/docker/daemon.json`, for example:
   720  
   721  ```json
   722  {
   723      "experimental": true
   724  }
   725  ```
   726  
   727  Then make sure the experimental flag is enabled:
   728  
   729  ```console
   730  $ docker version -f '{{.Server.Experimental}}'
   731  true
   732  ```
   733  
   734  #### Build an image with `--squash` argument
   735  
   736  The following is an example of docker build with `--squash` argument
   737  
   738  ```dockerfile
   739  FROM busybox
   740  RUN echo hello > /hello
   741  RUN echo world >> /hello
   742  RUN touch remove_me /remove_me
   743  ENV HELLO=world
   744  RUN rm /remove_me
   745  ```
   746  
   747  An image named `test` is built with `--squash` argument.
   748  
   749  ```console
   750  $ docker build --squash -t test .
   751  
   752  <...>
   753  ```
   754  
   755  If everything is right, the history looks like this:
   756  
   757  ```console
   758  $ docker history test
   759  
   760  IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
   761  4e10cb5b4cac        3 seconds ago                                                       12 B                merge sha256:88a7b0112a41826885df0e7072698006ee8f621c6ab99fca7fe9151d7b599702 to sha256:47bcc53f74dc94b1920f0b34f6036096526296767650f223433fe65c35f149eb
   762  <missing>           5 minutes ago       /bin/sh -c rm /remove_me                        0 B
   763  <missing>           5 minutes ago       /bin/sh -c #(nop) ENV HELLO=world               0 B
   764  <missing>           5 minutes ago       /bin/sh -c touch remove_me /remove_me           0 B
   765  <missing>           5 minutes ago       /bin/sh -c echo world >> /hello                 0 B
   766  <missing>           6 minutes ago       /bin/sh -c echo hello > /hello                  0 B
   767  <missing>           7 weeks ago         /bin/sh -c #(nop) CMD ["sh"]                    0 B
   768  <missing>           7 weeks ago         /bin/sh -c #(nop) ADD file:47ca6e777c36a4cfff   1.113 MB
   769  ```
   770  
   771  We could find that a layer's name is `<missing>`, and there is a new layer with
   772  COMMENT `merge`.
   773  
   774  Test the image, check for `/remove_me` being gone, make sure `hello\nworld` is
   775  in `/hello`, make sure the `HELLO` environment variable's value is `world`.