Files
conference-room-booking-system/Dockerfile-backend
2025-10-02 23:31:17 -04:00

65 lines
2.1 KiB
Plaintext

# Dockerfile for building and serving a Python FastAPI application using Poetry
# This Dockerfile uses a multi-stage build to keep the final image size small.
# It builds the application in a Python environment and serves it using Uvicorn.
# The User ID can be passed as a build argument to run the container as a non-root user.
ARG USER_ID=$(id -u)
# The Python version can be passed as a build argument.
# Default is set to Python 3.13.
ARG PYTHON_VERSION=3.13
# Builder stage
# This stage is used to install dependencies using Poetry and export them to a requirements.txt file.
FROM python:${PYTHON_VERSION}-slim AS builder
# Set the User ID for non-root user
USER ${USER_ID}
# Install Poetry in the builder stage
RUN pip install --no-cache-dir poetry
# Set working directory
WORKDIR /app
# Copy the Poetry configuration files
COPY backend/pyproject.toml backend/poetry.lock ./
# Add the poetry plugin for exporting dependencies
RUN poetry self add poetry-plugin-export
# Force the requirements.txt to be generated without cache
ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache
# Export dependencies to requirements.txt
# This will create a requirements.txt file with the dependencies listed in pyproject.toml
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
# Final stage
# Use a fresh Python image to keep the final image small
FROM python:${PYTHON_VERSION}-slim
# Set the User ID for non-root user
USER ${USER_ID}
# Set working directory
WORKDIR /app
# Copy the requirements.txt generated in the builder stage and install dependencies
COPY --from=builder /app/requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the project source code
COPY backend/src/ src/
# Copy the Alembic configuration and migrations
COPY backend/alembic.ini alembic.ini
COPY backend/migrations/ migrations/
# Copy the .env file
COPY .env .env
# Start fastapi application using uvicorn
ENV PYTHONPATH=/app/src
ENV BACKEND_PORT=${BACKEND_PORT}
CMD ["/bin/sh", "-c", "alembic upgrade head && uvicorn backend.main:app --host 0.0.0.0 --port $BACKEND_PORT --reload"]