Skip to content

Commit b8b3f90

Browse files
committed
Convert to use dls ubuntu-devcontainer
1 parent d2dc4c2 commit b8b3f90

File tree

3 files changed

+63
-85
lines changed

3 files changed

+63
-85
lines changed

.devcontainer/devcontainer.json

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,28 @@
88
"remoteEnv": {
99
// Allow X11 apps to run inside the container
1010
"DISPLAY": "${localEnv:DISPLAY}",
11-
"PATH": "${containerEnv:PATH}:/workspaces/${localWorkspaceFolderBasename}/.venv/bin"
11+
// We put venv on a different volume to cache so have to copy
12+
"UV_LINK_MODE": "copy",
13+
// Do the equivalent of "activate" the venv so we don't have to "uv run" everything
14+
"PATH": "/workspaces/${localWorkspaceFolderBasename}/.venv/bin:${localEnv:PATH}"
1215
},
1316
"customizations": {
1417
"vscode": {
1518
// Set *default* container specific settings.json values on container create.
1619
"settings": {
17-
"python.defaultInterpreterPath": "/venv/bin/python"
20+
"python.defaultInterpreterPath": "/workspaces/${localWorkspaceFolderBasename}/.venv/bin/python",
21+
"python.terminal.activateEnvInCurrentTerminal": false,
22+
"python.terminal.activateEnvironment": false,
23+
"python.testing.unittestEnabled": false,
24+
"python.testing.pytestEnabled": true,
25+
"editor.formatOnSave": true,
26+
"editor.codeActionsOnSave": {
27+
"source.organizeImports": "explicit"
28+
},
29+
"remote.autoForwardPorts": false,
30+
"[python]": {
31+
"editor.defaultFormatter": "charliermarsh.ruff"
32+
}
1833
},
1934
// Add the IDs of extensions you want installed when the container is created.
2035
"extensions": [
@@ -28,26 +43,37 @@
2843
]
2944
}
3045
},
31-
"features": {
32-
// add in eternal history and other bash features
33-
"ghcr.io/diamondlightsource/devcontainer-features/bash-config:1": {}
34-
},
35-
// Create the config folder for the bash-config feature
36-
"initializeCommand": "mkdir -p ${localEnv:HOME}/.config/bash-config",
46+
// Create the config folder for the bash-config feature and uv cache
47+
"initializeCommand": "mkdir -p ${localEnv:HOME}/.config/terminal-config ${localEnv:HOME}/.cache/uv",
3748
"runArgs": [
3849
// Allow the container to access the host X11 display and EPICS CA
3950
"--net=host",
4051
// Make sure SELinux does not disable with access to host filesystems like tmp
4152
"--security-opt=label=disable"
4253
],
54+
// Populate the user's terminal config folder with default config if it is blank
55+
"onCreateCommand": "/root/terminal-config/ensure-user-terminal-config.sh",
4356
"mounts": [
57+
// Mount in the user terminal config folder so it can be edited
58+
{
59+
"source": "${localEnv:HOME}/.config/terminal-config",
60+
"target": "/user-terminal-config",
61+
"type": "bind"
62+
},
63+
// Keep a persistent cross container cache for uv
64+
{
65+
"source": "${localEnv:HOME}/.cache/uv",
66+
"target": "/root/.cache/uv",
67+
"type": "bind"
68+
},
69+
// Use a volume mount for the uv venv so it is local to the container
4470
{
4571
"target": "/workspaces/${localWorkspaceFolderBasename}/.venv",
4672
"type": "volume"
4773
}
4874
],
4975
// Mount the parent as /workspaces so we can pip install peers as editable
5076
"workspaceMount": "source=${localWorkspaceFolder}/..,target=/workspaces,type=bind",
51-
// After the container is created, install the uv env
52-
"postCreateCommand": "uv sync && pre-commit install"
77+
// After the container is created, install the python project in editable form
78+
"postCreateCommand": "uv sync && uv run pre-commit install"
5379
}

Dockerfile

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
1-
# Use this version of Python
2-
ARG PYTHON_VERSION=3.11
3-
# Use this version of uv
4-
ARG UV_VERSION=0.7
5-
6-
# Install uv using the official image
7-
# See https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
8-
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv-distroless
9-
10-
# The devcontainer should use the developer target and run as root with podman
11-
# or docker with user namespaces.
12-
FROM python:${PYTHON_VERSION} AS developer
13-
14-
# Add any system dependencies for the developer/build environment here
15-
# RUN apt-get update && apt-get install -y --no-install-recommends \
16-
# graphviz
17-
18-
# Install from uv image
19-
COPY --from=uv-distroless /uv /uvx /bin/
1+
# The developer stage is used as a devcontainer including dev versions
2+
# of the build dependencies
3+
FROM ghcr.io/diamondlightsource/ubuntu-devcontainer:noble AS developer
4+
RUN apt-get update -y && apt-get install -y --no-install-recommends \
5+
libevent-dev \
6+
libreadline-dev

template/Dockerfile.jinja

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,15 @@
1-
# Use this version of Python
2-
ARG PYTHON_VERSION=3.11
3-
# Use this version of uv
4-
ARG UV_VERSION=0.7
5-
6-
# Install uv using the official image
7-
# See https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
8-
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv-distroless
9-
10-
# The devcontainer should use the developer target and run as root with podman
11-
# or docker with user namespaces.
12-
FROM python:${PYTHON_VERSION} AS developer
13-
14-
# Add any system dependencies for the developer/build environment here
15-
# RUN apt-get update && apt-get install -y --no-install-recommends \
16-
# graphviz
17-
18-
# Install from uv image
19-
COPY --from=uv-distroless /uv /uvx /bin/{% if docker %}
20-
21-
# The build stage installs the context into the venv
1+
# The developer stage is used as a devcontainer including dev versions
2+
# of the build dependencies
3+
FROM ghcr.io/diamondlightsource/ubuntu-devcontainer:noble AS developer
4+
RUN apt-get update -y && apt-get install -y --no-install-recommends \
5+
libevent-dev \
6+
libreadline-dev{% if docker %}
7+
8+
# The build stage makes some assets using the developer tools
229
FROM developer AS build
2310
# Copy only dependency files first
24-
COPY pyproject.toml uv.lock /context/
25-
WORKDIR /context
26-
27-
# Enable bytecode compilation and copy from the cache instead of linking
28-
# since it's a mounted volume
29-
ENV UV_COMPILE_BYTECODE=1
30-
ENV UV_LINK_MODE=copy
11+
COPY pyproject.toml uv.lock /assets/
12+
WORKDIR /assets
3113

3214
# Install the project's dependencies using the lockfile and settings
3315
RUN --mount=type=cache,target=/root/.cache/uv \
@@ -37,39 +19,22 @@ RUN --mount=type=cache,target=/root/.cache/uv \
3719

3820
# Then, add the rest of the project source code and install it
3921
# Installing separately from its dependencies allows optimal layer caching
40-
COPY . /context
22+
COPY . /assets/
4123
RUN --mount=type=cache,target=/root/.cache/uv \
4224
uv sync --locked --no-dev
4325

44-
{% if docker_debug %}
45-
FROM build AS debug
46-
47-
{% if git_platform=="github.com" %}
48-
# Set origin to use ssh
49-
RUN git remote set-url origin [email protected]:{{github_org}}/{{repo_name}}.git
50-
{% endif %}
51-
52-
# For this pod to understand finding user information from LDAP
53-
RUN apt update
54-
RUN DEBIAN_FRONTEND=noninteractive apt install libnss-ldapd -y
55-
RUN sed -i 's/files/ldap files/g' /etc/nsswitch.conf
56-
57-
# Make editable and debuggable
58-
RUN pip install debugpy
59-
RUN pip install -e .
60-
61-
# Alternate entrypoint to allow devcontainer to attach
62-
ENTRYPOINT [ "/bin/bash", "-c", "--" ]
63-
CMD [ "while true; do sleep 30; done;" ]
64-
65-
{% endif %}
66-
# The runtime stage copies the built venv into a slim runtime container
67-
FROM python:${PYTHON_VERSION}-slim AS runtime
68-
# Add apt-get system dependecies for runtime here if needed
26+
# The runtime stage installs runtime deps then copies in built assets
27+
# This time we remove the apt lists to save disk space
28+
FROM ubuntu:noble as runtime
29+
RUN apt-get update -y && apt-get install -y --no-install-recommends \
30+
libevent \
31+
libreadline \
32+
&& rm -rf /var/lib/apt/lists/*
33+
COPY --from=build /assets /
6934

7035
# We need to keep the venv at the same absolute path as in the build stage
71-
COPY --from=build /context/.venv/ /context/.venv/
72-
ENV PATH=/context/.venv/bin:$PATH
36+
COPY --from=build /assets/.venv/ .venv/
37+
ENV PATH=.venv/bin:$PATH
7338

7439
# Change this entrypoint if it is not the same as the repo
7540
ENTRYPOINT ["{{ repo_name }}"]

0 commit comments

Comments
 (0)