generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from amosproj/change/improve-generator-docker…
…-build Improved generator docker build by caching dependencies
- Loading branch information
Showing
1 changed file
with
33 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |