Docker 1.10通过主机从主机访问容器 [英] Docker 1.10 access a container by it's hostname from a host machine
问题描述
我已经在docker-compose文件中创建了两个服务容器。它们可以通过主机名和IP来相互访问,但是当我想要从主机访问其中的一个时,它不起作用,它只适用于IP而不是主机名。
那么,是否可以通过Docker 1.10的主机名从主机访问Docker容器?
更新:
docker-compose.yml
版本:'2'
服务:
service_a:
image:nginx
container_name:docker_a
ports:
- 8080 :80
service_b:
image:nginx
container_name:docker_b
ports:
- 8081:80
然后我通过命令启动它: docker-compos up --force-recreate
当我运行:
-
docker exec -i -t docker_a ping - c4 docker_b
- 它的工作原理 -
docker exec -i -t docker_b ping -c4 docker_a
- 它的工作原理 -
ping 172.19.0.2
- 它有效(172.19.0.2
code> docker_b 的ip) -
ping docker _a
- 失败
docker网络检查test_default
是
[
{
名称: test_default,
Id:f6436ef4a2cd4c09ffdee82b0d0b47f96dd5aee3e1bde068376dd26f81e79712,
Scope:local,
Driver:bridge,
IPAM b $ bDriver:default,
Options:null,
Config:[
{
Subnet:172.19.0.0/16 ,
网关:172.19.0.1/16
}
]
},
容器:{
a9f13f023761123115fcb2b454d3fd21666b8e1e0637f134026c44a7a84f1b0b
Name:docker_a,
EndpointID:a5c8e08feda96d0de8f7c6203f2707dd3f9f6c3a64666126055b16a3908fafed,
MacAddress:02:42:ac:13:00:03,
IPv4Address:172.19.0.3/16,
IPv6Address:
},
c6532af99f691659b452c1cbf1693731a75cdfab9ea50428d9c99dd09c3e9a40:{
Name:docker_b,
EndpointID:28a1877a0fdbaeb8d33a290e5a5768edc737d069d23ef9bbcc1d64cfe5fbe312,
MacAddress:02:42:ac:13:00:02,
IPv4Address:172.19.0.2/16 b $ bIPv6Address:
}
},
选项:{}
}
]
这是我做的。
写了一个名为 dnsthing 的Python脚本,该脚本侦听Docker事件API,以启动或停止容器。它使用容器的名称和地址维护一个主机
-style文件。容器被命名为< container_name>。< network> .docker
,所以例如如果我运行:
docker run --rm --name mysql -e MYSQL_ROOT_PASSWORD = secret mysql
我得到这个:
172.17.0.2 mysql.bridge.docker
然后,我运行一个 dnsmasq
进程指向这个 code>文件。具体来说,我使用以下配置运行一个dnsmasq实例:
listen-address = 172.31.255.253
bind-interfaces
addn-hosts = / run / dnsmasq / docker.hosts
local = / docker /
无主机
无解决方案
我运行 dnsthing
脚本,如下所示:
dnsthing -csystemctl restart dnsmasq_docker\
-H /run/dnsmasq/docker.hosts --verbose
所以:
-
dnsthing
更新/run/dnsmasq/docker.hosts
作为容器
停止/开始 - 更新后,
dnsthing
运行systemctl restart dnsmasq_docker
-
dnsmasq_docker
使用上述配置运行dnsmasq
,将
绑定到本地网桥接口,地址为172.31 .255.253
。 -
我的系统上的主dnsmasq进程由
维护NetworkMana ger,从
/etc/NetworkManager/dnsmasq.d/dockerdns
中使用此配置:server = / docker / 172.31.255.253
告诉dnsmasq通过
.docker
中的主机所有请求到docker_dnsmasq
服务。
这显然需要一些设置把所有东西放在一起,但是
之后似乎Just Work:
$ ping -c1 mysql.bridge.docker
PING mysql.bridge.docker(172.17.0.2)56(84)个字节数据。
从172.17.0.2的64字节:icmp_seq = 1 ttl = 64时间= 0.087毫秒
--- mysql.bridge.docker ping统计---
1个数据包传输, 1收到,0%丢包,时间0ms
rtt最小/ avg / max / mdev = 0.087 / 0.087 / 0.087 / 0.000 ms
I have the Docker version 1.10 with embedded DNS service.
I have created two service containers in my docker-compose file. They are reachable each other by hostname and by IP, but when I would like reach one of them from the host machine, it doesn't work, it works only with IP but not with hostname.
So, is it possible to access a docker container from the host machine by it's hostname in the Docker 1.10, please?
Update:
docker-compose.yml
version: '2'
services:
service_a:
image: nginx
container_name: docker_a
ports:
- 8080:80
service_b:
image: nginx
container_name: docker_b
ports:
- 8081:80
then I start it by command: docker-compose up --force-recreate
when I run:
docker exec -i -t docker_a ping -c4 docker_b
- it worksdocker exec -i -t docker_b ping -c4 docker_a
- it worksping 172.19.0.2
- it works (172.19.0.2
isdocker_b
's ip)ping docker_a
- fails
The result of the docker network inspect test_default
is
[
{
"Name": "test_default",
"Id": "f6436ef4a2cd4c09ffdee82b0d0b47f96dd5aee3e1bde068376dd26f81e79712",
"Scope": "local",
"Driver": "bridge",
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1/16"
}
]
},
"Containers": {
"a9f13f023761123115fcb2b454d3fd21666b8e1e0637f134026c44a7a84f1b0b": {
"Name": "docker_a",
"EndpointID": "a5c8e08feda96d0de8f7c6203f2707dd3f9f6c3a64666126055b16a3908fafed",
"MacAddress": "02:42:ac:13:00:03",
"IPv4Address": "172.19.0.3/16",
"IPv6Address": ""
},
"c6532af99f691659b452c1cbf1693731a75cdfab9ea50428d9c99dd09c3e9a40": {
"Name": "docker_b",
"EndpointID": "28a1877a0fdbaeb8d33a290e5a5768edc737d069d23ef9bbcc1d64cfe5fbe312",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16",
"IPv6Address": ""
}
},
"Options": {}
}
]
Here's what I do.
I wrote a Python script called dnsthing, which listens to the Docker events API for containers starting or stopping. It maintains a hosts
-style file with the names and addresses of containers. Containers are named <container_name>.<network>.docker
, so for example if I run this:
docker run --rm --name mysql -e MYSQL_ROOT_PASSWORD=secret mysql
I get this:
172.17.0.2 mysql.bridge.docker
I then run a dnsmasq
process pointing at this hosts
file. Specifically, I run a dnsmasq instance using the following configuration:
listen-address=172.31.255.253
bind-interfaces
addn-hosts=/run/dnsmasq/docker.hosts
local=/docker/
no-hosts
no-resolv
And I run the dnsthing
script like this:
dnsthing -c "systemctl restart dnsmasq_docker" \
-H /run/dnsmasq/docker.hosts --verbose
So:
dnsthing
updates/run/dnsmasq/docker.hosts
as containers stop/start- After an update,
dnsthing
runssystemctl restart dnsmasq_docker
dnsmasq_docker
runsdnsmasq
using the above configuration, bound to a local bridge interface with the address172.31.255.253
.The "main" dnsmasq process on my system, maintained by NetworkManager, uses this configuration from
/etc/NetworkManager/dnsmasq.d/dockerdns
:server=/docker/172.31.255.253
That tells dnsmasq to pass all requests for hosts in the
.docker
domain to thedocker_dnsmasq
service.
This obviously requires a bit of setup to put everything together, but after that it seems to Just Work:
$ ping -c1 mysql.bridge.docker
PING mysql.bridge.docker (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.087 ms
--- mysql.bridge.docker ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.087/0.087/0.087/0.000 ms
这篇关于Docker 1.10通过主机从主机访问容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!