To create and recreate our development environment we are using Docker containers that are started and managed using docker-compose. This works fine in most cases. The only hard thing to manage was access to the webserver and other services. This is because a new IP address is assigned to a Docker container each time it is started.
To fix this issue we searched for a DNS solution that would automatically be configured when an new container is started (e.g dnsmasq). We found this solution in docker-gen. This awsome tool has more than only service discovery. But for now we are only using the service discovery part. And of course we are using docker to run that. The docker-dns container created by jderusse does all we need at the moment.
Setup in ubuntu
Configure the local dnsmasq to forward request to 127.0.0.1:54 And listen to interfaces lo and docker0.
[sourcecode language=”bash”]
sudo vim /etc/NetworkManager/dnsmasq.d/01_docker
bind-interfaces
interface=lo
interface=docker0
server=/docker/127.0.0.1#54
sudo systemctl restart NetworkManager
[/sourcecode]
Run dns-gen and bind port 53 to port 54 of your host
[sourcecode language=”bash”]
docker run –detach –name dns-gen \
–restart always \
–publish 54:53/udp \
–volume /var/run/docker.sock:/var/run/docker.sock \
jderusse/dns-gen -R
[/sourcecode]
That was all we needed to do to have our project webservers available at webserver.project.docker.
If you are running a more barebones linux without dnsmasq and NetworkManager this is probably the easiest way to get it working:
(0. Skip the NetworkManager config since it does not apply)
1. start the container instead with “–publish 53:53/udp”
2. add 127.0.0.1 to /etc/resolv.conf
More recent versions of ubuntu don’t have dnsmasq installed by default. This might also be applicable for other distro’s that do not have dnsmasq installed but have systemd-revolved enabled.
I found out that systemd-resolved doesn’t support domain specific dns lookups to a specific server. Easiest work around was to install dnsmasq.
sudo apt-get install dnsmasq
sudo systemctl enable dnsmasq
sudo systemctl start dnsmasq
you might need to reboot your system to apply all changes correctly
Get your docker ip using this command
$ /sbin/ifconfig docker0 | grep “inet” | head -n1 | awk ‘{ print $2}’ | cut -d: -f2
example output:- 192.168.0.1
then use
docker run –detach \
–name dns-gen \
–publish 172.17.42.1:53:53/udp \
–volume /var/run/docker.sock:/var/run/docker.sock \
jderusse/dns-gen
and add
echo “nameserver 192.168.0.1” | sudo tee –append /etc/resolvconf/resolv.conf.d/head
it should be -publish 192.168.0.1:53:53/udp \
instead of -publish 172.17.42.1:53:53/udp \
When using a Linux version with systemd-resolve you can link the dns-gen DNS server container created as described above with a simple command:
sudo systemd-resolve –set-dns=IPADDRESS –set-domain=docker –interface docker0
replacing IPADDRESS with the IP address dns-gen is binded to
To get this working on Fedora 30:
1. Execute the steps as defined in the article
2. Change the NetworkManager so that it uses dnsmasq:
2a. sudo vim /etc/NetworkManager/NetworkManager.conf
2b. Add “dns=dnsmasq” to the [main] section.
2c. Restart the network manager: sudo systemctl restart NetworkManager
Done.