Improve Dockerfile to eliminate root user warnings and use virtual environment for dependencies
This commit is contained in:
parent
7333cd3d9a
commit
271fddc26e
35
Dockerfile
35
Dockerfile
@ -11,27 +11,41 @@ RUN apt-get update && apt-get install -y \
|
|||||||
gcc \
|
gcc \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Copy requirements and install Python dependencies
|
# Create a non-root user and group for building
|
||||||
|
RUN groupadd --gid 1001 builder && \
|
||||||
|
useradd --uid 1001 --gid builder --shell /bin/bash --create-home builder
|
||||||
|
|
||||||
|
# Switch to builder user for dependency installation
|
||||||
|
USER builder
|
||||||
|
WORKDIR /home/builder
|
||||||
|
|
||||||
|
# Copy requirements and install Python dependencies in a virtual environment
|
||||||
COPY src/server/requirements.txt .
|
COPY src/server/requirements.txt .
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
RUN python -m venv /home/builder/venv && \
|
||||||
|
/home/builder/venv/bin/pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
# Production stage
|
# Production stage
|
||||||
FROM python:3.9-slim
|
FROM python:3.9-slim
|
||||||
|
|
||||||
|
# Create a non-root user and group
|
||||||
|
RUN groupadd --gid 1001 appuser && \
|
||||||
|
useradd --uid 1001 --gid appuser --shell /bin/bash --create-home appuser
|
||||||
|
|
||||||
# Set working directory
|
# Set working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy Python dependencies from builder stage
|
# Copy Python dependencies from builder stage (virtual environment)
|
||||||
COPY --from=builder /usr/local/lib/python3.9/site-packages /usr/local/lib/python3.9/site-packages
|
COPY --from=builder /home/builder/venv /app/venv
|
||||||
|
|
||||||
# Copy application code
|
# Copy application code
|
||||||
COPY src/server/ .
|
COPY --chown=appuser:appuser src/server/ .
|
||||||
|
|
||||||
# Copy website files
|
# Copy website files
|
||||||
COPY src/website/ website/
|
COPY --chown=appuser:appuser src/website/ website/
|
||||||
|
|
||||||
# Create directory for cache if it doesn't exist
|
# Create directory for cache if it doesn't exist
|
||||||
RUN mkdir -p website/cache
|
RUN mkdir -p website/cache && \
|
||||||
|
chown -R appuser:appuser website/cache
|
||||||
|
|
||||||
# Expose port
|
# Expose port
|
||||||
EXPOSE 6006
|
EXPOSE 6006
|
||||||
@ -39,10 +53,15 @@ EXPOSE 6006
|
|||||||
# Set environment variables
|
# Set environment variables
|
||||||
ENV FLASK_APP=app.py
|
ENV FLASK_APP=app.py
|
||||||
ENV FLASK_ENV=production
|
ENV FLASK_ENV=production
|
||||||
|
ENV USER=appuser
|
||||||
|
ENV PATH=/app/venv/bin:$PATH
|
||||||
|
|
||||||
|
# Switch to non-root user
|
||||||
|
USER appuser
|
||||||
|
|
||||||
# Health check
|
# Health check
|
||||||
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
||||||
CMD curl -f http://localhost:6006/api || exit 1
|
CMD curl -f http://localhost:6006/api || exit 1
|
||||||
|
|
||||||
# Run the application
|
# Run the application
|
||||||
CMD ["python", "app.py"]
|
CMD ["python", "app.py"]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user