Skip to content

Commit 0cb0f1f

Browse files
kurowskiclaude
andcommitted
fix: make devcontainer-reset-db more robust for multiple projects
Detect compose project automatically from current container's Docker labels rather than guessing from workspace folder name. This allows the script to work correctly when multiple devcontainers are running on the same machine. The script now: - Inspects the current container's compose project label - Finds the mariadb container using compose project and service labels - Falls back to workspace-based detection if labels aren't available - Shows better error messages with a table of available containers Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent a73dc53 commit 0cb0f1f

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

local/etc/uceap.d/devcontainer_reset_db.sh

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
function devcontainer_reset_db() {
2-
# Determine the compose project name from the workspace folder
3-
# Use WORKSPACE_FOLDER env var if set by devcontainer, otherwise derive from pwd
4-
if [ -n "$WORKSPACE_FOLDER" ]; then
5-
WORKSPACE_NAME=$(basename "$WORKSPACE_FOLDER")
6-
else
7-
WORKSPACE_NAME=$(basename "$(pwd)")
2+
# Detect the compose project by inspecting the current container
3+
CURRENT_CONTAINER=$(hostname)
4+
echo "Running from container: $CURRENT_CONTAINER"
5+
6+
# Get the compose project name from the current container's labels
7+
COMPOSE_PROJECT=$(docker inspect "$CURRENT_CONTAINER" --format '{{index .Config.Labels "com.docker.compose.project"}}' 2>/dev/null)
8+
9+
if [ -z "$COMPOSE_PROJECT" ]; then
10+
echo "Warning: Could not detect compose project from container labels"
11+
echo "Falling back to workspace-based detection..."
12+
# Fallback to the old method
13+
if [ -n "$WORKSPACE_FOLDER" ]; then
14+
WORKSPACE_NAME=$(basename "$WORKSPACE_FOLDER")
15+
else
16+
WORKSPACE_NAME=$(basename "$(pwd)")
17+
fi
18+
COMPOSE_PROJECT="${COMPOSE_PROJECT_NAME:-${WORKSPACE_NAME}_devcontainer}"
819
fi
9-
COMPOSE_PROJECT="${COMPOSE_PROJECT_NAME:-${WORKSPACE_NAME}_devcontainer}"
1020

1121
echo "Using compose project: $COMPOSE_PROJECT"
1222

13-
# Get the container name
14-
CONTAINER_NAME="${COMPOSE_PROJECT}-mariadb-1"
23+
# Find the mariadb container in the same compose project
24+
CONTAINER_NAME=$(docker ps -a --filter "label=com.docker.compose.project=$COMPOSE_PROJECT" --filter "label=com.docker.compose.service=mariadb" --format "{{.Names}}" | head -n 1)
1525

16-
# Verify the container exists
17-
if ! docker inspect "$CONTAINER_NAME" &>/dev/null; then
18-
echo "Error: Container $CONTAINER_NAME not found"
26+
if [ -z "$CONTAINER_NAME" ]; then
27+
echo "Error: Could not find mariadb container for project '$COMPOSE_PROJECT'"
1928
echo "Available mariadb containers:"
20-
docker ps -a --filter "name=mariadb" --format "{{.Names}}"
29+
docker ps -a --filter "name=mariadb" --format "table {{.Names}}\t{{.Label \"com.docker.compose.project\"}}"
2130
return 1
2231
fi
2332

33+
echo "Found container: $CONTAINER_NAME"
34+
2435
# Get the volume name from the mariadb container before stopping it
2536
VOLUME_NAME=$(docker inspect "$CONTAINER_NAME" --format '{{range .Mounts}}{{if eq .Destination "/var/lib/mysql"}}{{.Name}}{{end}}{{end}}')
2637

@@ -29,7 +40,6 @@ function devcontainer_reset_db() {
2940
return 1
3041
fi
3142

32-
echo "Found container: $CONTAINER_NAME"
3343
echo "Found volume: $VOLUME_NAME"
3444

3545
# Stop and remove the container

0 commit comments

Comments
 (0)