Optimize caching in Dockerfile

This commit is contained in:
2021-07-08 01:41:43 +02:00
parent 22b4daa26a
commit 3306c3be88
5 changed files with 53 additions and 13 deletions

8
Cargo.lock generated
View File

@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]] [[package]]
name = "addr2line" name = "addr2line"
version = "0.14.1" version = "0.14.1"
@ -2166,7 +2168,7 @@ checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0"
[[package]] [[package]]
name = "stl" name = "stl"
version = "2.6.4" version = "2.6.5"
dependencies = [ dependencies = [
"bincode", "bincode",
"chrono", "chrono",
@ -2189,7 +2191,7 @@ dependencies = [
[[package]] [[package]]
name = "stl_cli" name = "stl_cli"
version = "2.6.4" version = "2.6.5"
dependencies = [ dependencies = [
"chrono", "chrono",
"futures", "futures",
@ -2208,7 +2210,7 @@ dependencies = [
[[package]] [[package]]
name = "stl_lib" name = "stl_lib"
version = "2.6.4" version = "2.6.5"
dependencies = [ dependencies = [
"chrono", "chrono",
"serde", "serde",

View File

@ -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 ### ### BUILD STAGE ###
################### ###################
FROM rust:1.51 as build_stage FROM base as build_stage
# Install build dependencies RUN cargo init --bin server
RUN rustup target add x86_64-unknown-linux-musl 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 WORKDIR /app/server
RUN cargo build --release --target x86_64-unknown-linux-musl RUN cargo build --release --target x86_64-unknown-linux-musl
# Copy actual source files
WORKDIR /app 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 ### ### PRODUCTION STAGE ###

View File

@ -1,6 +1,6 @@
[package] [package]
name = "stl_cli" name = "stl_cli"
version = "2.6.4" version = "2.6.5"
authors = ["Joakim Hulthe <joakim@hulthe.net>"] authors = ["Joakim Hulthe <joakim@hulthe.net>"]
license = "MPL-2.0" license = "MPL-2.0"
edition = "2018" edition = "2018"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "stl_lib" name = "stl_lib"
version = "2.6.4" version = "2.6.5"
authors = ["Joakim Hulthe <joakim@hulthe.net>"] authors = ["Joakim Hulthe <joakim@hulthe.net>"]
license = "MPL-2.0" license = "MPL-2.0"
edition = "2018" edition = "2018"

View File

@ -1,7 +1,7 @@
[package] [package]
name = "stl" name = "stl"
description = "studielogg aka scuffed toggl" description = "studielogg aka scuffed toggl"
version = "2.6.4" version = "2.6.5"
authors = ["Joakim Hulthe <joakim@hulthe.net>"] authors = ["Joakim Hulthe <joakim@hulthe.net>"]
license = "MPL-2.0" license = "MPL-2.0"
edition = "2018" edition = "2018"