Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved generator docker build by caching dependencies #138

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions Apps/generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
# Use official rust:slim as base
FROM rust:slim
################
##### Builder
FROM rust:slim as builder

WORKDIR /usr/src

# Create folder structur for code
RUN mkdir -p /usr/src/generator
# Create blank project
RUN USER=root cargo new generator

# We want dependencies cached, so copy those first.
COPY Cargo.toml Cargo.lock /usr/src/generator/

# Set the working directory
WORKDIR /usr/src/generator

# Copy source code onto docker image
COPY . .
## Install target platform (Cross-Compilation) --> Needed for Alpine
RUN rustup target add x86_64-unknown-linux-musl

# This is a dummy build to get the dependencies cached.
RUN cargo build --target x86_64-unknown-linux-musl --release

# Now copy in the rest of the sources
COPY src /usr/src/generator/src/

## Touch main.rs to prevent cached release build
RUN touch /usr/src/generator/src/main.rs

# This is the actual application build.
RUN cargo build --target x86_64-unknown-linux-musl --release

################
##### Runtime
FROM alpine:3.16.0 AS runtime

# Build
RUN cargo build
# Copy application binary from builder image
COPY --from=builder /usr/src/generator/target/x86_64-unknown-linux-musl/release/generator /usr/local/bin

# Run on container start
CMD ["cargo", "run"]
# Run the application
CMD ["/usr/local/bin/generator"]