Optimize caching in Dockerfile
This commit is contained in:
52
Dockerfile
52
Dockerfile
@ -1,18 +1,56 @@
|
||||
##################
|
||||
### BASE STAGE ###
|
||||
##################
|
||||
FROM rust:1.53 as base
|
||||
|
||||
# Install build dependencies
|
||||
RUN cargo install strip_cargo_version
|
||||
RUN rustup target add x86_64-unknown-linux-musl
|
||||
|
||||
WORKDIR /app
|
||||
RUN mkdir cli lib server
|
||||
|
||||
|
||||
###########################
|
||||
### STRIP-VERSION STAGE ###
|
||||
###########################
|
||||
FROM base as strip-version
|
||||
|
||||
# Strip version from Cargo.*
|
||||
# This avoids cache invalidations (ergo rebuilding all deps) when bumping the version number
|
||||
COPY Cargo.lock Cargo.toml ./
|
||||
COPY server/Cargo.toml ./server/
|
||||
COPY cli/Cargo.toml ./cli/
|
||||
COPY lib/Cargo.toml ./lib/
|
||||
RUN strip_cargo_version
|
||||
|
||||
|
||||
###################
|
||||
### BUILD STAGE ###
|
||||
###################
|
||||
FROM rust:1.51 as build_stage
|
||||
FROM base as build_stage
|
||||
|
||||
# Install build dependencies
|
||||
RUN rustup target add x86_64-unknown-linux-musl
|
||||
RUN cargo init --bin server
|
||||
RUN cargo init --lib lib
|
||||
|
||||
# Create a dummy binary for pre-compiling dependencies (for caching)
|
||||
COPY --from=strip-version /app/server/Cargo.toml /app/server/
|
||||
COPY --from=strip-version /app/lib/Cargo.toml /app/lib/
|
||||
COPY --from=strip-version /app/cli/Cargo.toml /app/cli/
|
||||
COPY --from=strip-version /app/Cargo.lock /app/Cargo.toml /app/
|
||||
|
||||
# Build project
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
WORKDIR /app/server
|
||||
RUN cargo build --release --target x86_64-unknown-linux-musl
|
||||
|
||||
# Copy actual source files
|
||||
WORKDIR /app
|
||||
RUN strip target/x86_64-unknown-linux-musl/release/stl
|
||||
COPY . .
|
||||
|
||||
# Compile final binary
|
||||
WORKDIR /app/server
|
||||
RUN cargo build --release --target x86_64-unknown-linux-musl
|
||||
RUN strip /app/target/x86_64-unknown-linux-musl/release/stl
|
||||
|
||||
|
||||
########################
|
||||
### PRODUCTION STAGE ###
|
||||
|
||||
Reference in New Issue
Block a user