はじめに
Docker Composeで構築したコンテナ間での通信を試してみます。
デフォルトのネットワークでの通信
Docker Composeではデフォルトで1つのネットワークを作成してくれます。全てのコンテナはこのデフォルトのネットワークに参加しています。
試しに以下のようなweb1
とweb2
というnginxのコンテナを構築してみます。
version: "3"
services:
web1:
image: nginx
web2:
image: nginx
まずはコンテナを実行します。
❯ docker-compose up -d
Creating network "compose-network_default" with the default driver
Creating compose-network_web2_1 ... done
Creating compose-network_web1_1 ... done
デフォルトのネットワークが作成されているのがわかります。このcompose-network_default
というネットワークにweb1
とweb2
コンテナが参加しています。
❯ docker network ls
NETWORK ID NAME DRIVER SCOPE
4aaabcc641ea compose-network_default bridge local
コンテナはそれぞれサービス名で認識できます。試しにweb1
からweb2
にアクセスしてみます。
❯ docker-compose exec web1 curl http://web2/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
web2
という名前でアクセスできていることがわかります。
ネットワークを分けてみる
自分でネットワークを設定して、それぞれのコンテナがどのネットワークに参加するかも指定できます。

Compose file version 3 reference
Compose file reference
以下のような3つのコンテナと2つのネットワークで試してみます。
version: "3"
services:
web1:
image: nginx
networks:
- net1
web2:
image: nginx
networks:
- net1
- net2
web3:
image: nginx
networks:
- net2
networks:
net1:
net2:
まずはコンテナを実行します。
❯ docker-compose up -d
Creating network "compose-network_net1" with the default driver
Creating network "compose-network_net2" with the default driver
Creating compose-network_web3_1 ... done
Creating compose-network_web1_1 ... done
Creating compose-network_web2_1 ... done
今回はデフォルトではないネットワークが2つ作成されています。
❯ docker network ls
NETWORK ID NAME DRIVER SCOPE
5880ed155493 compose-network_net1 bridge local
91ca6076d2a8 compose-network_net2 bridge local
同じネットワークnet1
に参加しているweb1
とweb2
は通信可能です。
❯ docker-compose exec web1 curl http://web2/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
しかし、同じネットワークに参加していないweb1
とweb3
は通信することができません。
❯ docker-compose exec web1 curl http://web3/index.html
curl: (6) Could not resolve host: web3
【補足】linksについて
links
はDockerの--link
オプションと同じでサービスに対してエイリアスを追加することができます。
しかし、--link
オプションはDockerから削除される可能性があるため、推奨されていないようです。

Compose file version 3 reference
Compose file reference
まとめ
- Composeではデフォルトでネットワークが作成されサービス名で通信できる
- 自分でネットワークの設定もできる