diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0e7671e --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/compose.yml b/compose.yml index 9c9bf2c..bba3f16 100644 --- a/compose.yml +++ b/compose.yml @@ -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 diff --git a/scraper/.dockerignore b/scraper/.dockerignore deleted file mode 100644 index ab2fe12..0000000 --- a/scraper/.dockerignore +++ /dev/null @@ -1,2 +0,0 @@ -.env -.gitignore \ No newline at end of file diff --git a/scraper/Dockerfile b/scraper/Dockerfile deleted file mode 100644 index 0121504..0000000 --- a/scraper/Dockerfile +++ /dev/null @@ -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 \ No newline at end of file diff --git a/scraper/compose.yml b/scraper/compose.yml deleted file mode 100644 index 711effb..0000000 --- a/scraper/compose.yml +++ /dev/null @@ -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: diff --git a/web-api/.dockerignore b/web-api/.dockerignore deleted file mode 100644 index 3dfba38..0000000 --- a/web-api/.dockerignore +++ /dev/null @@ -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 diff --git a/web-api/Dockerfile b/web-api/Dockerfile deleted file mode 100644 index b5eb280..0000000 --- a/web-api/Dockerfile +++ /dev/null @@ -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"] \ No newline at end of file diff --git a/web-api/compose.yml b/web-api/compose.yml deleted file mode 100644 index f746639..0000000 --- a/web-api/compose.yml +++ /dev/null @@ -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: - -