decrease docker image size by using alpine

This commit is contained in:
Moritz Hölting 2026-01-05 23:42:30 +01:00
parent bd22aeba67
commit 975193ff65
8 changed files with 73 additions and 152 deletions

69
Dockerfile Normal file
View File

@ -0,0 +1,69 @@
# -----------------------------
# Chef base
# -----------------------------
FROM rust:alpine AS chef
# SQLx offline mode
ENV SQLX_OFFLINE true
# Alpine build dependencies
RUN apk add --no-cache curl bash musl-dev openssl-dev pkgconfig
# Install cargo-chef
RUN curl -L --proto '=https' --tlsv1.2 -sSf \
https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
RUN cargo binstall cargo-chef -y
WORKDIR /app
# -----------------------------
# Planner
# -----------------------------
FROM chef AS planner
COPY . .
RUN OFFLINE=true cargo chef prepare --recipe-path recipe.json
# -----------------------------
# Builder
# -----------------------------
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN OFFLINE=true cargo build --release \
--bin mensa-upb-api \
--bin mensa-upb-scraper
# =====================================================
# Runtime image: scraper (cron-based)
# =====================================================
FROM alpine:latest AS scraper-runtime
WORKDIR /app
RUN apk add --no-cache ca-certificates tzdata dcron tini
RUN echo "0 0 * * * /app/mensa-upb-scraper >> /var/log/cron.log 2>&1" \
> /etc/crontabs/root && \
touch /var/log/cron.log
COPY --from=builder /app/target/release/mensa-upb-scraper /app/mensa-upb-scraper
ENTRYPOINT ["/sbin/tini", "--"]
CMD sh -c 'env > /etc/environment && crond -l 2 && tail -f /var/log/cron.log'
# =====================================================
# Runtime image: API
# =====================================================
FROM alpine:latest AS api-runtime
ARG UID=10001
RUN adduser -D -H -u "${UID}" appuser
USER appuser
COPY --from=builder /app/target/release/mensa-upb-api /bin/mensa-upb-api
ENV API_INTERFACE=0.0.0.0
EXPOSE 8080
CMD ["/bin/mensa-upb-api"]

View File

@ -2,7 +2,8 @@ services:
api:
build:
context: .
dockerfile: ./web-api/Dockerfile
dockerfile: ./Dockerfile
target: api-runtime
image: mensa-upb-api:latest
ports:
- 8080:8080
@ -16,7 +17,8 @@ services:
scraper:
build:
context: .
dockerfile: ./scraper/Dockerfile
dockerfile: ./Dockerfile
target: scraper-runtime
image: mensa-upb-scraper:latest
environment:
- DATABASE_URL=postgres://pguser:pgpass@postgres-mensa-upb/postgres

View File

@ -1,2 +0,0 @@
.env
.gitignore

View File

@ -1,29 +0,0 @@
FROM rust:latest AS chef
RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
RUN cargo binstall cargo-chef -y
WORKDIR /app
FROM chef AS planner
COPY . .
RUN OFFLINE=true cargo chef prepare --bin mensa-upb-scraper --recipe-path recipe.json
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --bin mensa-upb-scraper --release --recipe-path recipe.json
COPY . .
RUN OFFLINE=true cargo build --bin mensa-upb-scraper --release
FROM debian:bookworm-slim AS runtime
WORKDIR /app
RUN apt-get update -y && \
apt-get install -y ca-certificates cron
RUN echo "0 0 * * * /app/mensa-upb-scraper >> /var/log/cron.log 2>&1" > /etc/cron.d/mensa_upb_scraper
RUN chmod 0644 /etc/cron.d/mensa_upb_scraper
RUN crontab /etc/cron.d/mensa_upb_scraper
RUN touch /var/log/cron.log
COPY --from=builder /app/target/release/mensa-upb-scraper /app/mensa-upb-scraper
CMD env > /etc/environment && cron && tail -f /var/log/cron.log

View File

@ -1,23 +0,0 @@
services:
scraper:
build: .
image: mensa-upb-scraper:latest
environment:
- DATABASE_URL=postgres://pguser:pgpass@postgres-mensa-upb-scraper/postgres
- "RUST_LOG=none,mensa_upb_scraper=info"
- TZ=Europe/Berlin
depends_on:
- postgres
postgres:
container_name: postgres-mensa-upb-scraper
image: postgres:17-alpine
environment:
- POSTGRES_USER=pguser
- POSTGRES_PASSWORD=pgpass
- POSTGRES_DB=postgres
volumes:
- db:/var/lib/postgresql/data
volumes:
db:

View File

@ -1,32 +0,0 @@
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
**/.DS_Store
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/secrets.dev.yaml
**/values.dev.yaml
/bin
/target
LICENSE
README.md

View File

@ -1,37 +0,0 @@
FROM rust:latest AS chef
RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
RUN cargo binstall cargo-chef -y
WORKDIR /app
FROM chef AS planner
COPY . .
RUN OFFLINE=true cargo chef prepare --bin mensa-upb-api --recipe-path recipe.json
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --bin mensa-upb-api --release --recipe-path recipe.json
COPY . .
RUN OFFLINE=true cargo build --bin mensa-upb-api --release
FROM debian:bookworm-slim AS runtime
ARG UID=10001
RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser
COPY --from=builder /app/target/release/mensa-upb-api /bin/mensa-upb-api
ENV API_INTERFACE=0.0.0.0
EXPOSE 8080
# What the container should run when it is started.
CMD ["/bin/mensa-upb-api"]

View File

@ -1,27 +0,0 @@
services:
api:
build: .
image: mensa-upb-api:latest
ports:
- 8080:8080
environment:
- DATABASE_URL=postgres://pguser:pgpass@postgres-mensa-upb-api/postgres
- "RUST_LOG=none,mensa_upb_api=info"
- TZ=Europe/Berlin
depends_on:
- postgres
postgres:
container_name: postgres-mensa-upb-api
image: postgres:17-alpine
environment:
- POSTGRES_USER=pguser
- POSTGRES_PASSWORD=pgpass
- POSTGRES_DB=postgres
volumes:
- db:/var/lib/postgresql/data
volumes:
db: