设置Docker Dnsmasq [英] Setting Up Docker Dnsmasq

查看:200
本文介绍了设置Docker Dnsmasq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个docker dnsmasq容器,以便我可以让我的所有Docker容器查找域名,而不是使用硬编码的IP(如果它们在同一台主机上)。这解决了一个问题:无法更改docker 容器中的/ etc / hosts文件,这样可以通过更改dnsmasq容器引用的单个文件来轻松更新所有容器。 p>

看起来有人已经为我做了艰苦的工作,并创建了一个 dnsmasq容器。不幸的是,这不是我的工作。我写了一个bash脚本启动容器,如下所示:

  name =dnsmasq_
timenow = $日期+%s)
name =$ name $ timenow

sudo docker运行\
-v =$(pwd)/dnsmasq.hosts:/dnsmasq。主机\
--name = $ name \
-p ='127.0.0.1:53:5353 / udp'\
-d sroegner / dnsmasq

在运行之前,我创建了dnsmasq.hosts目录,并在其中插入了一个名为hosts.txt的单个文件,内容如下:

  192.168.1.3 database.mydomain.com 

不幸的是,每当我尝试从内部ping该域名:




  • 主机

  • dnsmasq容器

  • 同一主机上的另一个容器



我总是收到 ping:unknown host 错误信息。



我尝试在没有守护进程模式的情况下启动dnsmasq容器,所以我可以调试其输出,如下所示:

  dnsmasq:started,版本2.59 cachesize 150 
dnsmasq:编译时间选项:IPv6 GNU-getopt DBus i18n DHCP TFTP conntrack IDN
dnsmasq:读取/etc/resolv.dnsmasq .conf
dnsmasq:using nameserver 8.8.8.8#53
dnsmasq:read / etc / hosts - 7个地址
dnsmasq:读取/dnsmasq.hosts//hosts.txt - 1个地址

我猜测我没有指定 -p 参数正确启动容器。 有人可以告诉我其他docker容器查找DNS应该是什么,或者我想要做什么实际上是不可能的?

解决方案

Docker dnsmasq服务的构建脚本需要更改,以便绑定到您的服务器的公共IP ,在本例中为192.168.1.12 eth0界面

 #!/ bin / bash 

NIC =eth0

name =dnsmasq_
timenow = $(date +%s)
name =$ name $ timenow

MY_IP = $(ifconfig $ NIC | grep 'inet addr:'| grep -v'127.0.0.1'| cut -d:-f2 | awk'{print $ 1}'


sudo docker run \
-v =$(pwd)/dnsmasq.hosts:/dnsmasq.hosts\
--name = $ name \
-p = $ MY_IP:53:5353 / udp \
-d sroegner / dnsmasq

在主机上(在这种情况下是ubuntu 12),你需要更新resolv.conf或/ etc / network / interfaces文件,以便有注册将您的公共IP(eth0或eth1设备)用作名称服务器。





您可能希望在容器未运行时将辅助名称服务器设置为google,通过将行更改为 dns-nameservers xxx.xxx.xxx.xxx 8.8.8.8 例如没有逗号或另一行。



然后您需要重新启动网络服务 sudo /etc/init.d/networking restart 如果您更新了/ etc / network / interfaces文件,以便自动更新/etc/resolve.conf文件,该版本将在版本中复制到该容器。



现在重新启动所有容器




  • sudo docker stop $ CONTAINER_ID

  • sudo docker start $ CONTAINER_ID



这将导致他们的/etc/resolv.conf文件更新,以便它们指向新的名称服务器设置。



您在所有Docker容器(您制作完成后)中的DNS查找更改)现在应该使用您的dnsmasq容器!



作为附注,这意味着其他主机上的docker容器也可以利用的主机上的dnsmasq服务,只要其主机的名称服务器设置设置为使用此服务r的公共知识产权


I'm trying to set up a docker dnsmasq container so that I can have all my docker containers look up the domain names rather than having hard-coded IPs (if they are on the same host). This fixes an issue with the fact that one cannot alter the /etc/hosts file in docker containers, and this allows me to easily update all my containers in one go, by altering a single file that the dnsmasq container references.

It looks like someone has already done the hard work for me and created a dnsmasq container. Unfortunately, it is not "working" for me. I wrote a bash script to start the container as shown below:

name="dnsmasq_"
timenow=$(date +%s)
name="$name$timenow"

sudo docker run \
-v="$(pwd)/dnsmasq.hosts:/dnsmasq.hosts" \
--name=$name \
-p='127.0.0.1:53:5353/udp' \
-d sroegner/dnsmasq

Before running that, I created the dnsmasq.hosts directory and inserted a single file within it called hosts.txt with the following contents:

192.168.1.3 database.mydomain.com

Unfortunately whenever I try to ping that domain from within:

  • the host
  • The dnsmasq container
  • another container on the same host

I always receive the ping: unknown host error message.

I tried starting the dnsmasq container without daemon mode so I could debug its output, which is below:

dnsmasq: started, version 2.59 cachesize 150
dnsmasq: compile time options: IPv6 GNU-getopt DBus i18n DHCP TFTP conntrack IDN
dnsmasq: reading /etc/resolv.dnsmasq.conf
dnsmasq: using nameserver 8.8.8.8#53
dnsmasq: read /etc/hosts - 7 addresses
dnsmasq: read /dnsmasq.hosts//hosts.txt - 1 addresses

I am guessing that I have not specified the -p parameter correctly when starting the container. Can somebody tell me what it should be for other docker containers to lookup the DNS, or whether what I am trying to do is actually impossible?

解决方案

The build script for the docker dnsmasq service needs to be changed in order to bind to your server's public IP, which in this case is 192.168.1.12 on my eth0 interface

#!/bin/bash

NIC="eth0"

name="dnsmasq_"
timenow=$(date +%s)
name="$name$timenow"

MY_IP=$(ifconfig $NIC | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')


sudo docker run \
-v="$(pwd)/dnsmasq.hosts:/dnsmasq.hosts" \
--name=$name \
-p=$MY_IP:53:5353/udp \
-d sroegner/dnsmasq

On the host (in this case ubuntu 12), you need to update the resolv.conf or /etc/network/interfaces file so that you have registered your public IP (eth0 or eth1 device) as the nameserver.

You may want to set a secondary nameserver to be google for whenever the container is not running, by changing the line to be dns-nameservers xxx.xxx.xxx.xxx 8.8.8.8 E.g. there is no comma or another line.

You then need to restart your networking service sudo /etc/init.d/networking restart if you updated the /etc/network/interfaces file so that this auto updates the /etc/resolve.conf file that docker will copy to the container during the build.

Now restart all of your containers

  • sudo docker stop $CONTAINER_ID
  • sudo docker start $CONTAINER_ID

This causes their /etc/resolv.conf files update so they point to the new nameserver settings.

DNS lookups in all your docker containers (that you built since making the changes) should now work using your dnsmasq container!

As a side note, this means that docker containers on other hosts can also take advantage of your dnsmasq service on this host as long as their host's nameserver settings is set to using this server's public IP.

这篇关于设置Docker Dnsmasq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆