|
| 1 | +--- |
| 2 | +title: Export binaries |
| 3 | +description: Using Docker builds to create and export executable binaries |
| 4 | +keywords: build, buildkit, buildx, guide, tutorial, build arguments, arg |
| 5 | +aliases: |
| 6 | + - /build/guide/export/ |
| 7 | +--- |
| 8 | + |
| 9 | +Did you know that you can use Docker to build your application to standalone |
| 10 | +binaries? Sometimes, you don’t want to package and distribute your application |
| 11 | +as a Docker image. Use Docker to build your application, and use exporters to |
| 12 | +save the output to disk. |
| 13 | + |
| 14 | +The default output format for `docker build` is a container image. That image is |
| 15 | +automatically loaded to your local image store, where you can run a container |
| 16 | +from that image, or push it to a registry. Under the hood, this uses the default |
| 17 | +exporter, called the `docker` exporter. |
| 18 | + |
| 19 | +To export your build results as files instead, you can use the `--output` flag, |
| 20 | +or `-o` for short. the `--output` flag lets you change the output format of |
| 21 | +your build. |
| 22 | + |
| 23 | +## Export binaries from a build |
| 24 | + |
| 25 | +If you specify a filepath to the `docker build --output` flag, Docker exports |
| 26 | +the contents of the build container at the end of the build to the specified |
| 27 | +location on your host's filesystem. This uses the `local` |
| 28 | +[exporter](/build/exporters/local-tar.md). |
| 29 | + |
| 30 | +The neat thing about this is that you can use Docker's powerful isolation and |
| 31 | +build features to create standalone binaries. This |
| 32 | +works well for Go, Rust, and other languages that can compile to a single |
| 33 | +binary. |
| 34 | + |
| 35 | +The following example creates a simple Rust program that prints "Hello, |
| 36 | +World!", and exports the binary to the host filesystem. |
| 37 | + |
| 38 | +1. Create a new directory for this example, and navigate to it: |
| 39 | + |
| 40 | + ```console |
| 41 | + $ mkdir hello-world-bin |
| 42 | + $ cd hello-world-bin |
| 43 | + ``` |
| 44 | + |
| 45 | +2. Create a Dockerfile with the following contents: |
| 46 | + |
| 47 | + ```Dockerfile |
| 48 | + # syntax=docker/dockerfile:1 |
| 49 | + FROM rust:alpine AS build |
| 50 | + WORKDIR /src |
| 51 | + COPY <<EOT hello.rs |
| 52 | + fn main() { |
| 53 | + println!("Hello World!"); |
| 54 | + } |
| 55 | + EOT |
| 56 | + RUN rustc -o /bin/hello hello.rs |
| 57 | + |
| 58 | + FROM scratch |
| 59 | + COPY --from=build /bin/hello / |
| 60 | + ENTRYPOINT ["/hello"] |
| 61 | + ``` |
| 62 | + |
| 63 | + > [!TIP] |
| 64 | + > The `COPY <<EOT` syntax is a [here-document](/reference/dockerfile.md#here-documents). |
| 65 | + > It lets you write multi-line strings in a Dockerfile. Here it's used to |
| 66 | + > create a simple Rust program inline in the Dockerfile. |
| 67 | +
|
| 68 | + This Dockerfile uses a multi-stage build to compile the program in the first |
| 69 | + stage, and then copies the binary to a scratch image in the second. The |
| 70 | + final image is a minimal image that only contains the binary. This use case |
| 71 | + for the `scratch` image is common for creating minimal build artifacts for |
| 72 | + programs that don't require a full operating system to run. |
| 73 | + |
| 74 | +3. Build the Dockerfile and export the binary to the current working directory: |
| 75 | + |
| 76 | + ```console |
| 77 | + $ docker build --output=. . |
| 78 | + ``` |
| 79 | + |
| 80 | + This command builds the Dockerfile and exports the binary to the current |
| 81 | + working directory. The binary is named `hello`, and it's created in the |
| 82 | + current working directory. |
| 83 | + |
| 84 | +## Exporting multi-platform builds |
| 85 | + |
| 86 | +You use the `local` exporter to export binaries in combination with |
| 87 | +[multi-platform builds](/build/building/multi-platform.md). This lets you |
| 88 | +compile multiple binaries at once, that can be run on any machine of any |
| 89 | +architecture, provided that the target platform is supported by the compiler |
| 90 | +you use. |
| 91 | + |
| 92 | +Continuing on the example Dockerfile in the |
| 93 | +[Export binaries from a build](#export-binaries-from-a-build) section: |
| 94 | + |
| 95 | +```dockerfile |
| 96 | +# syntax=docker/dockerfile:1 |
| 97 | +FROM rust:alpine AS build |
| 98 | +WORKDIR /src |
| 99 | +COPY <<EOT hello.rs |
| 100 | +fn main() { |
| 101 | + println!("Hello World!"); |
| 102 | +} |
| 103 | +EOT |
| 104 | +RUN rustc -o /bin/hello hello.rs |
| 105 | + |
| 106 | +FROM scratch |
| 107 | +COPY --from=build /bin/hello / |
| 108 | +ENTRYPOINT ["/hello"] |
| 109 | +``` |
| 110 | + |
| 111 | +You can build this Rust program for multiple platforms using the `--platform` |
| 112 | +flag with the `docker build` command. In combination with the `--output` flag, |
| 113 | +the build exports the binaries for each target to the specified directory. |
| 114 | + |
| 115 | +For example, to build the program for both `linux/amd64` and `linux/arm64`: |
| 116 | + |
| 117 | +```console |
| 118 | +$ docker build --platform=linux/amd64,linux/arm64 --output=out . |
| 119 | +$ tree out/ |
| 120 | +out/ |
| 121 | +├── linux_amd64 |
| 122 | +│ └── hello |
| 123 | +└── linux_arm64 |
| 124 | + └── hello |
| 125 | + |
| 126 | +3 directories, 2 files |
| 127 | +``` |
0 commit comments