LoginSignup
4
3

More than 5 years have passed since last update.

同一ホスト上にあるコンテナ間連携

Posted at

同一ホスト上に2つのコンテナを立ち上げて、お互いを連携させてみます。
WebサーバとDBサーバを連携するようなイメージです。

今回は、今まで作ってきた下記2記事のコンテナ(Ubuntuとnginx)を元に連携を試してみました。

最後にまたもやproxyにハマりました…

nginxサーバのコンテナ起動

  • 普通に立ち上げ
$ docker run --name nginx01 -d -p 8080:80 nginx

Ubuntuサーバのコンテナ起動

  • linkオプションに<連携するコンテナ名>:<エイリアス>を記載
$ docker run --name ubuntu01 -it --link nginx01:nginx ubuntu1404

連携状況の確認

  • Ubuntu側サーバにて、envコマンドで見てみると接続先サーバであるNGINX(エイリアス)の環境変数確認ができる
# env | grep NGINX
NGINX_PORT_443_TCP_ADDR=172.17.0.41
NGINX_ENV_NGINX_VERSION=1.9.6-1~jessie
NGINX_NAME=/ubuntu01/nginx
NGINX_PORT_80_TCP_PROTO=tcp
NGINX_PORT_80_TCP=tcp://172.17.0.41:80
NGINX_PORT_443_TCP_PORT=443
NGINX_PORT_80_TCP_PORT=80
NGINX_PORT_443_TCP=tcp://172.17.0.41:443
NGINX_PORT_443_TCP_PROTO=tcp
NGINX_PORT_80_TCP_ADDR=172.17.0.41
NGINX_PORT=tcp://172.17.0.41:80
  • hostsでも確認ができる
# less /etc/hosts 
172.17.0.43     d6b904340038
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.41     nginx 03a5f77c3fdc nginx01
172.17.0.43     ubuntu01
172.17.0.43     ubuntu01.bridge
172.17.0.41     nginx01
172.17.0.41     nginx01.bridge
  • 各サーバのIP

    • nginx :172.17.0.41
    • Ubuntu: 172.17.0.43
  • pingを飛ばしてみる

# ping 172.17.0.41
PING 172.17.0.41 (172.17.0.41) 56(84) bytes of data.
64 bytes from 172.17.0.41: icmp_seq=1 ttl=64 time=0.116 ms
64 bytes from 172.17.0.41: icmp_seq=2 ttl=64 time=0.115 ms
...

Ubuntuサーバ側からWelcomeページを取得してみる

  • コンテナ側のIPで取得できることを確認
# cd /tmp/
# wget http://172.17.0.41:80
--2015-11-06 02:43:23--  http://172.17.0.41/
Connecting to 172.17.0.41:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 612 [text/html]
Saving to: 'index.html.1'

100%[================================================================================>] 612         --.-K/s   in 0s      

2015-11-06 02:43:23 (47.4 MB/s) - 'index.html.1' saved [612/612]

社内proxyに引っかかる場合…

  • Ubuntuサーバ側にてnginxサーバにアクセスする際に、下記のようなエラーが出た場合、no_proxy設定を行う
wget http://172.17.0.41:80
--2015-11-06 02:12:43--  http://172.17.0.41/
Proxy request sent, awaiting response...
  • Ubuntuならwgetrcファイルに接続先のnginxサーバのIDを記した1行を追加
/etc/wgetrc
no_proxy=172.17.0.41
4
3
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
4
3