41
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【基本を調査&検証】docker-compose.ymlのトップレベルの「volumes」

Last updated at Posted at 2019-05-05

docker-compose.ymlの一番上の階層に「volumes」があるんだけどこれ何かわからない。。。という方向けの記事

色々docker-compose.yml関連の調べ物をしていたら、「volumes」が一番上の階層に配置されていて、これなんの設定だろう。。。と思ったので、何か調べて、簡単に検証をしてみようと思います。

一番上の階層に配置されている「volumes」は何か

以下の公式ドキュメントに記載がありました。

Compose file version 3 reference | Docker Documentation

以下に一番上の階層に配置されている「volumes」は何かを言及されている部分を引用。

The top-level volumes key defines a named volume and references it from each service’s volumes list.

Compose file version 3 reference | Docker Documentationより

ということで自分なりに解釈すると、

トップレベル(一番上の階層)にvolumesを名前付きで定義することによって、マウントするディレクトリを複数のコンテナ間で共通化できる

動作検証

今回は検証なので、簡単にやってみようかと思います。

上記で述べた、トップレベルのvolumesに名前を定義・指定のディレクトリを設定しつつ、nginxコンテナを2つ立ち上げて、ブラウザに表示するディレクトリを共通化してみようと思います。

ディレクトリ構造は以下になります。


├── docker-compose.yml
└── nginx
    └── html
        └── index.html

docker-compose.ymlの中身は以下

docker-compose.yml

version: '3'
services:

  nginx1:
    image: nginx:latest
    container_name: 'nginx1'
    volumes:
      - document_root:/usr/share/nginx/html
    ports:
      - "8888:80"

  nginx2:
    image: nginx:latest
    container_name: 'nginx2'
    volumes:
      - document_root:/usr/share/nginx/html
    ports:
      - "8889:80"

volumes:
  document_root:
    driver_opts:
      type: none
      device: /path/to/host/nginx/html #ここに共通の絶対パスを記述。相対パスは×。
      o: bind

「device:〜」に設定する値は、絶対パスでないとエラーが吐き出されました。私はこの部分に躓いてかなり時間を割いてしましました。。。

index.htmlの中身は、ディレクトリが共通化できていることがわかれば良いので、以下のようにしてます。

index.html

Hello world!
terminal
$ docker-compose up -d

これで、localhost:8888localhost:8889にブラウザでアクセスすると、同じディレクトリを参照しているかと思います。

「volumes_from」は何?

これもまた、docker-compose.ymlのをネットで検索すると沢山出てくるのが、「volumes_form」という設定。

volumesという文言があるから似たような設定なのかと思いつつ、調べてみました。

すると、以下の記事を発見。

Compose ファイル・リファレンス — Docker-docs-ja 17.06.Beta ドキュメント

「volumes_from」について説明されている部分を引用。

他のサービスやコンテナ上のボリュームをマウントします。
Compose ファイル・リファレンス — Docker-docs-ja 17.06.Beta ドキュメントより

私的な解釈をすると、__コンテナ側から、共通化するディレクトリを指定できると__いうことなのかな。

同じ内容の検証を「volumes_from」で行う

docker-compose.ymlの中身だけ変えると良いので、以下のようにします。

※「version:'3'」だとエラーが出たので、「verision:'2'」で行いました。

「verision:'2'」で動くものなのですね。

docker-compose.yml

version: '2'
services:

  nginx1:
    image: nginx:latest
    container_name: 'nginx1'
    volumes:
      - /path/to/host/nginx/html:/usr/share/nginx/html
    ports:
      - "8888:80"

  nginx2:
    image: nginx:latest
    container_name: 'nginx2'
    volumes_from:
      - nginx1
    ports:
      - "8889:80"


上記の設定で以下のコマンドを実行

terminal
$ docker-compose up -d

これで最初の検証と一緒の結果になりました。

さいごに

Docker関連の環境構築はなんとなくで使う部分が多いのですが、技術ブログを書きながら検証と調査をすることで理解が深まりました。

正直まだまだ知らないことが多いのですが、ひとつずつ調べていきます...!!!

参考

Compose file version 3 reference | Docker Documentation

Dockerの公式ドキュメント。英語分かる方はここから調べる色々わかるかも。

Compose ファイル・リファレンス — Docker-docs-ja 17.06.Beta ドキュメント

Dockerの公式ドキュメントを日本語化しているサイト。英語が苦手な方はここで調べると良いですね。

41
30
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
41
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?