我正在开发一个服务,并使用docker compose来旋转postgres,redis,elasticsearch等服务.我有一个基于RubyOnRails的Web应用程序,可以从所有这些服务中进行写入和读取.
这是我的 docker-compose.yml
version: '2'
services:
redis:
image: redis:2.8
networks:
- frontapp
elasticsearch:
image: elasticsearch:2.2
networks:
- frontapp
postgres:
image: postgres:9.5
environment:
POSTGRES_USER: elephant
POSTGRES_PASSWORD: smarty_pants
POSTGRES_DB: elephant
volumes:
- /var/lib/postgresql/data
networks:
- frontapp
networks:
frontapp:
driver: bridge
Run Code Online (Sandbox Code Playgroud)
我可以ping这个网络中的容器
$ docker-compose run redis /bin/bash
root@777501e06c03:/data# ping postgres
PING postgres (172.20.0.2): 56 data bytes
64 bytes from 172.20.0.2: icmp_seq=0 ttl=64 time=0.346 ms
64 bytes from 172.20.0.2: icmp_seq=1 ttl=64 time=0.047 ms
...
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.现在我想在我的主机上的rails应用程序上运行ruby但是能够使用url访问postgres实例,就像postgresql://username:password@postgres/database目前那样是不可能的
$ ping postgres
ping: unknown host …Run Code Online (Sandbox Code Playgroud) 我知道Docker在127.0.0.11运行了一个神奇的dns,所以我想我会尝试
nslookup mycontainername 127.0.0.11
Run Code Online (Sandbox Code Playgroud)
其中mycontainername,当然,容器的名字我想访问.但是,那个超时了.
;; connection timed out; no servers could be reached
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我在Docker 1.10.3上.如果它有帮助,我用docker-compose1.6.2 拉起一堆容器.
编辑这是Docker 1.10的副本,它通过主机的主机名访问容器
我正在尝试从主机解析 docker 容器地址。Windows 主机。Windows 容器。我回顾了许多类似的问题:
这些问题都没有解决这个具体问题。看起来像是一个微不足道的操作,我根本不明白为什么它不起作用。
例如
$ docker network create -d nat --subnet 172.25.0.0/16 test-net
$ docker container run --network test-net --name example-1 test/example1
Run Code Online (Sandbox Code Playgroud)
其中 test/example1 只是一个 ASP.NET Core Web 服务器。
因此,在我看来,我应该能够根据我在上面引用的答案中看到的各种线索来解析主机的容器名称。例如
$ nslookup example-1 172.25.0.1
$ nslookup example-1 127.0.0.11
$ nslookup example-1.test-net.docker 127.0.0.11
Run Code Online (Sandbox Code Playgroud)
然而没有任何作用。
当然,我在生产中不需要这种功能,但它对于测试非常有帮助。我根本不明白为什么我不能让它发挥作用。我缺少什么?