使用容器名称从主机访问Docker容器
我正在开发服务,并在那里使用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
我可以在此网络中ping容器
$ docker-compose run redis /bin/bashroot@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
...
到目前为止,一切都很好。现在我想在主机上的Rails应用程序上运行ruby,但是能够像postgresql://username:password@postgres/database
目前这样使用url访问postgres实例
$ ping postgresping: unknown host postgres
我可以在Docker中看到我的网络
$ docker network lsNETWORK ID NAME DRIVER
ac394b85ce09 bridge bridge
0189d7e86b33 elephant_default bridge
7e00c70bde3b elephant_frontapp bridge
a648554a72fa host host
4ad9f0f41b36 none null
我可以看到一个界面
$ ifconfig br-0189d7e86b33 Link encap:Ethernet HWaddr 02:42:76:72:bb:c2
inet addr:172.18.0.1 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:76ff:fe72:bbc2/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:36 errors:0 dropped:0 overruns:0 frame:0
TX packets:60 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2000 (2.0 KB) TX bytes:8792 (8.7 KB)
br-7e00c70bde3b Link encap:Ethernet HWaddr 02:42:e7:d1:fe:29
inet addr:172.20.0.1 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:e7ff:fed1:fe29/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1584 errors:0 dropped:0 overruns:0 frame:0
TX packets:1597 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:407137 (407.1 KB) TX bytes:292299 (292.2 KB)
...
但是我不确定下一步该怎么做。我尝试使用/etc/resolv.conf
,主要是使用nameserver
指令,但没有效果。
对于建议如何正确配置此设置的建议,我将不胜感激。
浏览Internet资源后,我设法将静态IP地址分配给框。现在,对我来说继续开发就足够了。这是我目前的docker-compose.yml
version: '2'services:
redis:
image: redis:2.8
networks:
frontapp:
ipv4_address: 172.25.0.11
elasticsearch:
image: elasticsearch:2.2
networks:
frontapp:
ipv4_address: 172.25.0.12
postgres:
image: postgres:9.5
environment:
POSTGRES_USER: elephant
POSTGRES_PASSWORD: smarty_pants
POSTGRES_DB: elephant
volumes:
- /var/lib/postgresql/data
networks:
frontapp:
ipv4_address: 172.25.0.10
networks:
frontapp:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.25.0.0/16
gateway: 172.25.0.1
回答:
有一个开源应用程序可以解决此问题,称为DNS Proxy Server,这里有一些来自官方存储库的示例
这是一个DNS服务器,用于解析容器的主机名,如果找不到匹配的主机名,则也可以从Internet解析它
启动DNS服务器
$ docker run --hostname dns.mageddo --restart=unless-stopped -p 5380:5380 \-v /var/run/docker.sock:/var/run/docker.sock \
-v /etc/resolv.conf:/etc/resolv.conf \
defreitas/dns-proxy-server
它将自动设置为您的默认DNS(并在停止时恢复为原始DNS)
创建一些容器进行测试
检查docker-compose文件
$ cat docker-compose.ymlversion: '3'
services:
nginx-1:
image: nginx
hostname: nginx-1.docker
network_mode: bridge
linux-1:
image: alpine
hostname: linux-1.docker
command: sh -c 'apk add --update bind-tools && tail -f /dev/null'
network_mode: bridge # that way he can solve others containers names even inside, solve nginx-2, for example
起始容器
$ docker-compose up
解决容器
nslookup nginx-1.dockerServer: 13.0.0.5
Address: 13.0.0.5#53
Non-authoritative answer:
Name: nginx-1.docker
Address: 13.0.0.6
$ docker-compose exec linux-1 ping nginx-1.dockerPING nginx-1.docker (13.0.0.6): 56 data bytes
64 bytes from 13.0.0.6: seq=0 ttl=64 time=0.034 ms
$ nslookup google.comServer: 13.0.0.5
Address: 13.0.0.5#53
Non-authoritative answer:
Name: google.com
Address: 216.58.202.78
以上是 使用容器名称从主机访问Docker容器 的全部内容, 来源链接: utcz.com/qa/426937.html