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
16189setup_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).
34207setup_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
0 commit comments