Skip to content

Commit de4c7c8

Browse files
Update Dockerfile and devcontainer to use uv
Update Dockerfile based on uv docs, include uv build in devcontainer, moving from settings from Dockerfile into devcontainer.json. … Co-authored-by: Oliver Copping <[email protected]>
1 parent fd80557 commit de4c7c8

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

.devcontainer/devcontainer.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
},
88
"remoteEnv": {
99
// Allow X11 apps to run inside the container
10-
"DISPLAY": "${localEnv:DISPLAY}"
10+
"DISPLAY": "${localEnv:DISPLAY}",
11+
"PATH": "${containerEnv:PATH}:/workspaces/${localWorkspaceFolderBasename}/.venv/bin"
1112
},
1213
"customizations": {
1314
"vscode": {
@@ -39,8 +40,14 @@
3940
// Make sure SELinux does not disable with access to host filesystems like tmp
4041
"--security-opt=label=disable"
4142
],
43+
"mounts": [
44+
{
45+
"target": "/workspaces/${localWorkspaceFolderBasename}/.venv",
46+
"type": "volume"
47+
}
48+
],
4249
// Mount the parent as /workspaces so we can pip install peers as editable
4350
"workspaceMount": "source=${localWorkspaceFolder}/..,target=/workspaces,type=bind",
44-
// After the container is created, install the python project in editable form
45-
"postCreateCommand": "pip install $([ -f dev-requirements.txt ] && echo '-c dev-requirements.txt') -e '.[dev]' && pre-commit install"
51+
// After the container is created, install the uv env
52+
"postCreateCommand": "uv sync && pre-commit install"
4653
}

Dockerfile

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
ARG PYTHON_VERSION=3.11
44
FROM python:${PYTHON_VERSION} AS developer
55

6-
# Add any system dependencies for the developer/build environment here
7-
RUN apt-get update && apt-get install -y --no-install-recommends \
8-
graphviz \
9-
&& rm -rf /var/lib/apt/lists/*
6+
# Use this version of uv
7+
ARG UV_VERSION=0.7
108

11-
# Set up a virtual environment and put it in PATH
12-
RUN python -m venv /venv
13-
ENV PATH=/venv/bin:$PATH
9+
# Install uv using the official image
10+
# See https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
11+
COPY --from=ghcr.io/astral-sh/uv:${UV_VERSION} /uv /uvx /bin/

template/Dockerfile.jinja

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,47 @@
33
ARG PYTHON_VERSION=3.11
44
FROM python:${PYTHON_VERSION} AS developer
55

6+
# Use this version of uv
7+
ARG UV_VERSION=0.7
8+
69
# Add any system dependencies for the developer/build environment here
7-
RUN apt-get update && apt-get install -y --no-install-recommends \
8-
graphviz \
9-
&& rm -rf /var/lib/apt/lists/*
10+
# RUN apt-get update && apt-get install -y --no-install-recommends \
11+
# graphviz
1012

11-
# Set up a virtual environment and put it in PATH
12-
RUN python -m venv /venv
13-
ENV PATH=/venv/bin:$PATH{% if docker %}
13+
# Install uv using the official image
14+
# See https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
15+
COPY --from=ghcr.io/astral-sh/uv:${UV_VERSION} /uv /uvx /bin/{% if docker %}
1416

1517
# The build stage installs the context into the venv
1618
FROM developer AS build
1719
COPY . /context
1820
WORKDIR /context
19-
RUN touch dev-requirements.txt && pip install -c dev-requirements.txt .
21+
22+
# Enable bytecode compilation and copy from the cache instead of linking
23+
# since it's a mounted volume
24+
ENV UV_COMPILE_BYTECODE=1
25+
ENV UV_LINK_MODE=copy
26+
27+
# Install the project's dependencies using the lockfile and settings
28+
RUN --mount=type=cache,target=/root/.cache/uv \
29+
--mount=type=bind,source=uv.lock,target=uv.lock \
30+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
31+
uv sync --locked --no-install-project --no-dev
32+
33+
# Then, add the rest of the project source code and install it
34+
# Installing separately from its dependencies allows optimal layer caching
35+
COPY . /context
36+
RUN --mount=type=cache,target=/root/.cache/uv \
37+
uv sync --locked --no-dev
2038

2139
# The runtime stage copies the built venv into a slim runtime container
2240
FROM python:${PYTHON_VERSION}-slim AS runtime
2341
# Add apt-get system dependecies for runtime here if needed
24-
COPY --from=build /venv/ /venv/
25-
ENV PATH=/venv/bin:$PATH
2642

27-
# change this entrypoint if it is not the same as the repo
43+
# We need to keep the venv at the same absolute path as in the build stage
44+
COPY --from=build /context/venv/ /context/venv/
45+
ENV PATH=/context/venv/bin:$PATH
46+
47+
# Change this entrypoint if it is not the same as the repo
2848
ENTRYPOINT ["{{ repo_name }}"]
2949
CMD ["--version"]{% endif %}

0 commit comments

Comments
 (0)