|
1 | 1 | # syntax=docker/dockerfile:1 |
2 | 2 |
|
3 | | -FROM python:3.11-slim-bookworm as base |
| 3 | +FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim@sha256:74b8fe8ec5931f3930cfb6c87b46aeb1dbd497a609f6abf860fd0f4390f8b040 |
4 | 4 |
|
5 | | -ARG DEBIAN_FRONTEND=noninteractive |
| 5 | +WORKDIR /archivist |
6 | 6 |
|
7 | | -ENV PYTHONFAULTHANDLER=1 \ |
8 | | - PYTHONDONTWRITEBYTECODE=1 \ |
9 | | - PYTHONUNBUFFERED=1 \ |
10 | | - PYTHONHASHSEED=random \ |
11 | | - PIP_NO_CACHE_DIR=off \ |
12 | | - PIP_DISABLE_PIP_VERSION_CHECK=on \ |
13 | | - PIP_DEFAULT_TIMEOUT=100 |
| 7 | +# Enable bytecode compilation |
| 8 | +ENV UV_COMPILE_BYTECODE=1 |
14 | 9 |
|
15 | | -WORKDIR /archivist |
| 10 | +# Copy from the cache instead of linking since it's a mounted volume |
| 11 | +ENV UV_LINK_MODE=copy |
16 | 12 |
|
17 | 13 | # fetch container running user from build args |
18 | 14 | ARG ARCHIVIST_UID=1000 |
19 | 15 | ARG ARCHIVIST_GID=1000 |
20 | 16 |
|
21 | 17 | RUN <<EOF |
22 | | - apt-get update |
23 | | - apt-get install --no-install-recommends --yes curl git |
24 | 18 | # add archivist user that will be used to run container by default |
25 | 19 | groupadd --gid $ARCHIVIST_GID archivist |
26 | 20 | useradd --create-home --gid $ARCHIVIST_GID --uid $ARCHIVIST_UID archivist --no-log-init |
27 | | - # make archivist and owner of /opt/venv so it can write to it |
28 | | - mkdir -p /opt/venv |
29 | | - chown -R archivist:archivist /opt/venv /archivist |
30 | | -EOF |
31 | | - |
32 | | -ENV POETRY_VERSION=1.7.1 \ |
33 | | - POETRY_VIRTUAL_ENV=/opt/venv/poetry |
34 | | - |
35 | | -RUN --mount=type=cache,target=/root/.cache/pip/ <<EOF |
36 | | - # create virtual env for poetry |
37 | | - python -m venv $POETRY_VIRTUAL_ENV |
38 | | - # install poetry |
39 | | - $POETRY_VIRTUAL_ENV/bin/pip install poetry~=$POETRY_VERSION |
40 | | - # make poetry available system wide |
41 | | - ln -s $POETRY_VIRTUAL_ENV/bin/poetry /usr/local/bin/poetry |
| 21 | + chown -R archivist:archivist /archivist |
42 | 22 | EOF |
43 | 23 |
|
44 | 24 | USER archivist |
45 | 25 |
|
46 | | -# set archivist virtual env path and update PATH to register virtual env binaries |
47 | | -# this will ensure poetry will usie archivist virtual env when installing packages |
48 | | -ENV VIRTUAL_ENV=/opt/venv/archivist |
49 | | -ENV PATH=$VIRTUAL_ENV/bin:$PATH |
50 | | - |
51 | | -# create actual virtual env |
52 | | -RUN python -m venv $VIRTUAL_ENV |
53 | | - |
54 | | -COPY --chown=archivist:archivist poetry.lock pyproject.toml ./ |
55 | | - |
56 | | -# install non dev dependencies |
57 | | -RUN --mount=type=cache,uid=$ARCHIVIST_UID,gid=$ARCHIVIST_GID,target=/home/archivist/.cache/pypoetry/ \ |
58 | | - poetry install --without dev --no-interaction --no-ansi --no-root --sync |
| 26 | +# Install the project's dependencies using the lockfile and settings |
| 27 | +RUN --mount=type=cache,uid=$ARCHIVIST_UID,gid=$ARCHIVIST_GID,target=/home/archivist/.cache/uv \ |
| 28 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 29 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 30 | + uv sync --locked --no-install-project --no-dev |
59 | 31 |
|
60 | 32 | COPY --chown=archivist:archivist . . |
61 | 33 |
|
62 | | -############################## |
63 | | -# Production |
64 | | -############################## |
65 | | -FROM base as prod |
| 34 | +RUN --mount=type=cache,uid=$ARCHIVIST_UID,gid=$ARCHIVIST_GID,target=/home/archivist/.cache/uv \ |
| 35 | + uv sync --locked --no-dev |
66 | 36 |
|
67 | | -# install app as binary |
68 | | -RUN --mount=type=cache,uid=$ARCHIVIST_UID,gid=$ARCHIVIST_GID,target=/home/archivist/.cache/pypoetry/ \ |
69 | | - poetry install --without dev --no-interaction --no-ansi --sync |
| 37 | +ENV PATH="/archivist/.venv/bin:$PATH" |
70 | 38 |
|
71 | 39 | ENTRYPOINT ["archivist"] |
72 | 40 |
|
73 | 41 | CMD ["--help"] |
74 | | - |
75 | | - |
76 | | -############################## |
77 | | -# CI |
78 | | -############################## |
79 | | -FROM base as ci |
80 | | - |
81 | | -# install additional dev dependencies |
82 | | -RUN --mount=type=cache,uid=$ARCHIVIST_UID,gid=$ARCHIVIST_GID,target=/home/archivist/.cache/pypoetry/ \ |
83 | | - poetry install --no-interaction --no-ansi --sync |
84 | | - |
85 | | - |
86 | | - |
87 | | -############################## |
88 | | -# Development |
89 | | -############################## |
90 | | -FROM ci as dev |
91 | | - |
92 | | -USER root |
93 | | - |
94 | | -# Install development tools |
95 | | -RUN <<EOF |
96 | | - apt-get update |
97 | | - apt-get install --no-install-recommends --yes git curl gnupg ssh sudo vim |
98 | | - usermod --shell /usr/bin/bash archivist |
99 | | - echo 'archivist ALL=(root) NOPASSWD:ALL' > /etc/sudoers.d/archivist |
100 | | - chmod 0440 /etc/sudoers.d/archivist |
101 | | -EOF |
102 | | - |
103 | | -USER archivist |
0 commit comments