Skip to content

Instantly share code, notes, and snippets.

@technosophos
Created September 27, 2019 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technosophos/a0d7ba3ad5d9815e3d4f3582320f5959 to your computer and use it in GitHub Desktop.
Save technosophos/a0d7ba3ad5d9815e3d4f3582320f5959 to your computer and use it in GitHub Desktop.
Rust Dockerfile hacks for faster builds
FROM rust:1.37 AS builder
WORKDIR /usr/src/buck
COPY Cargo.toml .
COPY Cargo.lock .
# Layer hack: Build an empty program to compile dependencies and place on their own layer.
# This cuts down build time
RUN mkdir -p ./src/ && \
echo 'fn main() {}' > ./src/main.rs && \
echo '' > ./src/lib.rs
RUN cargo fetch
RUN cargo build --release && \
rm -rf ./target/release/.fingerprint/buck-*
# Build real binaries now
COPY ./src ./src
RUN cargo build --release
# Copy binary into a Debian container
FROM debian:stretch-slim
WORKDIR /usr/app
RUN apt-get update && apt-get install -y openssl && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/src/buck/target/release/buck .
CMD ["./buck"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment