|
1 | | -# Start with an official Python image |
2 | | -FROM python:3.12-slim |
| 1 | +# Build stage using pixi |
| 2 | +FROM ghcr.io/prefix-dev/pixi:0.59.0 AS build |
3 | 3 |
|
4 | 4 | # Set environment variables |
5 | 5 | ENV PYTHONDONTWRITEBYTECODE=1 |
6 | 6 | ENV PYTHONUNBUFFERED=1 |
7 | 7 |
|
8 | | -# Create and set the working directory |
| 8 | +# Install git and ca-certificates (needed for git+https:// dependencies in pyproject.toml) |
| 9 | +RUN apt-get update && \ |
| 10 | + apt-get install -y --no-install-recommends git ca-certificates && \ |
| 11 | + rm -rf /var/lib/apt/lists/* |
| 12 | + |
| 13 | +# Copy source code, pixi.toml and pixi.lock to the container |
9 | 14 | WORKDIR /app |
| 15 | +COPY . . |
| 16 | + |
| 17 | +# Install dependencies using pixi (uses the default environment) |
| 18 | +RUN pixi install --locked |
| 19 | + |
| 20 | +# Create the shell-hook bash script to activate the environment |
| 21 | +RUN pixi shell-hook > /shell-hook.sh |
| 22 | + |
| 23 | +# Extend the shell-hook script to run the command passed to the container |
| 24 | +RUN echo 'exec "$@"' >> /shell-hook.sh |
| 25 | + |
| 26 | +# Production stage |
| 27 | +FROM ubuntu:24.04 AS production |
10 | 28 |
|
11 | | -# Install build dependencies |
| 29 | +# Set environment variables |
| 30 | +ENV PYTHONDONTWRITEBYTECODE=1 |
| 31 | +ENV PYTHONUNBUFFERED=1 |
| 32 | + |
| 33 | +# Install runtime dependencies (libpq for PostgreSQL) |
12 | 34 | RUN apt-get update && \ |
13 | | - apt-get install -y --no-install-recommends \ |
14 | | - git \ |
15 | | - libpq-dev \ |
16 | | - build-essential && \ |
| 35 | + apt-get install -y --no-install-recommends git libpq5 && \ |
17 | 36 | rm -rf /var/lib/apt/lists/* |
18 | | -# Install Python dependencies |
19 | | -COPY requirements.txt . |
20 | | -RUN pip install --upgrade pip && pip install -r requirements.txt |
21 | 37 |
|
22 | | -# Copy the rest of the application code |
23 | | -COPY . . |
| 38 | +# Copy the pixi environment from the build stage |
| 39 | +# Note: the prefix path must stay the same as in the build container |
| 40 | +COPY --from=build /app/.pixi/envs/default /app/.pixi/envs/default |
| 41 | +COPY --from=build /shell-hook.sh /shell-hook.sh |
| 42 | +COPY --from=build /app /app |
| 43 | +WORKDIR /app |
24 | 44 |
|
25 | 45 | # Expose the port |
26 | 46 | EXPOSE 8000 |
27 | 47 |
|
28 | | -COPY entrypoint.sh /entrypoint.sh |
29 | | -RUN chmod +x /entrypoint.sh |
30 | | -CMD ["/entrypoint.sh"] |
| 48 | +# Set the entrypoint to the shell-hook script (activates the environment and runs the command) |
| 49 | +ENTRYPOINT ["/bin/bash", "/shell-hook.sh"] |
| 50 | + |
| 51 | +# Run the application using gunicorn (supports OFFSETS_DB_WEB_CONCURRENCY env var) |
| 52 | +CMD ["sh", "-c", "gunicorn -w ${OFFSETS_DB_WEB_CONCURRENCY:-2} -t 600 -k uvicorn.workers.UvicornWorker offsets_db_api.main:app --config gunicorn_config.py --access-logfile - --error-logfile -"] |
0 commit comments