Skip to content

Instantly share code, notes, and snippets.

@rhrn
Last active March 15, 2023 14:32
Show Gist options
  • Save rhrn/f7a13ba76c0d0de6993b0f23b3f80630 to your computer and use it in GitHub Desktop.
Save rhrn/f7a13ba76c0d0de6993b0f23b3f80630 to your computer and use it in GitHub Desktop.
Dockerfile
target
.git
.env

Rust app in scratch docker container

Build

docker build -t rust-app-scratch -f Dockerfile.scratch .

Run

docker run --rm rust-app-scratch

Imgage sizes

Hello world - 4.2MB
FROM rust:1.68.0-alpine3.16
RUN apk --update --no-cache add g++ pkgconfig openssl-dev
WORKDIR /app
COPY Cargo.lock .
COPY Cargo.toml .
RUN cargo fetch --target x86_64-unknown-linux-musl
COPY . .
RUN cargo build --release
FROM scratch
# Add to Cargo.toml
# [[bin]]
# name = "app"
# path = "src/main.rs"
COPY --from=0 /app/target/release/app /app
ENTRYPOINT ["/app"]
FROM rust:1.65.0-alpine3.16
WORKDIR /app
RUN cargo init
RUN cargo build --release
FROM scratch
COPY --from=0 /app/target/release/app /app
ENTRYPOINT ["/app"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment