|
1 | | -FROM python:3.12-bullseye AS python-base |
| 1 | +# Ideas from https://docs.astral.sh/uv/guides/integration/docker/ |
2 | 2 |
|
3 | | -# python |
4 | | -# ENV variables are also available in the later build stages |
5 | | -ENV PYTHONUNBUFFERED=1 \ |
6 | | - # prevents python creating .pyc files |
7 | | - PYTHONDONTWRITEBYTECODE=1 \ |
8 | | - \ |
9 | | - # pip |
10 | | - PIP_NO_CACHE_DIR=off \ |
11 | | - PIP_DISABLE_PIP_VERSION_CHECK=on \ |
12 | | - PIP_DEFAULT_TIMEOUT=100 \ |
13 | | - \ |
14 | | - # poetry |
15 | | - # https://python-poetry.org/docs/#installing-with-the-official-installer |
16 | | - # https://python-poetry.org/docs/configuration/#using-environment-variables |
17 | | - POETRY_VERSION=2.0.1 \ |
18 | | - # make poetry install to this location |
19 | | - POETRY_HOME="/opt/poetry" \ |
20 | | - # make poetry create the virtual environment in the project's root |
21 | | - # it gets named `.venv` |
22 | | - POETRY_VIRTUALENVS_IN_PROJECT=true \ |
23 | | - # do not ask any interactive question |
24 | | - POETRY_NO_INTERACTION=1 \ |
25 | | - \ |
26 | | - # paths |
27 | | - # this is where our requirements + virtual environment will live |
28 | | - PYSETUP_PATH="/opt/pysetup" \ |
29 | | - VENV_PATH="/opt/pysetup/.venv" \ |
30 | | - # needed for adit-radis-shared to be found |
31 | | - PYTHONPATH="/app" |
32 | | - |
33 | | -# prepend poetry and venv to path |
34 | | -ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" |
35 | | - |
36 | | -# deps for db management commands |
37 | | -# make sure to match the postgres version to the service in the compose file |
| 3 | +FROM python:3.12-bullseye AS builder-base |
| 4 | + |
| 5 | +# Install dependencies for the `psql` command. |
| 6 | +# Must match the version of the postgres service in the compose file! |
38 | 7 | RUN apt-get update \ |
39 | 8 | && apt-get install --no-install-recommends -y postgresql-common \ |
40 | 9 | && /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y \ |
41 | 10 | && apt-get install --no-install-recommends -y \ |
42 | 11 | postgresql-client-17 \ |
43 | 12 | && rm -rf /var/lib/apt/lists/* |
44 | 13 |
|
| 14 | +ENV PYTHONUNBUFFERED=1 \ |
| 15 | + PYTHONDONTWRITEBYTECODE=1 |
45 | 16 |
|
46 | | -# `builder-base` stage is used to build deps + create our virtual environment |
47 | | -FROM python-base AS builder-base |
| 17 | +COPY --from=ghcr.io/astral-sh/uv:0.6.0 /uv /uvx /bin/ |
48 | 18 |
|
49 | | -RUN apt-get update \ |
50 | | - && apt-get install --no-install-recommends -y \ |
51 | | - # deps for installing poetry |
52 | | - curl \ |
53 | | - # deps for building python deps |
54 | | - build-essential \ |
55 | | - && rm -rf /var/lib/apt/lists/* |
| 19 | +ENV UV_COMPILE_BYTECODE=1 \ |
| 20 | + UV_LINK_MODE=copy |
56 | 21 |
|
57 | | -# install poetry - respects $POETRY_VERSION & $POETRY_HOME |
58 | | -RUN curl -sSL https://install.python-poetry.org | python3 - |
| 22 | +# There is no git during image build so we need to provide a fake version |
| 23 | +ENV UV_DYNAMIC_VERSIONING_BYPASS=0.0.0 |
59 | 24 |
|
60 | | -# copy project requirement files here to ensure they will be cached. |
61 | | -WORKDIR $PYSETUP_PATH |
62 | | -COPY poetry.lock pyproject.toml README.md ./ |
| 25 | +ENV PATH="/app/.venv/bin:$PATH" |
63 | 26 |
|
64 | | -# install runtime deps - uses $POETRY_VIRTUALENVS_IN_PROJECT internally |
65 | | -RUN poetry install --without dev --no-root |
| 27 | +WORKDIR /app |
66 | 28 |
|
67 | 29 |
|
68 | | -# `development` image is used during development / testing |
69 | | -FROM python-base AS development |
| 30 | +# development image |
| 31 | +FROM builder-base AS development |
70 | 32 |
|
71 | | -WORKDIR $PYSETUP_PATH |
| 33 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 34 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 35 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 36 | + uv sync --frozen --no-install-project |
72 | 37 |
|
73 | | -# copy in our built poetry + venv |
74 | | -COPY --from=builder-base $POETRY_HOME $POETRY_HOME |
75 | | -COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH |
| 38 | +RUN playwright install --with-deps chromium |
76 | 39 |
|
77 | | -# quicker install as runtime deps are already installed |
78 | | -RUN poetry install --no-root |
| 40 | +ADD . /app |
79 | 41 |
|
80 | | -# Install requirements for end-to-end testing |
81 | | -RUN playwright install --with-deps chromium |
| 42 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 43 | + uv sync --frozen |
82 | 44 |
|
83 | | -# will become mountpoint of our code |
84 | | -WORKDIR /app |
85 | 45 |
|
| 46 | +# production image |
| 47 | +FROM builder-base AS production |
86 | 48 |
|
87 | | -# `production` image used for runtime |
88 | | -FROM python-base AS production |
89 | | -COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH |
90 | | -COPY . /app/ |
| 49 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 50 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 51 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 52 | + uv sync --frozen --no-install-project --no-dev |
91 | 53 |
|
92 | | -WORKDIR /app |
| 54 | +ADD . /app |
| 55 | + |
| 56 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 57 | + uv sync --frozen --no-dev |
0 commit comments