我正在使用node:4.2.1-wheezy和dnsdock容器运行docker-compose.
我在/ etc / default / docker中有DOCKER_OPTS =“ – dns 172.17.42.1”.
当我在node.js容器中运行node -e“require(‘dns’).resolve(‘host_name_here’)”时,主机通过172.17.42.1 dns服务器正确解析.
但是当我运行node -e“require(‘dns’).lookup(‘host_name_here’)”时它会因ENOTFOUND错误而失败.
问题是http.request使用dns.lookup而不是dns.resolve.
dns.lookup调用getaddrinfo的The docs say.据我所知getaddrinfo缓存/etc/resolv.conf,也许它缓存空/etc/resolv.conf(但cat /etc/resolv.conf打印名称服务器172.17.42.1).
我真的不知道如何解决这个问题.什么可以导致这种行为?
更新1.
docker -v
Docker version 1.7.1,build 786b29d
docker-compose -v
docker-compose version: 1.4.2
更新2.
我将所有内容更新到最新版本(docker 1.9.0,docker-compose 1.5.0和节点高达5.0.0),但问题仍然存在.
所以这是再现问题的docker-compose.yml:
dnsdock:
image: tonistiigi/dnsdock
volumes:
- /var/run/docker.sock:/run/docker.sock
ports:
- "172.17.42.1:53:53/udp"
environment:
- DNSDOCK_ALIAS=dns.org
node:
image: node:5.0.0-wheezy
command: node -e "setTimeout(function () { var dns = require('dns'); dns.resolve('dns.org',console.log.bind(console,'resolve')); dns.lookup('dns.org','lookup')); },5000)"
dns: 172.17.42.1
您应该用docker0接口的IP替换172.17.42.1.需要setTimeout(…,5000),因为节点容器可能在dnsdock之前启动.
这是我的docker-compose up输出:
Creating test_node_1
Creating test_dnsdock_1
Attaching to test_node_1,test_dnsdock_1
dnsdock_1 | 2015/11/07 09:29:44 Added service: 3653951cff40c06c04b9ab3f5d2fc94ccc19305eaac7ba1a545ce1dbab3e3e17 {test_dnsdock_1 dnsdock 172.17.42.3 -1 [dns.org]}
dnsdock_1 | 2015/11/07 09:29:44 Added service: 36577feea136bc713f77b64b2a6a9712cd509c47ca55427f6749308cc5a4b140 {test_node_1 node 172.17.42.2 -1 []}
node_1 | resolve null [ '172.17.42.3' ]
node_1 | lookup { [Error: getaddrinfo ENOTFOUND dns.org]
node_1 | code: 'ENOTFOUND',node_1 | errno: 'ENOTFOUND',node_1 | syscall: 'getaddrinfo',node_1 | hostname: 'dns.org' }
dnsdock_1 | 2015/11/07 09:29:49 Stopped service: 36577feea136bc713f77b64b2a6a9712cd509c47ca55427f6749308cc5a4b140
test_node_1 exited with code 0
它使用基于Consul的KV(键/值)存储,以及可以注册节点的群集.
您可以构建覆盖网络
eval "$(docker-machine env --swarm c0-master)"
docker network create -d overlay myStack1
并使用它来运行图像:
docker run -d --name web --net myStack1 Nginx
docker run -itd --name shell1 --net myStack1 alpine /bin/sh
Both of these containers will be attached to the same network,and be discoverable by container name (regardless of start order).
Further,when a container is restarted it will remain discoverable without cascading restarts.