Home Explore Blog CI



docker

1st chunk of `content/manuals/build/building/export.md`
8167b321033389cbbcf3bd6f64da6e32d4a9d2096b95983000000001000008c7
---
title: Export binaries
weight: 50
description: Using Docker builds to create and export executable binaries
keywords: build, buildkit, buildx, guide, tutorial, build arguments, arg
aliases:
  - /build/guide/export/
---

Did you know that you can use Docker to build your application to standalone
binaries? Sometimes, you don’t want to package and distribute your application
as a Docker image. Use Docker to build your application, and use exporters to
save the output to disk.

The default output format for `docker build` is a container image. That image is
automatically loaded to your local image store, where you can run a container
from that image, or push it to a registry. Under the hood, this uses the default
exporter, called the `docker` exporter.

To export your build results as files instead, you can use the `--output` flag,
or `-o` for short. the `--output` flag lets you change the output format of
your build.

## Export binaries from a build

If you specify a filepath to the `docker build --output` flag, Docker exports
the contents of the build container at the end of the build to the specified
location on your host's filesystem. This uses the `local`
[exporter](/manuals/build/exporters/local-tar.md).

The neat thing about this is that you can use Docker's powerful isolation and
build features to create standalone binaries. This
works well for Go, Rust, and other languages that can compile to a single
binary.

The following example creates a simple Rust program that prints "Hello,
World!", and exports the binary to the host filesystem.

1. Create a new directory for this example, and navigate to it:

   ```console
   $ mkdir hello-world-bin
   $ cd hello-world-bin
   ```

2. Create a Dockerfile with the following contents:

   ```Dockerfile
   # syntax=docker/dockerfile:1
   FROM rust:alpine AS build
   WORKDIR /src
   COPY <<EOT hello.rs
   fn main() {
       println!("Hello World!");
   }
   EOT
   RUN rustc -o /bin/hello hello.rs
   
   FROM scratch
   COPY --from=build /bin/hello /
   ENTRYPOINT ["/hello"]
   ```

   > [!TIP]
   > The `COPY <<EOT` syntax is a [here-document](/reference/dockerfile.md#here-documents).
   > It lets you write multi-line strings in a Dockerfile. Here it's used to

Title: Exporting Standalone Binaries Using Docker Builds
Summary
Docker can be used to build applications into standalone binaries instead of Docker images. By using the `--output` flag with `docker build`, the contents of the build container can be exported to a specified location on the host's filesystem, using the `local` exporter. This is useful for languages like Go and Rust that can compile to a single binary. An example is provided, demonstrating how to build a simple Rust program and export the binary.