[docker] Bind Mount

Bind MountはPC上の任意のディレクトリをコンテナにマウントする
-v <ホスト上のディレクトリ>:<コンテナ上のディレクトリ>

$ touch test-file
$ ls -la
total 8
drwxr-xr-x 2 vagrant vagrant 4096 Mar 19 00:57 .
drwxrwxr-x 3 vagrant vagrant 4096 Mar 18 23:07 ..
-rw-rw-r– 1 vagrant vagrant 0 Mar 19 00:57 test-file

$ sudo docker run -itd –name bind-c1 -v “$(pwd)”:/bind_dir busybox
$ sudo docker exec bind-c1 ls -la /bind_dir
total 8
drwxr-xr-x 2 1000 1000 4096 Mar 19 00:57 .
drwxr-xr-x 1 root root 4096 Mar 19 01:12 ..
-rw-rw-r– 1 1000 1000 0 Mar 19 00:57 test-file
$ touch test-file2
$ sudo docker exec bind-c1 ls -la /bind_dir
total 8
drwxr-xr-x 2 1000 1000 4096 Mar 19 01:13 .
drwxr-xr-x 1 root root 4096 Mar 19 01:12 ..
-rw-rw-r– 1 1000 1000 0 Mar 19 00:57 test-file
-rw-rw-r– 1 1000 1000 0 Mar 19 01:13 test-file2

### -mountを指定したコンテナの起動
$ sudo docker run -itd –name bind-c2 –mount type=bind,source=”$(pwd)”,target=/bind_dir busybox
54deefdb625241544cc84326df8055e38c32a8d3e6b2a97c1d0db2bb260c693f
$ sudo docker exec bind-c2 ls -l /bind_dir
total 0
-rw-rw-r– 1 1000 1000 0 Mar 19 00:57 test-file
-rw-rw-r– 1 1000 1000 0 Mar 19 01:13 test-file2
※–mountオプションの方が読みやすい。また、間違えたディレクトリのバインドを防ぐ意味でも–mountの方が安全

### バインドマウントの設定確認
$ sudo docker inspect bind-c1
“Mounts”: [
{
“Type”: “bind”,
“Source”: “/home/vagrant/dev/docker/test/app”,
“Destination”: “/bind_dir”,
“Mode”: “”,
“RW”: true,
“Propagation”: “rprivate”
}
],

なるほど、bindの概念はvolumeと一緒でわりかし簡単だな