LoginSignup
4
3

More than 5 years have passed since last update.

他のサービスやコンテナ上のボリュームをマウント(volumes_from)

Last updated at Posted at 2018-04-08

自分用のメモ
volumes_fromを使いたいときのdocker-compose.yml

リファレンスに書いてあることを書いただけ。

  • アクセス権は省略するとrw
  • /test1に作ったファイルはコンテナcにのみ存在
    • コンテナcを消したらファイルも消える
  • /test2に作ったファイルはdocker-compose.ymlと同階層に存在
    • コンテナcを消してもファイルは消えない
docker-compose.yml
version: "2"

services:

  a:
    image: alpine:latest
    tty: true
    volumes_from:
      - d

  b:
    image: alpine:latest
    tty: true
    volumes_from:
      - d:rw

  c:
    image: alpine:latest
    tty: true
    volumes_from:
      - d:ro

  d:
    image: alpine:latest
    tty: true
    volumes:
      - /test1
      - .:/test2

アクセス権の確認結果

コンテナcからは書き込めないことを確認

$ docker-compose exec a ash
/ # touch test1/a1
/ # touch test2/a2

$ docker-compose exec b ash
/ # touch test1/b1
/ # touch test2/b2

$ docker-compose exec c ash
/ # touch test1/c1
touch: test1/c1: Read-only file system
/ # touch test2/c2
touch: test2/c2: Read-only file system

コンテナdの中身を確認すると
test1test2 にそれぞれ他のコンテナで作ったファイルが存在している。

$ docker-compose exec d ash
/ # ls -la /test1
total 8
drwxr-xr-x    2 root     root          4096 Apr  8 12:25 .
drwxr-xr-x    1 root     root          4096 Apr  8 12:22 ..
-rw-r--r--    1 root     root             0 Apr  8 12:25 a1
-rw-r--r--    1 root     root             0 Apr  8 12:25 b1

/ # ls -la /test2
total 8
drwxr-xr-x    5 root     root           160 Apr  8 12:26 .
drwxr-xr-x    1 root     root          4096 Apr  8 12:22 ..
-rw-r--r--    1 root     root             0 Apr  8 12:25 a2
-rw-r--r--    1 root     root             0 Apr  8 12:26 b2
-rw-r--r--    1 root     root           339 Apr  8 12:22 docker-compose.yml

Mac上のdocker-compose.ymlと同階層を確認するとtest2に作ったファイルが存在している。

$ ls -la
total 8
drwxr-xr-x  5 sakuraba  staff  160  4  8 21:26 .
drwxr-xr-x  7 sakuraba  staff  224  4  8 20:50 ..
-rw-r--r--  1 sakuraba  staff    0  4  8 21:25 a2
-rw-r--r--  1 sakuraba  staff    0  4  8 21:26 b2
-rw-r--r--  1 sakuraba  staff  339  4  8 21:22 docker-compose.yml
4
3
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
3