Skip to content

Commit d8841ee

Browse files
committed
Port dev containers to CSCS rootless runtimes
1 parent 8a3012b commit d8841ee

26 files changed

Lines changed: 668 additions & 68 deletions

File tree

‎brev/dev-build.bash‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /bin/bash
22

3-
# This script builds Docker containers for tutorials.
3+
# This script builds containers for tutorials.
44
#
55
# Usage:
66
# ./dev-build.bash [--no-cache] [<tutorial-name>]
@@ -29,6 +29,9 @@ done
2929
SCRIPT_PATH=$(cd $(dirname ${0}); pwd -P)
3030
REPO_ROOT=$(cd ${SCRIPT_PATH}/..; pwd -P)
3131

32+
source "${SCRIPT_PATH}/dev-common.bash"
33+
setup_container_engine
34+
3235
# Function to build a single tutorial
3336
build_tutorial() {
3437
local ACH_TUTORIAL_PATH=${1}
@@ -55,7 +58,11 @@ build_tutorial() {
5558
echo "Dockerfile generated successfully"
5659
fi
5760

58-
docker compose --progress=plain -f "${ACH_TUTORIAL_PATH}/brev/docker-compose.yml" build ${NO_CACHE}
61+
if [ "${ACH_CONTAINER_ENGINE}" = "docker" ]; then
62+
compose --progress=plain -f "${ACH_TUTORIAL_PATH}/brev/docker-compose.yml" build ${NO_CACHE}
63+
else
64+
compose -f "${ACH_TUTORIAL_PATH}/brev/docker-compose.yml" build ${NO_CACHE}
65+
fi
5966

6067
echo "Successfully built image for ${ACH_TUTORIAL}"
6168
echo ""

‎brev/dev-common.bash‎

Lines changed: 187 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,191 @@
11
#! /bin/bash
22
#
3-
# Helper functions for setting up development mounts with Docker's --user flag.
3+
# Helper functions for setting up development mounts with container --user flag.
44
#
55
# This library provides functions for:
6-
# - Exporting HOST_UID/HOST_GID for Docker to match host user permissions
7-
# - Creating Docker volumes (bind-mount to local repo or plain image-backed)
6+
# - Exporting HOST_UID/HOST_GID for the container engine to match host user permissions
7+
# - Creating container volumes (bind-mount to local repo or plain image-backed)
88
#
99
# Usage:
1010
# source ./brev/dev-common.bash
1111
# setup_dev_env "/path/to/repo"
1212
# setup_docker_volume "tutorial-name" true # bind-mount local repo
1313
# setup_docker_volume "tutorial-name" false # image content only
1414

15-
# Export host user's UID/GID/username for Docker Compose
15+
16+
# Select the local container engine. Docker remains the default for Brev and CI,
17+
# while ACH_CONTAINER_ENGINE=podman uses rootless Podman where available (CSCS).
18+
setup_container_engine() {
19+
if [ -n "${ACH_CONTAINER_ENGINE_CMD:-}" ] && [ -n "${ACH_COMPOSE_CMD:-}" ]; then
20+
return 0
21+
fi
22+
23+
local requested_engine="${ACH_CONTAINER_ENGINE:-auto}"
24+
25+
if [ "${requested_engine}" = "auto" ]; then
26+
if command -v docker >/dev/null 2>&1 && docker info >/dev/null 2>&1; then
27+
requested_engine="docker"
28+
elif command -v podman >/dev/null 2>&1; then
29+
requested_engine="podman"
30+
else
31+
echo "Error: neither Docker nor Podman is available" >&2
32+
return 1
33+
fi
34+
fi
35+
36+
case "${requested_engine}" in
37+
docker)
38+
if ! command -v docker >/dev/null 2>&1; then
39+
echo "Error: Docker is not installed or not in PATH" >&2
40+
return 1
41+
fi
42+
export ACH_CONTAINER_ENGINE="docker"
43+
export ACH_CONTAINER_ENGINE_CMD="docker"
44+
export ACH_COMPOSE_CMD="docker compose"
45+
;;
46+
podman)
47+
if ! command -v podman >/dev/null 2>&1; then
48+
echo "Error: Podman is not installed or not in PATH" >&2
49+
return 1
50+
fi
51+
if command -v podman-compose >/dev/null 2>&1; then
52+
export ACH_COMPOSE_CMD="podman-compose"
53+
elif podman compose version >/dev/null 2>&1; then
54+
export ACH_COMPOSE_CMD="podman compose"
55+
elif command -v docker-compose >/dev/null 2>&1; then
56+
export DOCKER_HOST="${DOCKER_HOST:-unix://${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/podman/podman.sock}"
57+
export ACH_COMPOSE_CMD="docker-compose"
58+
else
59+
echo "Error: Podman is available, but no Compose frontend was found" >&2
60+
echo "Install podman-compose or a podman compose provider." >&2
61+
return 1
62+
fi
63+
export ACH_CONTAINER_ENGINE="podman"
64+
export ACH_CONTAINER_ENGINE_CMD="podman"
65+
;;
66+
*)
67+
echo "Error: unsupported ACH_CONTAINER_ENGINE='${requested_engine}' (expected auto, docker, or podman)" >&2
68+
return 1
69+
;;
70+
esac
71+
72+
echo "🔧 Container engine: ${ACH_CONTAINER_ENGINE} (${ACH_COMPOSE_CMD})" >&2
73+
}
74+
75+
container() {
76+
setup_container_engine
77+
${ACH_CONTAINER_ENGINE_CMD} "$@"
78+
}
79+
80+
compose() {
81+
setup_container_engine
82+
${ACH_COMPOSE_CMD} "$@"
83+
}
84+
85+
prepare_compose_file() {
86+
local compose_file=${1}
87+
88+
setup_container_engine
89+
90+
if [ "${ACH_CONTAINER_ENGINE}" != "podman" ]; then
91+
echo "${compose_file}"
92+
return 0
93+
fi
94+
95+
local output_file="${TMPDIR:-/tmp}/$(basename "${compose_file}").podman.$$"
96+
python3 - "${compose_file}" "${output_file}" "${ACH_REPO_ROOT:-}" "${ACH_PODMAN_BIND_REPO:-}" <<'PY'
97+
import glob
98+
import os
99+
import sys
100+
import yaml
101+
102+
source, destination, repo_root, bind_repo = sys.argv[1:5]
103+
with open(source, "r", encoding="utf-8") as handle:
104+
data = yaml.safe_load(handle)
105+
106+
def strip_podman_incompatible(value):
107+
if isinstance(value, dict):
108+
for key in ("privileged", "ulimits", "deploy"):
109+
value.pop(key, None)
110+
for child in value.values():
111+
strip_podman_incompatible(child)
112+
elif isinstance(value, list):
113+
for child in value:
114+
strip_podman_incompatible(child)
115+
116+
strip_podman_incompatible(data)
117+
118+
nvidia_devices = [path for path in (
119+
"/dev/nvidia0",
120+
"/dev/nvidiactl",
121+
"/dev/nvidia-uvm",
122+
"/dev/nvidia-uvm-tools",
123+
) if os.path.exists(path)]
124+
nvidia_mounts = []
125+
for pattern in (
126+
"/usr/bin/nvidia-smi",
127+
"/usr/lib/x86_64-linux-gnu/libcuda.so.*",
128+
"/usr/lib/x86_64-linux-gnu/libnvidia-ml.so.*",
129+
"/usr/lib/x86_64-linux-gnu/libnvidia-ptxjitcompiler.so.*",
130+
"/usr/lib/x86_64-linux-gnu/libnvidia-nvvm.so.*",
131+
):
132+
for path in glob.glob(pattern):
133+
if os.path.isfile(path) and not os.path.islink(path):
134+
nvidia_mounts.append(f"{path}:{path}:ro")
135+
136+
if bind_repo == "1" and repo_root:
137+
volume_name = "accelerated-computing-hub"
138+
bind_mount = f"{repo_root}:/accelerated-computing-hub:U"
139+
services = data.get("services") or {}
140+
for service in services.values():
141+
volumes = service.get("volumes") or []
142+
service["volumes"] = [bind_mount if item == f"{volume_name}:/accelerated-computing-hub" else item for item in volumes]
143+
environment = service.get("environment") or {}
144+
if isinstance(environment, dict):
145+
environment.setdefault("ACH_RUNTIME_DIR", "/tmp/accelerated-computing-hub-runtime")
146+
environment["ACH_USER"] = "root"
147+
environment["ACH_UID"] = "0"
148+
environment["ACH_GID"] = "0"
149+
service["environment"] = environment
150+
volumes = data.get("volumes")
151+
if isinstance(volumes, dict):
152+
volumes.pop(volume_name, None)
153+
services = data.get("services") or {}
154+
for service_name, service in services.items():
155+
if service_name in ("base", "jupyter", "nsys", "ncu") and nvidia_devices:
156+
devices = service.get("devices") or []
157+
for device in nvidia_devices:
158+
if device not in devices:
159+
devices.append(device)
160+
service["devices"] = devices
161+
162+
volumes = service.get("volumes") or []
163+
for mount in nvidia_mounts:
164+
if mount not in volumes:
165+
volumes.append(mount)
166+
service["volumes"] = volumes
167+
168+
environment = service.get("environment") or {}
169+
if isinstance(environment, dict):
170+
environment.setdefault("LD_LIBRARY_PATH", "/usr/lib/x86_64-linux-gnu")
171+
environment["ACH_ROOTLESS_PODMAN"] = "1"
172+
service["environment"] = environment
173+
174+
command = service.get("command")
175+
entrypoint = service.get("entrypoint") or []
176+
service_arg = entrypoint[1] if isinstance(entrypoint, list) and len(entrypoint) > 1 else service_name
177+
service["entrypoint"] = ["/accelerated-computing-hub/brev/entrypoint-podman-gpu.bash", service_arg]
178+
if command is not None:
179+
service["command"] = command
180+
else:
181+
service.pop("command", None)
182+
with open(destination, "w", encoding="utf-8") as handle:
183+
yaml.safe_dump(data, handle, default_flow_style=False, sort_keys=False)
184+
PY
185+
echo "${output_file}"
186+
}
187+
188+
# Export host user's UID/GID/username for Docker/Podman Compose
16189
setup_dev_env() {
17190
local REPO_ROOT=${1}
18191

@@ -25,31 +198,33 @@ setup_dev_env() {
25198
echo ""
26199
}
27200

28-
# Set up the Docker volume for a tutorial.
201+
# Set up the container volume for a tutorial.
29202
#
30203
# Always removes any existing volume first for a clean state.
31204
# When MOUNT=true, creates a bind-mount volume pointing to the local repo.
32-
# When MOUNT=false, does nothing (docker compose will create a plain volume
205+
# When MOUNT=false, does nothing (compose will create a plain volume
33206
# populated from the image on first use).
34207
setup_docker_volume() {
35208
local ACH_TUTORIAL=${1}
36209
local MOUNT=${2:-true}
37210
local VOLUME_NAME="${ACH_TUTORIAL}_accelerated-computing-hub"
38211

212+
setup_container_engine
213+
39214
# Remove existing volume for clean state
40-
if docker volume inspect "${VOLUME_NAME}" &>/dev/null; then
41-
echo "🗑️ Removing existing Docker volume: ${VOLUME_NAME}"
42-
docker volume rm "${VOLUME_NAME}" &>/dev/null || true
215+
if ${ACH_CONTAINER_ENGINE_CMD} volume inspect "${VOLUME_NAME}" &>/dev/null; then
216+
echo "🗑️ Removing existing container volume: ${VOLUME_NAME}"
217+
${ACH_CONTAINER_ENGINE_CMD} volume rm "${VOLUME_NAME}" &>/dev/null || true
43218
fi
44219

45220
if [ "${MOUNT}" = "true" ]; then
46-
echo "🔧 Creating Docker volume (bind mount to local repo)..."
47-
docker volume create --driver local \
221+
echo "🔧 Creating container volume (bind mount to local repo)..."
222+
${ACH_CONTAINER_ENGINE_CMD} volume create --driver local \
48223
--opt type=none \
49224
--opt o=bind \
50225
--opt device="${ACH_REPO_ROOT}" \
51226
"${ACH_TUTORIAL}_accelerated-computing-hub" > /dev/null
52-
echo -e "${GREEN:-}✅ Docker volume created (local mount)${NC:-}"
227+
echo -e "${GREEN:-}✅ container volume created (local mount)${NC:-}"
53228
else
54229
echo "📦 Using image content (no local mount)"
55230
fi

‎brev/dev-shell.bash‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /bin/bash
22

3-
# This script starts an interactive shell in a Docker container for a tutorial.
3+
# This script starts an interactive shell in a container for a tutorial.
44
#
55
# Usage:
66
# ./brev/dev-shell.bash [--mount|--no-mount] <tutorial-name|docker-compose-file> <service-name>
@@ -29,7 +29,7 @@ usage() {
2929
cat << EOF
3030
Usage: $(basename "$0") [--mount|--no-mount] <tutorial-name|docker-compose-file> <service-name>
3131
32-
Start an interactive shell in a Docker container for a tutorial.
32+
Start an interactive shell in a container for a tutorial.
3333
3434
Options:
3535
--mount Bind-mount local repo into the container (default)
@@ -46,7 +46,7 @@ Examples:
4646
$(basename "$0") tutorials/accelerated-python/brev/docker-compose.yml jupyter
4747
4848
Requirements:
49-
- Docker and Docker Compose must be installed
49+
- Docker Compose or Podman Compose must be installed
5050
EOF
5151
exit 1
5252
}
@@ -101,13 +101,14 @@ fi
101101

102102
# Validate docker-compose file exists
103103
if [ ! -f "${COMPOSE_FILE}" ]; then
104-
echo -e "${RED}Error: Docker Compose file not found: ${COMPOSE_FILE}${NC}"
104+
echo -e "${RED}Error: Docker/Podman Compose file not found: ${COMPOSE_FILE}${NC}"
105105
exit 1
106106
fi
107+
COMPOSE_FILE=$(prepare_compose_file "${COMPOSE_FILE}")
107108

108109
echo "================================================================================"
109110
echo "Starting interactive shell for: ${ACH_TUTORIAL} (service: ${SERVICE})"
110-
echo "Docker Compose file: ${COMPOSE_FILE}"
111+
echo "Docker/Podman Compose file: ${COMPOSE_FILE}"
111112
echo "================================================================================"
112113
echo ""
113114

@@ -119,7 +120,7 @@ echo "🚀 Starting interactive shell as ${ACH_USER}..."
119120
echo " (Type 'exit' or press Ctrl+D to exit the shell)"
120121
echo ""
121122

122-
docker compose -f "${COMPOSE_FILE}" run --rm -it \
123+
compose -f "${COMPOSE_FILE}" run --rm -it \
123124
--entrypoint "/accelerated-computing-hub/brev/entrypoint.bash" \
124125
"${SERVICE}" shell
125126

‎brev/dev-start.bash‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /bin/bash
22

3-
# This script starts Docker containers for a tutorial.
3+
# This script starts containers for a tutorial.
44
#
55
# Usage:
66
# ./dev-start.bash [--mount|--no-mount] <tutorial-name>
@@ -64,11 +64,13 @@ setup_docker_volume "${ACH_TUTORIAL}" "${MOUNT}"
6464
echo "Starting tutorial: ${ACH_TUTORIAL}"
6565
cd ${REPO_ROOT}
6666

67+
DOCKER_COMPOSE=$(prepare_compose_file "${DOCKER_COMPOSE}")
68+
6769
# Create a modified docker-compose file that binds to 0.0.0.0 instead of 127.0.0.1
6870
# This is needed for local development so services are accessible from outside the container
6971
sed 's/127\.0\.0\.1:/0.0.0.0:/g' "${DOCKER_COMPOSE}" > "${DOCKER_COMPOSE_DEV}"
7072

7173
# Filter out the "volume already exists" warning while preserving all other warnings/errors on stderr
72-
docker compose -f ${DOCKER_COMPOSE_DEV} up -d 2> >(grep -v "already exists but was not created by Docker Compose" >&2)
74+
compose -f ${DOCKER_COMPOSE_DEV} up -d 2> >(grep -v "already exists but was not created by Docker/Podman Compose" >&2)
7375

7476
echo "Tutorial ${ACH_TUTORIAL} started successfully!"

‎brev/dev-stop.bash‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#! /bin/bash
22

3-
# This script stops Docker containers for a tutorial.
3+
# This script stops containers for a tutorial.
44
#
55
# Usage:
66
# ./dev-stop.bash <tutorial-name>
@@ -13,6 +13,9 @@ set -eu
1313
SCRIPT_PATH=$(cd $(dirname ${0}); pwd -P)
1414
REPO_ROOT=$(cd ${SCRIPT_PATH}/..; pwd -P)
1515

16+
source "${SCRIPT_PATH}/dev-common.bash"
17+
setup_container_engine
18+
1619
# Check argument
1720
if [ $# -ne 1 ]; then
1821
echo "Error: Tutorial name is required"
@@ -39,6 +42,6 @@ fi
3942

4043
echo "Stopping tutorial: ${ACH_TUTORIAL}"
4144
cd ${REPO_ROOT}
42-
docker compose -f ${DOCKER_COMPOSE} down
45+
compose -f ${DOCKER_COMPOSE} down
4346

4447
echo "Tutorial ${ACH_TUTORIAL} stopped successfully!"

0 commit comments

Comments
 (0)