@@ -47,6 +47,7 @@ Options:
4747 'host': use the Docker host network stack
4848 '<network-name >|<network-id >': connect to a user-defined network
4949 --no-cache Do not use cache when building the image
50+ -o, --output Output destination (format: type=local,dest=path)
5051 --pull Always attempt to pull a newer version of the image
5152 --progress Set type of progress output (only if BuildKit enabled) (auto, plain, tty).
5253 Use plain to show container output
@@ -323,7 +324,16 @@ Successfully built 99cc1ad10469
323324This example shows the use of the ` .dockerignore ` file to exclude the ` .git `
324325directory from the context. Its effect can be seen in the changed size of the
325326uploaded context. The builder reference contains detailed information on
326- [ creating a .dockerignore file] ( ../builder.md#dockerignore-file )
327+ [ creating a .dockerignore file] ( ../builder.md#dockerignore-file ) .
328+
329+ When using the [ BuildKit backend] ( ../builder.md#buildkit ) , ` docker build ` searches
330+ for a ` .dockerignore ` file relative to the Dockerfile name. For example, running
331+ ` docker build -f myapp.Dockerfile . ` will first look for an ignore file named
332+ ` myapp.Dockerfile.dockerignore ` . If such a file is not found, the ` .dockerignore `
333+ file is used if present. Using a Dockerfile based ` .dockerignore ` is useful if a
334+ project contains multiple Dockerfiles that expect to ignore different sets of
335+ files.
336+
327337
328338### Tag an image (-t)
329339
@@ -489,6 +499,137 @@ FROM alpine AS production-env
489499$ docker build -t mybuildimage --target build-env .
490500```
491501
502+ ### Custom build outputs
503+
504+ By default, a local container image is created from the build result. The
505+ ` --output ` (or ` -o ` ) flag allows you to override this behavior, and a specify a
506+ custom exporter. For example, custom exporters allow you to export the build
507+ artifacts as files on the local filesystem instead of a Docker image, which can
508+ be useful for generating local binaries, code generation etc.
509+
510+ The value for ` --output ` is a CSV-formatted string defining the exporter type
511+ and options. Currently, ` local ` and ` tar ` exporters are supported. The ` local `
512+ exporter writes the resulting build files to a directory on the client side. The
513+ ` tar ` exporter is similar but writes the files as a single tarball (` .tar ` ).
514+
515+ If no type is specified, the value defaults to the output directory of the local
516+ exporter. Use a hyphen (` - ` ) to write the output tarball to standard output
517+ (` STDOUT ` ).
518+
519+ The following example builds an image using the current directory (` . ` ) as build
520+ context, and exports the files to a directory named ` out ` in the current directory.
521+ If the directory does not exist, Docker creates the directory automatically:
522+
523+ ``` bash
524+ $ docker build -o out .
525+ ```
526+
527+ The example above uses the short-hand syntax, omitting the ` type ` options, and
528+ thus uses the default (` local ` ) exporter. The example below shows the equivalent
529+ using the long-hand CSV syntax, specifying both ` type ` and ` dest ` (destination
530+ path):
531+
532+ ``` bash
533+ $ docker build --output type=local,dest=out .
534+ ```
535+
536+ Use the ` tar ` type to export the files as a ` .tar ` archive:
537+
538+ ``` bash
539+ $ docker build --output type=tar,dest=out.tar .
540+ ```
541+
542+ The example below shows the equivalent when using the short-hand syntax. In this
543+ case, ` - ` is specified as destination, which automatically selects the ` tar ` type,
544+ and writes the output tarball to standard output, which is then redirected to
545+ the ` out.tar ` file:
546+
547+ ``` bash
548+ docker build -o - . > out.tar
549+ ```
550+
551+ The ` --output ` option exports all files from the target stage. A common pattern
552+ for exporting only specific files is to do multi-stage builds and to copy the
553+ desired files to a new scratch stage with [ ` COPY --from ` ] ( ../builder.md#copy ) .
554+
555+ The example ` Dockerfile ` below uses a separate stage to collect the
556+ build-artifacts for exporting:
557+
558+ ``` Dockerfile
559+ FROM golang AS build-stage
560+ RUN go get -u github.com/LK4D4/vndr
561+
562+ FROM scratch AS export-stage
563+ COPY --from=build-stage /go/bin/vndr /
564+ ```
565+
566+ When building the Dockerfile with the ` -o ` option, only the files from the final
567+ stage are exported to the ` out ` directory, in this case, the ` vndr ` binary:
568+
569+ ``` bash
570+ $ docker build -o out .
571+
572+ [+] Building 2.3s (7/7) FINISHED
573+ => [internal] load build definition from Dockerfile 0.1s
574+ => => transferring dockerfile: 176B 0.0s
575+ => [internal] load .dockerignore 0.0s
576+ => => transferring context: 2B 0.0s
577+ => [internal] load metadata for docker.io/library/golang:latest 1.6s
578+ => [build-stage 1/2] FROM docker.io/library/golang@sha256:2df96417dca0561bf1027742dcc5b446a18957cd28eba6aa79269f23f1846d3f 0.0s
579+ => => resolve docker.io/library/golang@sha256:2df96417dca0561bf1027742dcc5b446a18957cd28eba6aa79269f23f1846d3f 0.0s
580+ => CACHED [build-stage 2/2] RUN go get -u github.com/LK4D4/vndr 0.0s
581+ => [export-stage 1/1] COPY --from=build-stage /go/bin/vndr / 0.2s
582+ => exporting to client 0.4s
583+ => => copying files 10.30MB 0.3s
584+
585+ $ ls ./out
586+ vndr
587+ ```
588+
589+ > ** Note** : This feature requires the BuildKit backend. You can either
590+ > [ enable BuildKit] ( ../builder.md#buildkit ) or use the [ buildx] ( https://github.com/docker/buildx )
591+ > plugin which provides more output type options.
592+
593+ ### Specifying external cache sources
594+
595+ In addition to local build cache, the builder can reuse the cache generated from
596+ previous builds with the ` --cache-from ` flag pointing to an image in the registry.
597+
598+ To use an image as a cache source, cache metadata needs to be written into the
599+ image on creation. This can be done by setting ` --build-arg BUILDKIT_INLINE_CACHE=1 `
600+ when building the image. After that, the built image can be used as a cache source
601+ for subsequent builds.
602+
603+ Upon importing the cache, the builder will only pull the JSON metadata from the
604+ registry and determine possible cache hits based on that information. If there
605+ is a cache hit, the matched layers are pulled into the local environment.
606+
607+ In addition to images, the cache can also be pulled from special cache manifests
608+ generated by [ ` buildx ` ] ( https://github.com/docker/buildx ) or the BuildKit CLI
609+ (` buildctl ` ). These manifests (when built with the ` type=registry ` and ` mode=max `
610+ options) allow pulling layer data for intermediate stages in multi-stage builds.
611+
612+ The following example builds an image with inline-cache metadata and pushes it
613+ to a registry, then uses the image as a cache source on another machine:
614+
615+ ``` bash
616+ $ docker build -t myname/myapp --build-arg BUILDKIT_INLINE_CACHE=1 .
617+ $ docker push myname/myapp
618+ ```
619+
620+ After pushing the image, the image is used as cache source on another machine.
621+ BuildKit automatically pulls the image from the registry if needed.
622+
623+ ``` bash
624+ # on another machine
625+ $ docker build --cache-from myname/myapp .
626+ ```
627+
628+ > ** Note** : This feature requires the BuildKit backend. You can either
629+ > [ enable BuildKit] ( ../builder.md#buildkit ) or use the [ buildx] ( https://github.com/docker/buildx )
630+ > plugin. The previous builder has limited support for reusing cache from
631+ > pre-pulled images.
632+
492633### Squash an image's layers (--squash) (experimental)
493634
494635#### Overview
0 commit comments