@@ -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
@@ -489,6 +490,97 @@ FROM alpine AS production-env
489490$ docker build -t mybuildimage --target build-env .
490491```
491492
493+ ### Custom build outputs
494+
495+ By default, a local container image is created from the build result. The
496+ ` --output ` (or ` -o ` ) flag allows you to override this behavior, and a specify a
497+ custom exporter. For example, custom exporters allow you to export the build
498+ artifacts as files on the local filesystem instead of a Docker image, which can
499+ be useful for generating local binaries, code generation etc.
500+
501+ The value for ` --output ` is a CSV-formatted string defining the exporter type
502+ and options. Currently, ` local ` and ` tar ` exporters are supported. The ` local `
503+ exporter writes the resulting build files to a directory on the client side. The
504+ ` tar ` exporter is similar but writes the files as a single tarball (` .tar ` ).
505+
506+ If no type is specified, the value defaults to the output directory of the local
507+ exporter. Use a hyphen (` - ` ) to write the output tarball to standard output
508+ (` STDOUT ` ).
509+
510+ The following example builds an image using the current directory (` . ` ) as build
511+ context, and exports the files to a directory named ` out ` in the current directory.
512+ If the directory does not exist, Docker creates the directory automatically:
513+
514+ ``` bash
515+ $ docker build -o out .
516+ ```
517+
518+ The example above uses the short-hand syntax, omitting the ` type ` options, and
519+ thus uses the default (` local ` ) exporter. The example below shows the equivalent
520+ using the long-hand CSV syntax, specifying both ` type ` and ` dest ` (destination
521+ path):
522+
523+ ``` bash
524+ $ docker build --output type=local,dest=out .
525+ ```
526+
527+ Use the ` tar ` type to export the files as a ` .tar ` archive:
528+
529+ ``` bash
530+ $ docker build --output type=tar,dest=out.tar .
531+ ```
532+
533+ The example below shows the equivalent when using the short-hand syntax. In this
534+ case, ` - ` is specified as destination, which automatically selects the ` tar ` type,
535+ and writes the output tarball to standard output, which is then redirected to
536+ the ` out.tar ` file:
537+
538+ ``` bash
539+ docker build -o - . > out.tar
540+ ```
541+
542+ The ` --output ` option exports all files from the target stage. A common pattern
543+ for exporting only specific files is to do multi-stage builds and to copy the
544+ desired files to a new scratch stage with [ ` COPY --from ` ] ( ../builder.md#copy ) .
545+
546+ The example ` Dockerfile ` below uses a separate stage to collect the
547+ build-artifacts for exporting:
548+
549+ ``` Dockerfile
550+ FROM golang AS build-stage
551+ RUN go get -u github.com/LK4D4/vndr
552+
553+ FROM scratch AS export-stage
554+ COPY --from=build-stage /go/bin/vndr /
555+ ```
556+
557+ When building the Dockerfile with the ` -o ` option, only the files from the final
558+ stage are exported to the ` out ` directory, in this case, the ` vndr ` binary:
559+
560+ ``` bash
561+ $ docker build -o out .
562+
563+ [+] Building 2.3s (7/7) FINISHED
564+ => [internal] load build definition from Dockerfile 0.1s
565+ => => transferring dockerfile: 176B 0.0s
566+ => [internal] load .dockerignore 0.0s
567+ => => transferring context: 2B 0.0s
568+ => [internal] load metadata for docker.io/library/golang:latest 1.6s
569+ => [build-stage 1/2] FROM docker.io/library/golang@sha256:2df96417dca0561bf1027742dcc5b446a18957cd28eba6aa79269f23f1846d3f 0.0s
570+ => => resolve docker.io/library/golang@sha256:2df96417dca0561bf1027742dcc5b446a18957cd28eba6aa79269f23f1846d3f 0.0s
571+ => CACHED [build-stage 2/2] RUN go get -u github.com/LK4D4/vndr 0.0s
572+ => [export-stage 1/1] COPY --from=build-stage /go/bin/vndr / 0.2s
573+ => exporting to client 0.4s
574+ => => copying files 10.30MB 0.3s
575+
576+ $ ls ./out
577+ vndr
578+ ```
579+
580+ > ** Note** : This feature requires the BuildKit backend. You can either
581+ > [ enable BuildKit] ( ../builder.md#buildkit ) or use the [ buildx] ( https://github.com/docker/buildx )
582+ > plugin which provides more output type options.
583+
492584### Specifying external cache sources
493585
494586In addition to local build cache, the builder can reuse the cache generated from
0 commit comments