LoginSignup
4
5

More than 5 years have passed since last update.

DockerコンテナにマウントされているVolumeディレクトリを docker-inspectで取得する

Last updated at Posted at 2015-12-04

docker 1.7 から 1.8 に更新したら、
以前の方法で取れなくなってしまった。

1.8では配列になったようなので、
インデックス指定またはループで検索する必要がある。

CONTANER="コンテナ名 / コンテナID"
VOLUME_DEST="コンテナのマウントパス"

# docker 1.7
docker inspect -f '{{(index .Volumes "${VOLUME_DEST}")}}' ${CONTAINER}

# docker 1.8
# 配列0番目のマウント
docker inspect -f '{{ (index .Mounts 0).Source }}' ${CONTAINER}

# マウントパスからループ検索
docker inspect --format='{{range $conf := .Mounts}} {{if eq .Destination "${VOLUME_DEST}"}} {{.Source}} {{end}} {{end}}' ${CONTAINER}  | tr -d ' '

# 移動用につかったりする(長い・・・)
cd $(docker inspect --format='{{range $conf := .Mounts}} {{if eq .Destination "${VOLUME_DEST}"}} {{.Source}} {{end}} {{end}}' ${CONTAINER}  | tr -d ' ')

※追記1 余分な空白が付いてしまうのでトリミング

man によると、
format は Go言語の text/template だそうだ。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5