Skip to content

Instantly share code, notes, and snippets.

@JaredTan95
Last active July 9, 2019 05:03
Show Gist options
  • Save JaredTan95/c941e57f313bf495e04fa08d14e683e0 to your computer and use it in GitHub Desktop.
Save JaredTan95/c941e57f313bf495e04fa08d14e683e0 to your computer and use it in GitHub Desktop.
Deploy Rust App In Docker Container.
# Dockerfile for creating a statically-linked Rust application using docker's
# multi-stage build feature. This also leverages the docker build cache to avoid
# re-downloading dependencies if they have not changed.
FROM rust:latest AS build
WORKDIR /usr/src
# Download the target for static linking.
RUN rustup target add x86_64-unknown-linux-musl
# Create a dummy project and build the app's dependencies.
# If the Cargo.toml or Cargo.lock files have not changed,
# we can use the docker build cache and skip these (typically slow) steps.
RUN USER=root
WORKDIR /usr/src/rust-book-sample
# Cargo.toml:
# [package]
# name = "rust-book-sample"
# version = "0.1.0"
# authors = ["Jared.Tan <tanjian@apache.org>"]
# edition = "2018"
# [dependencies]
COPY Cargo.toml Cargo.lock ./
COPY src src
RUN cargo build --release \
&& cargo install --target x86_64-unknown-linux-musl --path .
# Copy the statically-linked binary into a scratch container.
FROM scratch
COPY --from=build /usr/local/cargo/bin/rust-book-sample .
USER 1000
CMD ["./rust-book-sample"]
# docker build -f Dockerfile-Rust-App -t rust-app .
# docker run rust-app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment