Optimized Docker build process.

This commit is contained in:
2025-03-31 08:17:04 +03:00
parent e18511c7c4
commit 78dda6afdb

View File

@@ -1,30 +1,25 @@
# FROM python:3.12.9-alpine3.21
FROM python:3.12-slim FROM python:3.12-slim
# Set environment variables # Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONDONTWRITEBYTECODE=1 \
ENV PYTHONUNBUFFERED=1 PYTHONUNBUFFERED=1
# Set the working directory inside the container # Set the working directory inside the container
WORKDIR /app WORKDIR /app
# Install system dependencies required for MySQL and other libraries # Install system dependencies required for MySQL and other libraries
RUN apt-get update && apt-get install -y git RUN RUN apt-get update && apt-get install -y --no-install-recommends git && \
# default-libmysqlclient-dev \ rm -rf /var/lib/apt/lists/*
# build-essential \
# && rm -rf /var/lib/apt/lists/*
RUN mkdir files/ # Create a new non-root user and switch to it
COPY files/requirements.txt files/requirements.txt RUN groupadd --system appuser && useradd --system --create-home --gid appuser appuser
COPY files/requirements.txt /tmp/requirements.txt
RUN sleep 5 RUN sleep 5
# Install Python dependencies # Install Python dependencies
RUN pip install --upgrade pip RUN pip install --upgrade pip && pip install --no-cache-dir -r /tmp/requirements.txt
RUN pip install --no-cache-dir -r files/requirements.txt
# Expose port 8080 for the FastAPI application
# EXPOSE 8080
COPY . . COPY . .
@@ -32,6 +27,11 @@ RUN rm files/db.sqlite -f
RUN /usr/local/bin/alembic -c alembic/alembic.ini upgrade head RUN /usr/local/bin/alembic -c alembic/alembic.ini upgrade head
# Command to run the FastAPI application # Change ownership to the new user
# CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8080"] RUN chown -R appuser:appuser /app
# Switch to the new non-root user
USER appuser
CMD ["bash", "/app/scripts/run.sh"] CMD ["bash", "/app/scripts/run.sh"]