60 lines
No EOL
1.9 KiB
Docker
60 lines
No EOL
1.9 KiB
Docker
# Use an official Python runtime as a parent image
|
|
FROM python:3.11-slim
|
|
|
|
# Set the working directory in the container
|
|
WORKDIR /app
|
|
|
|
# Install system dependencies
|
|
RUN apt-get update && apt-get install -y \
|
|
curl \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Install Poetry
|
|
RUN pip install poetry
|
|
|
|
# Create a non-root user and group with proper home directory
|
|
RUN groupadd -r appuser && \
|
|
useradd -r -g appuser -d /home/appuser -m appuser && \
|
|
mkdir -p /home/appuser/.cache && \
|
|
chown -R appuser:appuser /home/appuser
|
|
|
|
# Copy the dependency files
|
|
COPY pyproject.toml poetry.lock ./
|
|
|
|
# Set proper Poetry configuration to use app directory
|
|
ENV POETRY_CACHE_DIR=/app/.poetry_cache
|
|
|
|
# Install project dependencies
|
|
# --no-root avoids installing the project itself, only dependencies
|
|
# --only main installs only the main dependencies (not dev)
|
|
RUN poetry install --no-root --only main && \
|
|
mkdir -p /app/.poetry_cache && \
|
|
chown -R appuser:appuser /app/.poetry_cache
|
|
|
|
# Copy the rest of the application code
|
|
COPY src/ ./src/
|
|
COPY entrypoint.sh ./
|
|
|
|
# Make the entrypoint script executable
|
|
RUN chmod +x ./entrypoint.sh
|
|
|
|
# Change ownership of the application files to the non-root user
|
|
RUN chown -R appuser:appuser /app
|
|
|
|
# Make port 5000 available to the world outside this container
|
|
EXPOSE 5000
|
|
|
|
# Switch to non-root user
|
|
USER appuser
|
|
|
|
# Configure health check
|
|
# Check every 30 seconds with 3 second timeout, 3 retries, and 5 second start period
|
|
HEALTHCHECK --interval=30s --timeout=3s --retries=3 --start-period=5s CMD curl -f http://localhost:5000/health || exit 1
|
|
|
|
# Set the entrypoint script
|
|
ENTRYPOINT ["./entrypoint.sh"]
|
|
|
|
# Define the command to run the application using Gunicorn
|
|
# This will be passed as arguments to the entrypoint script
|
|
# Use `poetry run` to execute the command within the Poetry environment
|
|
CMD ["poetry", "run", "gunicorn", "--bind", "0.0.0.0:5000", "src.anonchat:app"] |