Optimizing container builds for caching dependencies, (resulting in faster compile times)
This commit is contained in:
parent
d46794fc17
commit
d397b5c1ed
2 changed files with 22 additions and 6 deletions
|
@ -12,11 +12,19 @@ RUN npm run build
|
|||
# We use musl to get a truly static binary
|
||||
# that runs cleanly without depending on glibc.
|
||||
FROM docker.io/rust:latest as builder
|
||||
WORKDIR /build
|
||||
ADD server /build
|
||||
RUN apt update
|
||||
RUN apt install musl musl-dev musl-tools -y
|
||||
RUN rustup target add x86_64-unknown-linux-musl
|
||||
|
||||
# Some hacks to make podman cache build deps, see:
|
||||
# https://whitfin.io/blog/speeding-up-rust-docker-builds/
|
||||
RUN cargo new build-container
|
||||
WORKDIR /build-container
|
||||
COPY server/Cargo.toml server/Cargo.lock ./
|
||||
RUN cargo build --target x86_64-unknown-linux-musl --release
|
||||
RUN rm src/*.rs
|
||||
ADD server /build-container
|
||||
|
||||
RUN cargo build --target x86_64-unknown-linux-musl --release
|
||||
|
||||
# Final stage, copy the server binary and the frontend build
|
||||
|
@ -29,7 +37,7 @@ RUN addgroup -S user && adduser -S user -G user
|
|||
|
||||
WORKDIR /runner
|
||||
# Copy the server binary and the public directory
|
||||
COPY --from=builder /build/target/x86_64-unknown-linux-musl/release/server /runner/server
|
||||
COPY --from=builder /build-container/target/x86_64-unknown-linux-musl/release/server /runner/server
|
||||
# Copy the frontend SPA build into public
|
||||
COPY --from=client /build/dist /runner/public
|
||||
|
||||
|
|
|
@ -9,11 +9,19 @@
|
|||
# We use musl to get a truly static binary
|
||||
# that runs cleanly without depending on glibc.
|
||||
FROM docker.io/rust:latest as builder
|
||||
WORKDIR /build
|
||||
ADD server /build
|
||||
RUN apt update
|
||||
RUN apt install musl musl-dev musl-tools -y
|
||||
RUN rustup target add x86_64-unknown-linux-musl
|
||||
|
||||
# Some hacks to make podman cache build deps, see:
|
||||
# https://whitfin.io/blog/speeding-up-rust-docker-builds/
|
||||
RUN cargo new build-container
|
||||
WORKDIR /build-container
|
||||
COPY server/Cargo.toml server/Cargo.lock ./
|
||||
RUN cargo build --target x86_64-unknown-linux-musl
|
||||
RUN rm src/*.rs
|
||||
ADD server /build-container
|
||||
|
||||
# Note that '--release' is missing here, so we build in debug mode
|
||||
RUN cargo build --target x86_64-unknown-linux-musl
|
||||
|
||||
|
@ -27,7 +35,7 @@ RUN addgroup -S user && adduser -S user -G user
|
|||
|
||||
WORKDIR /runner
|
||||
# Copy the server binary and the public directory, note the debug binary
|
||||
COPY --from=builder /build/target/x86_64-unknown-linux-musl/debug/server /runner/server
|
||||
COPY --from=builder /build-container/target/x86_64-unknown-linux-musl/debug/server /runner/server
|
||||
RUN mkdir /runner/public
|
||||
RUN echo "Debug build!" > /runner/public/index.html
|
||||
|
||||
|
|
Loading…
Reference in a new issue