diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 01c976b..f7a9166 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -96,6 +96,18 @@ jobs: if: always() run: devcontainer-sh down --workspace-folder tests/fixtures/workspace + - name: Start a compose dev container + run: devcontainer-sh up --workspace-folder tests/fixtures/compose + + - name: Down removes every service and the network, not only the workspace + run: | + project=compose_devcontainer + services() { docker ps -aq --filter "label=com.docker.compose.project=$project" | wc -l | tr -d ' '; } + test "$(services)" = 2 + devcontainer-sh down --workspace-folder tests/fixtures/compose + test "$(services)" = 0 + test -z "$(docker network ls -q --filter "label=com.docker.compose.project=$project")" + release: needs: [lint, test, action] if: github.event_name == 'push' diff --git a/README.md b/README.md index 15405e3..c863bf6 100644 --- a/README.md +++ b/README.md @@ -126,10 +126,15 @@ devcontainer-sh build -p linux/amd64 # --platform linux/amd64 **devcontainer-sh down** — Stop and remove the container associated with the current workspace. Resolves the same `devcontainer.local_folder` label the CLI infers from `--workspace-folder`, then runs `docker rm -f`. +A compose-based dev container goes as a whole: the CLI labels only the workspace container, so `down` reads the compose project from it and runs `docker compose -p down --remove-orphans`. Every service and the project's network are removed. Named volumes are kept, since they may hold data; pass `-v` to remove them too. + ```bash -devcontainer-sh down +devcontainer-sh down # keeps named volumes +devcontainer-sh down -v # also removes them ``` +If the same compose file is also run outside the dev container under the same project name, `down` stops that stack too. + ## Configuration ### Workspace auto-detection diff --git a/devcontainer-sh b/devcontainer-sh index 0ed3f6c..54dceef 100755 --- a/devcontainer-sh +++ b/devcontainer-sh @@ -164,8 +164,29 @@ cmd_shell() { exec devcontainer exec "${passthru[@]}" sh -c "exec ${picker} -l" } +# list_add LIST ITEM +# +# Print the newline-separated LIST with ITEM appended, unless it is already +# there. (bash 3.2 has no associative arrays to dedupe with.) +list_add() { + case " +$1 +" in + *" +$2 +"*) printf '%s' "$1" ;; + *) + if [ -z "$1" ]; then + printf '%s' "$2" + else + printf '%s\n%s' "$1" "$2" + fi + ;; + esac +} + cmd_down() { - local ws="" arg + local ws="" volumes=0 arg while [ $# -gt 0 ]; do arg="$1" case "$arg" in @@ -178,9 +199,20 @@ cmd_down() { ws="${arg#--workspace-folder=}" shift ;; + -v | --volumes) + volumes=1 + shift + ;; -h | --help) - printf 'Usage: devcontainer-sh down [--workspace-folder PATH]\n' - printf 'Stops and removes the dev container associated with the workspace.\n' + cat <<'EOF' +Usage: devcontainer-sh down [--workspace-folder PATH] [-v|--volumes] + +Stops and removes the dev container associated with the workspace. + +A compose-based dev container is removed as a whole: every service of its +compose project and the project's network go, not only the workspace service. +Named volumes are kept unless -v/--volumes is given. +EOF return 0 ;; *) shift ;; @@ -202,7 +234,41 @@ EOF [ "${#ids[@]}" -gt 0 ] || die "down: no container found for label ${label}" - exec docker rm -f "${ids[@]}" + # The CLI puts the devcontainer.local_folder label on the workspace container + # only. The other services of a compose dev container are found through the + # compose project the workspace container belongs to. + local project projects="" plain="" + for id in "${ids[@]}"; do + project="$(docker inspect --format '{{index .Config.Labels "com.docker.compose.project"}}' "$id" 2>/dev/null)" || project="" + if [ -n "$project" ]; then + projects="$(list_add "$projects" "$project")" + else + plain="$(list_add "$plain" "$id")" + fi + done + + local status=0 + local -a compose_args + while IFS= read -r project; do + [ -n "$project" ] || continue + compose_args=(-p "$project" down --remove-orphans) + [ "$volumes" = 0 ] || compose_args+=(--volumes) + docker compose "${compose_args[@]}" || status=$? + done < bash > sh, or pass a shell name) exec [args] Run a one-off command in the container build [opts] Build the container image (-n no-cache, -p PLATFORM) - down Stop and remove the container for this workspace + down [-v] Stop and remove the container for this workspace + (a compose dev container goes as a whole; -v also + removes its named volumes) help Show this help version Show wrapper and devcontainer CLI versions diff --git a/tests/down.bats b/tests/down.bats index 9166bab..b1188cb 100644 --- a/tests/down.bats +++ b/tests/down.bats @@ -46,3 +46,43 @@ teardown() { [ "$status" -eq 0 ] [[ "$output" == *"Usage: devcontainer-sh down"* ]] } + +@test "down on a compose dev container removes the whole compose project" { + DEVCONTAINER_SH_TEST_DOCKER_PS_OUT="abc123" DEVCONTAINER_SH_TEST_COMPOSE_PROJECT="myproj" \ + run devcontainer-sh down + [ "$status" -eq 0 ] + [[ "$output" == *"docker compose -p myproj down --remove-orphans"* ]] + [[ "$output" != *"docker rm"* ]] +} + +@test "down keeps named volumes unless -v is given" { + DEVCONTAINER_SH_TEST_DOCKER_PS_OUT="abc123" DEVCONTAINER_SH_TEST_COMPOSE_PROJECT="myproj" \ + run devcontainer-sh down + [[ "$output" != *"--volumes"* ]] +} + +@test "down -v also removes the compose volumes" { + DEVCONTAINER_SH_TEST_DOCKER_PS_OUT="abc123" DEVCONTAINER_SH_TEST_COMPOSE_PROJECT="myproj" \ + run devcontainer-sh down -v + [ "$status" -eq 0 ] + [[ "$output" == *"docker compose -p myproj down --remove-orphans --volumes"* ]] +} + +@test "down brings a compose project down once however many containers match" { + DEVCONTAINER_SH_TEST_DOCKER_PS_OUT=$'abc123\ndef456' DEVCONTAINER_SH_TEST_COMPOSE_PROJECT="myproj" \ + run devcontainer-sh down + [ "$status" -eq 0 ] + [ "$(printf '%s\n' "$output" | grep -c 'docker compose')" -eq 1 ] +} + +@test "down fails when docker compose fails" { + DEVCONTAINER_SH_TEST_DOCKER_PS_OUT="abc123" DEVCONTAINER_SH_TEST_COMPOSE_PROJECT="myproj" \ + DEVCONTAINER_SH_TEST_COMPOSE_EXIT=3 run devcontainer-sh down + [ "$status" -eq 3 ] +} + +@test "down --help mentions compose and volumes" { + run devcontainer-sh down --help + [[ "$output" == *"compose"* ]] + [[ "$output" == *"--volumes"* ]] +} diff --git a/tests/fixtures/compose/.devcontainer/devcontainer.json b/tests/fixtures/compose/.devcontainer/devcontainer.json new file mode 100644 index 0000000..426eb1e --- /dev/null +++ b/tests/fixtures/compose/.devcontainer/devcontainer.json @@ -0,0 +1,6 @@ +{ + "name": "devcontainer-sh-compose-fixture", + "dockerComposeFile": "docker-compose.yml", + "service": "workspace", + "workspaceFolder": "/home/vscode" +} diff --git a/tests/fixtures/compose/.devcontainer/docker-compose.yml b/tests/fixtures/compose/.devcontainer/docker-compose.yml new file mode 100644 index 0000000..9cb4818 --- /dev/null +++ b/tests/fixtures/compose/.devcontainer/docker-compose.yml @@ -0,0 +1,7 @@ +services: + workspace: + image: mcr.microsoft.com/devcontainers/base:bookworm + command: sleep infinity + sidecar: + image: busybox:stable + command: sleep infinity diff --git a/tests/helpers.bash b/tests/helpers.bash index 1d7a3b1..358c4a1 100644 --- a/tests/helpers.bash +++ b/tests/helpers.bash @@ -15,8 +15,10 @@ STUB cat > "$STUB_DIR/docker" <<'STUB' #!/bin/sh case "$1" in - ps) echo "${DEVCONTAINER_SH_TEST_DOCKER_PS_OUT-}" ;; - *) echo "docker $*" ;; + ps) echo "${DEVCONTAINER_SH_TEST_DOCKER_PS_OUT-}" ;; + inspect) echo "${DEVCONTAINER_SH_TEST_COMPOSE_PROJECT-}" ;; + compose) echo "docker $*"; exit "${DEVCONTAINER_SH_TEST_COMPOSE_EXIT-0}" ;; + *) echo "docker $*" ;; esac STUB chmod +x "$STUB_DIR/devcontainer" "$STUB_DIR/docker"