Dockerfile
· 1.2 KiB · Docker
Raw
# Build stage
FROM python:3.12-slim as builder
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# Create and activate virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Copy and install requirements
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Final stage
FROM python:3.12-slim
# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Create a non-root user
RUN useradd -m -u 1000 appuser
# Set working directory
WORKDIR /app
# Copy only necessary files
COPY --chown=appuser:appuser main.py .
COPY --chown=appuser:appuser config ./config/
COPY --chown=appuser:appuser entrypoint.sh .
# Create necessary directories
RUN mkdir -p /app/data /app/storage /app/config && \
chown -R appuser:appuser /app && \
mkdir -p /app/default_config && \
cp /app/config/* /app/default_config/ && \
chown -R appuser:appuser /app/default_config && \
chmod +x entrypoint.sh
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Expose port
EXPOSE 8501
ENTRYPOINT ["./entrypoint.sh"]
1 | # Build stage |
2 | FROM python:3.12-slim as builder |
3 | |
4 | # Install build dependencies |
5 | RUN apt-get update && apt-get install -y --no-install-recommends \ |
6 | build-essential \ |
7 | && rm -rf /var/lib/apt/lists/* |
8 | |
9 | # Create and activate virtual environment |
10 | RUN python -m venv /opt/venv |
11 | ENV PATH="/opt/venv/bin:$PATH" |
12 | |
13 | # Copy and install requirements |
14 | COPY requirements.txt . |
15 | RUN pip install --no-cache-dir -r requirements.txt |
16 | |
17 | # Final stage |
18 | FROM python:3.12-slim |
19 | |
20 | # Copy virtual environment from builder |
21 | COPY --from=builder /opt/venv /opt/venv |
22 | ENV PATH="/opt/venv/bin:$PATH" |
23 | |
24 | # Create a non-root user |
25 | RUN useradd -m -u 1000 appuser |
26 | |
27 | # Set working directory |
28 | WORKDIR /app |
29 | |
30 | # Copy only necessary files |
31 | COPY --chown=appuser:appuser main.py . |
32 | COPY --chown=appuser:appuser config ./config/ |
33 | COPY --chown=appuser:appuser entrypoint.sh . |
34 | |
35 | # Create necessary directories |
36 | RUN mkdir -p /app/data /app/storage /app/config && \ |
37 | chown -R appuser:appuser /app && \ |
38 | mkdir -p /app/default_config && \ |
39 | cp /app/config/* /app/default_config/ && \ |
40 | chown -R appuser:appuser /app/default_config && \ |
41 | chmod +x entrypoint.sh |
42 | |
43 | RUN apt-get clean && \ |
44 | rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* |
45 | |
46 | # Expose port |
47 | EXPOSE 8501 |
48 | |
49 | ENTRYPOINT ["./entrypoint.sh"] |