-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
41 lines (30 loc) · 1.16 KB
/
Copy pathDockerfile
File metadata and controls
41 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# syntax=docker/dockerfile:1
# Use a slim Python base image for both builder and final stages
FROM python:3.11-slim AS base
# Builder stage: install dependencies into a virtual environment
FROM base AS builder
WORKDIR /app
# Copy only requirements files first for better cache utilization
COPY --link requirements*.txt ./
# Create virtual environment and install dependencies
RUN --mount=type=cache,target=/root/.cache/pip \
python -m venv .venv && \
.venv/bin/pip install --upgrade pip setuptools && \
if [ -f requirements.txt ]; then .venv/bin/pip install -r requirements.txt; fi
# Copy the rest of the application code
COPY --link . .
# Final stage: minimal image with only runtime needs
FROM base AS final
WORKDIR /app
# Create a non-root user and group
RUN addgroup --system appgroup && adduser --system --ingroup appgroup appuser
# Copy the virtual environment from builder
COPY --from=builder /app/.venv /app/.venv
# Copy application code
COPY --from=builder /app /app
# Set environment to use the venv
ENV PATH="/app/.venv/bin:$PATH"
# Use non-root user
USER appuser
# Default command (update as needed based on your app's entrypoint)
CMD ["python", "main.py"]