X

docker runやdocker compose upで出来てしまう匿名ボリュームを作るコンテナを見つけるスクリプト

docker inspectで出来るのですが、何度も、何度も、手動でやっているので作りました。

#!/usr/bin/env bash

# すべてのコンテナ(停止中のものも含む)に対して実行
for container in $(docker ps -aq); do
  # コンテナ名とマウントされているボリューム名(名前付き/匿名問わず)を表示
  echo "--- Container: $(docker inspect -f '{{.Name}}' $container) ---"
  docker inspect -f '{{range .Mounts}} Name: {{.Name}}, Destination: {{.Destination}}{{println .}}{{end}}' $container
done

find_volume.shなどと名前を付けて実行すると

yohgaki@dev2:~/Docker/compose/gitlab-runner$ ../find_volume.sh 
--- Container: /gitlab-runner-gitlab-runner-1 ---
 Name: , Destination: /var/run/docker.sock{bind  /var/run/docker.sock /var/run/docker.sock  rw true rprivate}
 Name: , Destination: /etc/gitlab-runner{bind  /home/yohgaki/Docker/compose/gitlab-runner/fs/gitlab-runner/config /etc/gitlab-runner  rw,Z true rprivate}
 Name: 07f8873974463c152f6fe31fb925c381f53b82a61355ae3c9fd5369319604fb0, Destination: /home/gitlab-runner{volume 07f8873974463c152f6fe31fb925c381f53b82a61355ae3c9fd5369319604fb0 /var/lib/docker/volumes/07f8873974463c152f6fe31fb925c381f53b82a61355ae3c9fd5369319604fb0/_data /home/gitlab-runner local  true }

--- Container: /portainer-portainer-1 ---
 Name: , Destination: /var/run/docker.sock{bind  /var/run/docker.sock /var/run/docker.sock  rw,Z true rprivate}
 Name: , Destination: /data{bind  /home/yohgaki/Docker/compose/portainer/fs/data /data  rw,Z true rprivate}

どのコンテナがどのvolumeをどのようにマウントしているか判ります。この例だとgitlab-runnerのDockerfileがVOLUMEで/home/gitlab-runnerを指定していて匿名ボリューうとしてマウントされていると判ります。

docker volume pruneで使っていないボリュームを丸ごと消しても構わないのですが、精神衛生上あまり良くないので匿名ボリュームを作らないようにした方が良いです。

Categories: Computer
yohgaki:
Related Post
Leave a Comment