LoginSignup
33
32

More than 5 years have passed since last update.

Dockerのコンテナ間での連携

Posted at

Container同士のリンク

webという名前のコンテナーとdbという名前のコンテナーをリンク
リンクしておくとリンク先の情報が環境変数と/etc/hostsに反映される

$ #--linkでdb containerをweb containerとリンク
$ sudo docker run -d -P --name web --link db:db training/webapp python app.py

$ #web container内の環境変数の表示
$ sudo docker run --rm --name web --link db:db training/webapp env
    . . .
    DB_NAME=/web2/db
    DB_PORT=tcp://172.17.0.5:5432
    DB_PORT_5000_TCP=tcp://172.17.0.5:5432
    DB_PORT_5000_TCP_PROTO=tcp
    DB_PORT_5000_TCP_PORT=5432
    DB_PORT_5000_TCP_ADDR=172.17.0.5
    . . .
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
172.17.0.7  aed84ee21bde
. . .
172.17.0.5  db

参考URL

Container間のデータ共有

データボリューム機能によりコンテナ間のデータ共有が可能

以下のコマンドでコンテナ(dbdata)にデータボリューム(/dbdata)を追加できる

$ sudo docker run -d -v /data --name dbdata training/postgres

--volumes-fromを使ってdbdataのデータボリューム(/data)を他のコンテナー(web)にマウントする

$ sudo docker run -d --volumes-from dbdata --name web training/webapp
  • training/webapp及びtraining/postgresはdocker hubにあるオフィシャル?のdocker イメージ

参考URL

コンテナの連携を図にしてみた

data container (2).png

33
32
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
33
32