概要
docker run
した時のコンテナ間通信の仕方メモ。
推奨されてないらしいので、 --link
は使わないよ。
docker network create
したネットワークを docker run
時に指定するとコンテナ間通信ができる。
メモ
docker network create
する。
$ docker network create test-network
3c1309d35ee951ec743d1a7cf1b02178fd4e4107823c7c959089dd79e828a532
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
d7274732b28f bridge bridge local
0a4b170c1445 host host local
22009373ca6d none null local
3c1309d35ee9 test-network bridge local
docker run
する際にネットワークを指定すると、コンテナの中からコンテナ名指定で通信できるようになる。
docker run -it -p 80:80 --network test-network --rm --name nginx nginx
docker run -it --rm --network test-network --name test alpine
testコンテナ内から、wget "http://nginx"
で、 ※nginxはコンテナ名
$ wget "http://nginx:80" -O - -q
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
ping
を打つと、
$ ping nginx
PING nginx (172.18.0.4): 56 data bytes
64 bytes from 172.18.0.4: seq=0 ttl=255 time=0.099 ms
64 bytes from 172.18.0.4: seq=1 ttl=255 time=0.096 ms
64 bytes from 172.18.0.4: seq=2 ttl=255 time=0.096 ms
$ping test
PING test (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=255 time=0.033 ms
64 bytes from 172.18.0.3: seq=1 ttl=255 time=0.063 ms
64 bytes from 172.18.0.3: seq=2 ttl=255 time=0.062 ms
という感じ。