-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDockerfile
More file actions
78 lines (61 loc) · 2.54 KB
/
Dockerfile
File metadata and controls
78 lines (61 loc) · 2.54 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
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
# This Dockerfile has three stages:
#
# base-image
# Updates the base Python image with security patches and common system
# packages. This image becomes the base of all other images.
# install-image
# Installs third-party dependencies into a virtual environment and
# installs the application into /app. This directory will be copied
# across build stages.
# runtime-image
# - Copies the virtual environment into place.
# - Runs as a non-root user.
# - Sets up the entrypoint and port.
FROM python:3.14.3-slim-trixie AS base-image
# Update system packages
COPY scripts/install-base-packages.sh .
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
./install-base-packages.sh && rm ./install-base-packages.sh
FROM base-image AS install-image
# Install uv.
COPY --from=ghcr.io/astral-sh/uv:0.11.6 /uv /bin/uv
# Install some additional packages required for building dependencies.
COPY scripts/install-dependency-packages.sh .
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
./install-dependency-packages.sh
# Disable hard links during uv package installation since we're using a
# cache on a separate file system.
ENV UV_LINK_MODE=copy
# Force use of system Python so that the Python version is controlled by
# the Docker base image version, not by whatever uv decides to install.
ENV UV_PYTHON_PREFERENCE=only-system
# Install the dependencies.
WORKDIR /app
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-default-groups --compile-bytecode --no-install-project
# Install the Gafaelfawr Python application.
ADD . /app
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-default-groups --compile-bytecode --no-editable
FROM base-image AS runtime-image
# Create a non-root user
RUN useradd --create-home appuser
# Copy the virtualenv, Alembic configuration, and startup script.
COPY --from=install-image /app/.venv /app/.venv
COPY --from=install-image /app/alembic /app/alembic
COPY --from=install-image /app/alembic.ini /app/alembic.ini
COPY --from=install-image /app/scripts/start.sh /app/scripts/start.sh
# Set the working directory.
WORKDIR /app
# Switch to the non-root user.
USER appuser
# Expose the port.
EXPOSE 8080
# Make sure we use the uv virtualenv.
ENV PATH="/app/.venv/bin:$PATH"
# Run the application.
CMD ["/app/scripts/start.sh"]