コンテナがホストOSのどのボリュームを利用できるか確認する方法
コンテナがホストOSのどのボリュームを利用できるか確認するにはdocker container inspectコマンド実行します。
なお、ホストOSのデータをコンテナに見せる方法として以下の3種類があります。
- bind mount:Dockerホスト上のディレクトリをコンテナに見せる
- volume:Dockerホストが管理するボリューム(/var/lib/docker/volumes以下)をコンテナに見せる
- tmpfs mount:Dockerホストのメモリをファイルシステムとしてコンテナに見せる
オプションの説明(抜粋)
$ docker container run --help Usage: docker container run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container Options: -d, --detach Run container in background and print container ID -i, --interactive Keep STDIN open even if not attached --mount mount Attach a filesystem mount to the container --name string Assign a name to the container -t, --tty Allocate a pseudo-TTY
オプションの説明
- --mountオプション:ホストOSが提供するディレクトリをコンテナに見せる
- -dオプション:Dockerコンテナをバックグラウンドで起動する
- -iオプション:Dockerコンテナ起動時に、標準入力(STDIN)を受け付ける
- -tオプション:仮想端末(pseudo-TTY)をコンテナに割り当てる
- --nameオプション:作成するコンテナに名前を付ける
- ※-i -t -dを同時に使用する場合は、-itdオプションを使用できます
コマンドの説明
$ docker container inspect --help Usage: docker container inspect [OPTIONS] CONTAINER [CONTAINER...] Display detailed information on one or more containers Options: -f, --format string Format the output using the given Go template -s, --size Display total file sizes
実行例
$ docker container run -itd --name test01 \ --mount type=bind,src=$HOME/hostdir01,dst=/root/ctdir01 \ --mount type=volume,src=vol02,dst=/root/ctdir02 \ --mount type=tmpfs,dst=/root/ctdir03 \ centos:7.7.1908 22f8a35d2c72cf74c9cde5ee7c3595f3897de2fb2b6bbc0aae2d2b328cb58d73 $ docker container inspect test01 | less ... "Mounts": [ { "Type": "bind", "Source": "/home/vagrant/hostdir01", "Destination": "/root/ctdir01", "Mode": "", "RW": true, "Propagation": "rprivate" }, { "Type": "volume", "Name": "vol02", "Source": "/var/lib/docker/volumes/vol02/_data", "Destination": "/root/ctdir02", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" }, { "Type": "tmpfs", "Source": "", "Destination": "/root/ctdir03", "Mode": "", "RW": true, "Propagation": "" } ], ...