|
1 | 1 | FROM rust:1.80-alpine3.20 as builder
|
2 | 2 |
|
3 |
| -ARG UPTIME_CHECKER_GIT_REVISION |
4 |
| -ENV UPTIME_CHECKER_GIT_REVISION=$UPTIME_CHECKER_GIT_REVISION |
5 |
| - |
| 3 | +# Install system dependencies |
| 4 | +RUN apk add --no-cache \ |
| 5 | + libc-dev \ |
| 6 | + cmake \ |
| 7 | + make \ |
| 8 | + g++ \ |
| 9 | + pkgconfig \ |
| 10 | + openssl-dev |
| 11 | + |
| 12 | +# Configure cargo |
6 | 13 | RUN mkdir -p ~/.cargo && \
|
7 | 14 | echo '[registries.crates-io]' > ~/.cargo/config && \
|
8 | 15 | echo 'protocol = "sparse"' >> ~/.cargo/config
|
9 | 16 |
|
10 |
| -RUN apk add --no-cache libc-dev cmake make g++ |
11 |
| -RUN apk add --no-cache pkgconfig openssl-dev |
12 |
| - |
13 |
| -RUN cargo new --bin /app |
14 | 17 | WORKDIR /app
|
15 | 18 |
|
16 |
| -# Just copy the Cargo.toml files and trigger a build so that we compile our |
17 |
| -# dependencies only. This way we avoid layer cache invalidation if our |
18 |
| -# dependencies haven't changed, resulting in faster builds. |
| 19 | +# Copy only the files needed for dependency caching |
19 | 20 | COPY Cargo.toml Cargo.lock ./
|
20 | 21 | COPY redis-test-macro redis-test-macro/
|
21 | 22 |
|
| 23 | +# Create a dummy main.rs to build dependencies |
| 24 | +RUN mkdir src && \ |
| 25 | + echo "fn main() {}" > src/main.rs && \ |
| 26 | + # Build dependencies only |
| 27 | + export RUSTFLAGS="-Ctarget-feature=-crt-static" && \ |
| 28 | + export PKG_CONFIG_ALLOW_CROSS=1 && \ |
| 29 | + cargo build --release && \ |
| 30 | + rm -rf src/ |
| 31 | + |
| 32 | +# Set environment variables for the final build |
22 | 33 | ENV RUSTFLAGS="-Ctarget-feature=-crt-static"
|
23 | 34 | ENV PKG_CONFIG_ALLOW_CROSS=1
|
24 |
| -RUN cargo build --release && rm -rf src/ |
| 35 | +ARG UPTIME_CHECKER_GIT_REVISION |
| 36 | +ENV UPTIME_CHECKER_GIT_REVISION=$UPTIME_CHECKER_GIT_REVISION |
25 | 37 |
|
26 |
| -# Copy the source code and run the build again. This should only compile the |
27 |
| -# app itself as the dependencies were already built above. |
28 |
| -COPY . ./ |
29 |
| -RUN rm target/release/deps/uptime_checker* && cargo build --release |
| 38 | +# Copy the actual source code and build |
| 39 | +COPY . . |
| 40 | +RUN cargo build --release |
30 | 41 |
|
31 | 42 | FROM alpine:3.20
|
32 | 43 |
|
33 | 44 | COPY --from=builder /app/target/release/uptime-checker /usr/local/bin/uptime-checker
|
34 | 45 |
|
35 |
| -RUN apk add --no-cache tini libgcc |
| 46 | +RUN apk add --no-cache tini libgcc && \ |
| 47 | + addgroup -S app && \ |
| 48 | + adduser -S app -G app |
36 | 49 |
|
37 |
| -RUN addgroup -S app && adduser -S app -G app |
38 | 50 | USER app
|
39 | 51 |
|
40 | 52 | ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/uptime-checker"]
|
0 commit comments