Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
54 lines (49 loc) · 2.35 KB
/
Copy pathDockerfile
File metadata and controls
54 lines (49 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Shared base image for all steps except runtime
# This is necessary to ensure that the same rust version is used for all these steps
FROM lukemathwalker/cargo-chef:latest-rust-1.86 AS chef
# Created by docker for us in case it doesn't exist
WORKDIR /app
# Required for our linking config: rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"]
RUN apt update && apt install lld clang -y
# Dependencies planner stage
FROM chef AS planner
# Copy all files from current directory to working directory
# This invalidates the cache on code changes, but only for the planner stage
COPY . .
# Compute a lock-like file for our project
RUN cargo chef prepare --recipe-path recipe.json
# Builder stage
# We run both chef and build cargo commands in this stage as they need to be executed from the same working directory
FROM chef AS builder
# We only use recipe.json from the planner container, so as long as that doesn't change, the cache remains valid
COPY --from=planner /app/recipe.json recipe.json
# Build just the dependencies of the application
RUN cargo chef cook --release --recipe-path recipe.json
#--- Uptil this point, if our dependency tree stays the same, all layers should be cached ---#
# Copy all files from current directory to working directory
COPY . .
# Make sure sqlx doesn't try to communicate with a database
ENV SQLX_OFFLINE=true
# Build the application
RUN cargo build --release --bin zero2prod
# Runtime stage
FROM debian:bookworm-slim AS runtime
WORKDIR /app
# Install OpenSSL - it is dynamically linked by some of our dependencies
# Install ca-certificates - it is needed to verify TLS certificates when establishing HTTPS connections
RUN apt-get update -y \
&& apt-get install -y --no-install-recommends openssl ca-certificates \
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*
# Copy the compiled binary from the builder environment to our runtime environment
COPY --from=builder /app/target/release/zero2prod zero2prod
# Copy all files from current directory to working directory (as we need things like configuration at runtime)
# Note that we could be more specific here, but we'd rather forget adding something to .dockerignore than forget adding
# something here
COPY . .
# Need to set the profile to production
ENV ACTIVE_PROFILE=production
# Launch the binary on `docker run`
ENTRYPOINT ["./zero2prod"]