Skip to content

Commit 9e5654e

Browse files
Move to managing devcontainer environment outside of dockerfile
1 parent 5bbb867 commit 9e5654e

File tree

5 files changed

+60
-53
lines changed

5 files changed

+60
-53
lines changed

.devcontainer/devcontainer.json

Lines changed: 9 additions & 2 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",
4451
// After the container is created, install the uv env
45-
"postCreateCommand": "uv pip install $([ -f dev-requirements.txt ] && echo '-c dev-requirements.txt') -e '.[dev]' && pre-commit install"
52+
"postCreateCommand": "uv venv --seed && uv sync --locked && pre-commit install"
4653
}

Dockerfile

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +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.4
108

11-
# Install uv using the official installer script
12-
RUN curl -LsSf https://astral.sh/uv/install.sh | \
13-
env UV_INSTALL_DIR="/usr/local/bin" sh
14-
15-
# Configure environment
16-
ENV UV_CHECK_UPDATE=false
17-
18-
# Create virtual environment
19-
RUN uv venv --seed venv
20-
ENV VIRTUAL_ENV=/venv
21-
ENV PATH=$VIRTUAL_ENV/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/

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "python-copier-template"
77
dynamic = ["version"]
88
requires-python = ">=3.11"
99

10-
[project.optional-dependencies]
10+
[dependency-groups]
1111
dev = [
1212
"copier",
1313
"myst-parser",

template/Dockerfile.jinja

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,47 @@
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/*
10-
11-
# Install uv using the official installer script
12-
RUN curl -LsSf https://astral.sh/uv/install.sh | \
13-
env UV_INSTALL_DIR="/usr/local/bin" sh
6+
# Use this version of uv
7+
ARG UV_VERSION=0.7.4
148

15-
# Configure environment
16-
ENV UV_CHECK_UPDATE=false
9+
# Add any system dependencies for the developer/build environment here
10+
# RUN apt-get update && apt-get install -y --no-install-recommends \
11+
# graphviz
1712

18-
# Create virtual environment
19-
RUN uv venv --seed venv
20-
ENV VIRTUAL_ENV=/venv
21-
ENV PATH=$VIRTUAL_ENV/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/
2216

2317
# The build stage installs the context into the venv
2418
FROM developer AS build
2519
COPY . /context
2620
WORKDIR /context
2721

28-
# install dependencies and project into the local packages directory
29-
RUN touch dev-requirements.txt && uv pip install -c dev-requirements.txt .
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
3038

3139
# The runtime stage copies the built venv into a slim runtime container
3240
FROM python:${PYTHON_VERSION}-slim AS runtime
3341
# Add apt-get system dependecies for runtime here if needed
34-
COPY --from=build /venv/ .venv/
35-
ENV VIRTUAL_ENV=./.venv
36-
ENV PATH=$VIRTUAL_ENV/bin:$PATH
3742

38-
# 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
3948
ENTRYPOINT ["{{ repo_name }}"]
4049
CMD ["--version"]{% endif %}

uv.lock

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)