Skip to content

Instantly share code, notes, and snippets.

@martinos
Forked from ihrwein/README.md
Last active February 6, 2019 14:36
Show Gist options
  • Save martinos/50d273ffee72bf9d256583cf4d95f56b to your computer and use it in GitHub Desktop.
Save martinos/50d273ffee72bf9d256583cf4d95f56b to your computer and use it in GitHub Desktop.
Building minimal Rust containers with Docker multi stage builds

The built image for a hello world project is less than 8 MB.

Builder image (it'd be best to upload this image to Docker Hub. This is 100% percent reusable).

FROM ubuntu:latest

ENV TARGET=x86_64-unknown-linux-musl
ENV BUILD_DIR=/src/target/x86_64-unknown-linux-musl/release/

RUN apt-get update && \
    apt-get install \
        curl \
        gcc \
        -y

RUN curl https://sh.rustup.rs -sSf -o /tmp/rustup-init.sh
RUN sh /tmp/rustup-init.sh -y

RUN ~/.cargo/bin/rustup target add ${TARGET}

ONBUILD COPY . /src
ONBUILD WORKDIR /src

ONBUILD RUN ~/.cargo/bin/cargo test --release --target=${TARGET}
ONBUILD RUN ~/.cargo/bin/cargo build --release --target=${TARGET}

# Build artifacts will be available in /app.
RUN mkdir /app
# Copy the "interesting" files into /app.
ONBUILD RUN find ${BUILD_DIR} \
                -regextype egrep \
                # The interesting binaries are all directly in ${BUILD_DIR}.
                -maxdepth 1 \
                # Well, binaries are executable.
                -executable \
                # Well, binaries are files.
                -type f \
                # Filter out tests.
                ! -regex ".*\-[a-fA-F0-9]{16,16}$" \
                # Copy the matching files into /app.
                -exec cp {} /app \;

ONBUILD RUN echo "The following files will be copied to the runtime image: $(ls /app)"

Build it once with docker build . -t rust:stable.

Runtime image (create this Dockerfile in your crate, next to Cargo.toml):

FROM rust:stable

FROM alpine:latest
COPY --from=0 /app/ /app/

CMD ["/app/hello2"]

Then to run:

docker build .

Then the image ID will be printed to the screen.

Successfully built 4ed04e01aa03

If you want the executable only (without the container) and copy it to the host.

docker run 4ed04e01aa03 cat app/hello2 > path/to/host/hello2

That's the minimal amount of boilerplate that currently works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment