Docker Compose環境を安全に一括削除する方法(特定プロジェクトだけ掃除)

Safely Clean Up a Single Docker Compose Project

* 本ページはプロモーションが含まれています

GitHub上のリポジトリをクローンして、一時的にDocker環境を立ち上げて試す機会は多いと思います。
しかし、テスト後に不要となったコンテナやボリュームを個別に削除するのは手間がかかります。

docker system prune などのコマンドも便利ですが、これを使うとテスト用以外の稼働していないコンテナやボリュームまで削除されてしまい、困ったことが何度かありました。
そこで、特定プロジェクトのDocker環境だけをまとめて安全に削除できるスクリプトを作成しました。

Docker 環境を一掃する方法 

プロジェクトで起動した Docker のコンテナ・ボリューム・イメージをまとめて削除するスクリプト docker-clean.sh の使い方を紹介します。


スクリプトの準備 

#!/usr/bin/env bash
# docker-clean.sh - Remove all Docker containers, volumes, and images for a compose project
# Usage: ./docker-clean.sh [compose-file...] [--all]
#   --all  : Also remove images built/pulled by the compose project
# If no compose files are specified, auto-detects docker-compose.yml and docker-compose.*.yml

set -euo pipefail

RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m'

REMOVE_IMAGES=false
COMPOSE_FILES=()

for arg in "$@"; do
  case "$arg" in
    --all) REMOVE_IMAGES=true ;;
    *) COMPOSE_FILES+=("$arg") ;;
  esac
done

# Auto-detect compose files if none specified
if [ ${#COMPOSE_FILES[@]} -eq 0 ]; then
  while IFS= read -r f; do
    COMPOSE_FILES+=("$f")
  done < <(find . -maxdepth 1 -name "docker-compose*.yml" -o -name "docker-compose*.yaml" 2>/dev/null | sort)
fi

if [ ${#COMPOSE_FILES[@]} -eq 0 ]; then
  echo -e "${RED}Error: No docker-compose files found.${NC}"
  exit 1
fi

echo -e "${YELLOW}Found compose files:${NC}"
for f in "${COMPOSE_FILES[@]}"; do echo "  $f"; done
echo ""

# Confirm
read -r -p "$(echo -e "${RED}WARNING${NC}: This will delete all containers and volumes. Continue? [y/N] ")" confirm
[[ "$confirm" =~ ^[Yy]$ ]] || { echo "Aborted."; exit 0; }

# Build -f flags for docker compose
COMPOSE_ARGS=()
for f in "${COMPOSE_FILES[@]}"; do
  COMPOSE_ARGS+=("-f" "$f")
done

RMI_FLAG=""
if $REMOVE_IMAGES; then
  RMI_FLAG="--rmi all"
fi

echo ""
echo -e "${YELLOW}Stopping and removing containers, networks, and volumes...${NC}"
# shellcheck disable=SC2086
docker compose "${COMPOSE_ARGS[@]}" down --volumes $RMI_FLAG

echo ""
echo -e "${GREEN}Done! All containers and volumes have been removed.${NC}"
if $REMOVE_IMAGES; then
  echo -e "${GREEN}Images were also removed.${NC}"
else
  echo -e "${YELLOW}Tip: Run with --all to also remove images.${NC}"
fi

1. 実行権限を付与する 

chmod +x docker-clean.sh

基本的な使い方 

コンテナとボリュームを削除する 

./docker-clean.sh

カレントディレクトリにある docker-compose*.yml / docker-compose*.yaml を自動検出して処理します。 実行前に確認プロンプトが表示されるため、誤実行を防げます。

Found compose files:
  ./docker-compose.yml
  ./docker-compose.dev.yml

WARNING: This will delete all containers and volumes. Continue? [y/N]

イメージも一緒に削除する(-all) 

./docker-clean.sh --all
  • -all オプションを付けると、コンテナ・ボリュームに加えて、compose で使用したイメージもすべて削除します。

compose ファイルを明示的に指定する 

./docker-clean.sh docker-compose.yml docker-compose.dev.yml

処理対象の compose ファイルを手動で指定することもできます。--all との組み合わせも可能です。

./docker-clean.sh docker-compose.yml docker-compose.dev.yml --all

オプション一覧 

オプション説明
(なし)コンテナ・ネットワーク・ボリュームを削除
--all上記に加えてイメージも削除
<ファイル名>処理対象の compose ファイルを明示指定(複数可)

削除されるリソース 

リソース--all なし--all あり
コンテナ
ネットワーク
ボリューム
イメージ

注意事項 

⚠️ ボリュームを削除するとデータベースのデータも失われます。 バックアップが必要な場合は事前に docker volume コマンドでデータをエクスポートしてください。


汎用スクリプトとして使う 

このスクリプトは特定のプロジェクトに依存しません。 docker-clean.sh を任意のプロジェクトルートにコピーするだけで利用できます。

cp /path/to/docker-clean.sh ~/my-other-project/

また、~/bin/ など PATH が通ったディレクトリに置けば、どこからでも呼び出せます。

cp docker-clean.sh ~/bin/docker-clean
docker-clean --all

See also