docker network config

Backup from here: https://stackoverflow.com/questions/46327006/connect-two-docker-containers

And read this great answer: https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach

关于如何将两个docker中的port进行map,以及连接。

这个配置非常有用,例如当你的服务器上跑着两个 mysql 服务的时候,可能需要分开 map 端口。
但是,如果此时你本地又有一个container需要连接本地的其中一个 mysql 服务的时候,这时就可以上将这个 app 的 network map到那个 mysql 的network中(方法1)

Since your are running the containers individually you have different options

Run django on network of postgres container(方法1)

$ docker run -d ... postgres
<container id>

$ docker run -d ... --net=--net=container:NAME_or_ID django

Then django can find postgres on localhost:5432

Run django and postgres as named containers container

$ docker run --name postgresdb -d ... postgres
<container id>

$ docker run -d ...  django

Now django can find db on postgresdb:5432

Run both containers on host

$ docker run --net=host -d ... postgres
<container id>

$ docker run -d ... --net=host django

Then django can find postgres on localhost:5432

Run containers on same network

$ docker network create mynet

$ docker run --name postgresdb --net mynet -d ... postgres
<container id>

$ docker run --net mynet -d ...  django

Now django can find db on postgresdb:5432

Connect to the host IP and mapped port

$ docker run -d -p 32770:5432 .... postgres
$ docker run -d .... django

Django can now connect to the DB on :32770.

Note:

docker restart xxx will not refresh image
use: docker rm -f xxx + docker run xxx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值