LoginSignup
223
182

More than 5 years have passed since last update.

docker-compose で別の docker-compose.yml で作ったコンテナとリンクする (ネットワークを繋げる)

Last updated at Posted at 2016-06-20

バーチャルホストのように、1つのサーバーに複数のWebアプリを置いておきたい場合があります。
その際に、nginxと各アプリにはそれぞれの docker-compose.yml を作って別々に管理したいと思います。

  • nginx (リバースプロキシ)
    • Webアプリ1 (PHP http)
    • Webアプリ2 (Python uWSGI)
    • ...

この場合に、 nginx から Webアプリ1Webアプリ2 にリンクするように作りたいのですが、
docker-compose で作ったコンテナはネットワークが分かれている?ので、
ネットワークを指定してあげないといけないみたいです。

方法

  • 方法1: nginxから、各ネットワークに入るように設定する
    • メリット
      • 各アプリのdocker-compose.ymlでネットワークの定義を書かなくて良い
    • デメリット
      • アプリが増えるとnginxのdocker-compose.ymlに書く設定が増える
      • アプリの追加と削除時に毎回docker-compose.ymlを変更する必要がある
      • 各アプリのネットワーク名が変わった場合は、nginxのdocker-compose.ymlを直す必要がある(プロジェクト名が変わった時など)
  • 方法2: 共通リンク用のネットワークを作成して、nginxと各アプリでそのネットワークに入るように設定する
    • メリット
      • アプリの追加と削除時にdocker-compose.ymlを変更する必要がない
    • デメリット
      • ネットワークを1個作っておく必要がある
      • 各アプリのdocker-compose.ymlにネットワークの設定を書く必要がある

方法1: nginxから、各ネットワークに入るように設定する

nginxのdocker-compose.ymlにネットワークの設定を書きます。

nginx

version: '2'
services:
    nginx:
        image: nginx:latest
        container_name: nginx
        ports:
            - '80:80'
            - '443:443'
        volumes:
            - ./volumes/conf.d:/etc/nginx/conf.d
            - ./volumes/certs:/etc/nginx/certs
        networks:
            - default
            - webapp1_default
            - webapp2_default
networks:
    webapp1_default:
        external: true
    webapp2_default:
        external: true

このように、2箇所に networks の定義を追加してあげると、そのネットワークを見れるようになります。

ネットワーク名の調べ方

docker network ls

docker network ls を使用すると、現在 docker に存在するネットワークの一覧が出てきます。

$ docker network ls
NETWORK ID          NAME                   DRIVER
xxxxxxxxxxxx        bridge                 bridge              
xxxxxxxxxxxx        host                   host                
xxxxxxxxxxxx        nginx_default          bridge              
xxxxxxxxxxxx        webapp1_default        bridge              
xxxxxxxxxxxx        webapp2_default        bridge              

docker-compose up -d

docker-compose up -d を実行した時にも出てきます。

$ docker-compose up -d
Creating network "webapp1_default" with the default driver
Creating webapp1

方法2: 共通リンク用のネットワークを作成して、nginxと各アプリでそのネットワークに入るように設定する

まず、共通で使うネットワークを作成します。
名前はとりあえず、 common_link で作ります。
参考: http://docs.docker.jp/engine/userguide/networking/dockernetworks.html#id3

docker network create --driver bridge common_link

作成したら、 docker network ls で確認出来ます。

$ docker network ls
NETWORK ID          NAME                   DRIVER
...
xxxxxxxxxxxx        common_link            bridge              
...

次に、nginxと各アプリのdocker-compose.ymlにネットワークの設定を書きます。

アプリ1

version: '2'
services:
    webapp1:
        〜色々な設定省略〜
        networks:
            - common_link
networks:
    common_link:
        external: true

nginx

version: '2'
services:
    nginx:
        image: nginx:latest
        container_name: nginx
        ports:
            - '80:80'
            - '443:443'
        volumes:
            - ./volumes/conf.d:/etc/nginx/conf.d
            - ./volumes/certs:/etc/nginx/certs
        networks:
            - common_link
networks:
    common_link:
        external: true

networks の設定を書けばOKです。

参考

223
182
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
223
182