我已经阅读了几个围绕同一问题的SO问题,但由于积分不足,我无法对现有问题发表评论。因此,我必须生成我自己的有关 docker-compose 和链接的问题线程。
我的印象是,在 docker-compose 文件中包含以下内容会将“db”添加到我的容器的主机文件中
web:
links:
- db
我认为我的网络代码可以与以下内容建立数据库连接
db := sql.Open("postgres", "user=foo dbname=baz host=db")
其中主机名db存在于我的Web容器的/etc/host文件中,因此解析为到达db容器的某个地址。
我的网络应用程序没有解析到我的数据库运行的地址,而是解析到一个地址(172.19.0.3),但我无法弄清楚它来自哪里或如何修复。
docker exec $WEB_CONTAINER_ID cat /etc/hosts
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.19.0.2 6f52f9e78f00
我期待看到 db 的条目。此外:
docker exec $WEB_CONTAINER_ID nslookup db
Name: db
Address 1: 172.19.0.3 apiserver_postgres_1.apiserver_default
我有什么误解吗?如何使我的网络代码能够解析我的数据库地址?
最佳答案
这不再是经典的 docker 1.8 或更低版本的“链接”,特别是当您使用 docker-compose.yml
文件的版本 2 时。
如 issue 3002 中所述:
version:"2" creates a bridge network by default for your project. If you run
docker network ls
you'll see a new bridged network where your project containers will be created. (which makes totally sense)Docker itself has removed these variables from links on any network except for the old bridge network.
现在有一个 embedded DNS .
关于postgresql - 一项服务如何使用 docker compose、链接主机名和端口解析另一项服务的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35836916/