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