Docker Composeでコンテナ間通信

2021.10.08
2024.03.24
Docker
Docker Compose

はじめに

Docker Composeで構築したコンテナ間での通信を試してみます。

デフォルトのネットワークでの通信

Docker Composeではデフォルトで1つのネットワークを作成してくれます。全てのコンテナはこのデフォルトのネットワークに参加しています。

試しに以下のようなweb1web2というnginxのコンテナを構築してみます。

1version: "3"
2services:
3  web1:
4    image: nginx
5  web2:
6    image: nginx

まずはコンテナを実行します。

1docker-compose up -d
2Creating network "compose-network_default" with the default driver
3Creating compose-network_web2_1 ... done
4Creating compose-network_web1_1 ... done

デフォルトのネットワークが作成されているのがわかります。このcompose-network_defaultというネットワークにweb1web2コンテナが参加しています。

1docker network ls
2NETWORK ID     NAME                      DRIVER    SCOPE
34aaabcc641ea   compose-network_default   bridge    local

コンテナはそれぞれサービス名で認識できます。試しにweb1からweb2にアクセスしてみます。

1docker-compose exec web1 curl http://web2/index.html
2<!DOCTYPE html>
3<html>
4<head>
5<title>Welcome to nginx!</title>
6<style>
7html { color-scheme: light dark; }
8body { width: 35em; margin: 0 auto;
9font-family: Tahoma, Verdana, Arial, sans-serif; }
10</style>
11</head>
12<body>
13<h1>Welcome to nginx!</h1>
14<p>If you see this page, the nginx web server is successfully installed and
15working. Further configuration is required.</p>
16
17<p>For online documentation and support please refer to
18<a href="http://nginx.org/">nginx.org</a>.<br/>
19Commercial support is available at
20<a href="http://nginx.com/">nginx.com</a>.</p>
21
22<p><em>Thank you for using nginx.</em></p>
23</body>
24</html>

web2という名前でアクセスできていることがわかります。

ネットワークを分けてみる

自分でネットワークを設定して、それぞれのコンテナがどのネットワークに参加するかも指定できます。

Compose file version 3 reference

Compose file version 3 reference

Find a quick reference for Docker Compose version 3, including Docker Engine compatibility, memory limitations, and more.

以下のような3つのコンテナと2つのネットワークで試してみます。

1version: "3"
2services:
3  web1:
4    image: nginx
5    networks:
6      - net1
7  web2:
8    image: nginx
9    networks:
10      - net1
11      - net2
12  web3:
13    image: nginx
14    networks:
15      - net2
16
17networks:
18  net1:
19  net2:

まずはコンテナを実行します。

1docker-compose up -d
2Creating network "compose-network_net1" with the default driver
3Creating network "compose-network_net2" with the default driver
4Creating compose-network_web3_1 ... done
5Creating compose-network_web1_1 ... done
6Creating compose-network_web2_1 ... done

今回はデフォルトではないネットワークが2つ作成されています。

1docker network ls
2NETWORK ID     NAME                   DRIVER    SCOPE
35880ed155493   compose-network_net1   bridge    local
491ca6076d2a8   compose-network_net2   bridge    local

同じネットワークnet1に参加しているweb1web2は通信可能です。

1docker-compose exec web1 curl http://web2/index.html
2<!DOCTYPE html>
3<html>
4<head>
5<title>Welcome to nginx!</title>
6<style>
7html { color-scheme: light dark; }
8body { width: 35em; margin: 0 auto;
9font-family: Tahoma, Verdana, Arial, sans-serif; }
10</style>
11</head>
12<body>
13<h1>Welcome to nginx!</h1>
14<p>If you see this page, the nginx web server is successfully installed and
15working. Further configuration is required.</p>
16
17<p>For online documentation and support please refer to
18<a href="http://nginx.org/">nginx.org</a>.<br/>
19Commercial support is available at
20<a href="http://nginx.com/">nginx.com</a>.</p>
21
22<p><em>Thank you for using nginx.</em></p>
23</body>
24</html>

しかし、同じネットワークに参加していないweb1web3は通信することができません。

1docker-compose exec web1 curl http://web3/index.html
2curl: (6) Could not resolve host: web3

【補足】linksについて

linksはDockerの--linkオプションと同じでサービスに対してエイリアスを追加することができます。

しかし、--linkオプションはDockerから削除される可能性があるため、推奨されていないようです。

Compose file version 3 reference

Compose file version 3 reference

Find a quick reference for Docker Compose version 3, including Docker Engine compatibility, memory limitations, and more.

まとめ

  • Composeではデフォルトでネットワークが作成されサービス名で通信できる
  • 自分でネットワークの設定もできる

参考

Support

\ この記事が役に立ったと思ったら、サポートお願いします! /

buy me a coffee
Share

Profile

author

Masa

都内のIT企業で働くエンジニア
自分が学んだことをブログでわかりやすく発信していきながらスキルアップを目指していきます!

buy me a coffee