-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dockerfile.prod
79 lines (63 loc) · 2.51 KB
/
Dockerfile.prod
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# While optional this tells the Docker builder of the version
# syntax=docker/dockerfile:1
#
ARG PYTHON_VERSION=3.12-bookworm
# This Dockerfile uses a multi stage build to slim down the image
# https://docs.docker.com/develop/develop-images/multistage-build/
#
# Portion of this is adapted from
# https://bit.ly/3Vw9B2m
#
# Base image for Python applications
# This image is particularly for a web server using uvicorn
FROM --platform=linux/amd64 python:${PYTHON_VERSION} as requirements-stage
# Update the based image to latest versions of packages
# python 3.10 seems to want python3-tk installed
RUN apt-get update \
&& apt-get -y upgrade \
&& apt-get install -y --no-install-recommends gcc python3-dev build-essential libpq-dev python3-tk \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Work in the temporary directory for the build phase
WORKDIR /tmp
# Copy the files in the src directory which is the app package
# and the dependency matrix dedescribed by pyproject.toml
COPY src/pyproject.toml src/poetry.lock* /tmp/
# Ask poetry to install all packages including the app
# not in virtual machine as we are in a container
RUN pip3 install --upgrade pip
RUN pip3 install poetry
RUN poetry config virtualenvs.create false
RUN poetry install --no-dev --no-root
# Generate the requirements.txt file.
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
### Stage 2
FROM python:${PYTHON_VERSION}
ARG USER={PROJ_NAME}
ARG GROUP={PROJ_NAME}
ARG UID=1000
ARG GID=1000
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONFAULTHANDLER=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
# Update the based image to latest versions of packages
# python 3.10 seems to want python3-tk installed
RUN apt-get update \
&& apt-get -y upgrade \
&& apt-get install -y --no-install-recommends libpq-dev python3-tk postgresql-client\
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY --from=requirements-stage /tmp/requirements.txt /opt/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /opt/requirements.txt
RUN groupadd --gid "${GID}" "${GROUP}"
RUN useradd --uid "${UID}" --gid "${GID}" --create-home "${USER}"
USER "${USER}"
WORKDIR /opt/${PROJ_NAME}
COPY ./src/. .
# Expose ports which is proxied via traefik
EXPOSE 80
# Labels are used to identify the image
LABEL org.opencontainers.image.source="https://github.com/anomaly/${PROJ_NAME}"
LABEL org.opencontainers.image.description="A Python web server using FastAPI and Uvicorn"
LABEL org.opencontainers.image.licenses="Apache-2.0"