-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathDockerfile
60 lines (47 loc) · 1.87 KB
/
Dockerfile
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Define a base stage with a Debian Bookworm base image that includes the latest glibc update
FROM python:3.12-bookworm as base
# Set environment variables
ENV PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1 \
PIP_NO_CACHE_DIR=true \
PIP_DEFAULT_TIMEOUT=100 \
PIP_DISABLE_PIP_VERSION_CHECK=on \
QR_CODE_DIR=/myapp/qr_codes
WORKDIR /myapp
# Update system and specifically upgrade libc-bin to the required security patch version
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
&& apt-get install -y libc-bin=2.36-9+deb12u7 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install Python dependencies in /.venv
COPY requirements.txt .
RUN python -m venv /.venv \
&& . /.venv/bin/activate \
&& pip install --upgrade pip \
&& pip install -r requirements.txt
# Define a second stage for the runtime, using the same Debian Bookworm slim image
FROM python:3.12-slim-bookworm as final
# Upgrade libc-bin in the final stage to ensure security patch is applied
RUN apt-get update && apt-get install -y libc-bin=2.36-9+deb12u7 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Copy the virtual environment from the base stage
COPY --from=base /.venv /.venv
# Set environment variable to ensure all python commands run inside the virtual environment
ENV PATH="/.venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1 \
QR_CODE_DIR=/myapp/qr_codes
# Set the working directory
WORKDIR /myapp
# Create and switch to a non-root user
RUN useradd -m myuser
USER myuser
# Copy application code with appropriate ownership
COPY --chown=myuser:myuser . .
# Inform Docker that the container listens on the specified port at runtime.
EXPOSE 8000
# Use ENTRYPOINT to specify the executable when the container starts.
ENTRYPOINT ["uvicorn", "app.main:app", "--reload", "--host", "0.0.0.0", "--port", "8000"]