For development, I run Docker containers that start up running sshd. Then I ssh to those machines and do my development work. It works great but finding the host name to ssh into was always a chore. It usually involved something like docker inspect my_container | grep IPAddress
and then copying the address. Very tedious. I'd rather just type ssh root@my_container
. So I did some searching and came up with a solution similar to http://blog.oddbit.com/2013/10/04/automatic-dns-entries-for-libvirt-domains/
What we will do is have incrond create a hosts file every time a container is started or stopped. Then we will set NetworkManager to use dnsmasq and resolve names through that hosts file.
The below instructions work on Fedora 20. For other distributions, you're on your own, but the principles should be the same.
-
Set NetworkManager to use dnsmasq by adding the following under the
[main]
block of /etc/NetworkManager/NetworkManager.confdns=dnsmasq
-
Add the following to /etc/sysconfig/network-scripts/ifcfg-em1
DOMAIN="default.docker"
-
yum install incrond
-
systemctl enable incrond.service && systemctl start incrond.service
-
Drop the
docker_hosts
shell script into /usr/bin andchmod 755
it -
Create a file under /etc/incron.d and add the text below. Now every time a file gets created, deleted, or modified in /var/lib/NetworkManager, incrond will run the docker_hosts script and send it the name of the applicable file (that's what the $# means). The docker_hosts script will in turn look for running containers, inspect them, and output a hosts file that is then written to /var/lib/dnsmasq/docker.addnhosts.
/var/lib/NetworkManager IN_CREATE,IN_DELETE,IN_MODIFY /usr/bin/docker_hosts $# /var/lib/dnsmasq/docker.addnhosts
-
Now we just need to make dnsmasq aware of the hosts file. Under /etc/NetworkManager/dnsmasq.d create a file and put this in it:
addn-hosts=/var/lib/dnsmasq/docker.addnhosts
-
systemctl restart NetworkManager
-
Now you should be able to just
ssh root@my_container
.
If you want to add ssh settings unique to docker containers (like ForwardAgent or ForwardX11), you can do this:
Host *.default.docker
ForwardX11 yes
ForwardAgent yes
Please note however, that if you do add ssh settings, you must use the fully qualified host name. E.g. ssh root@my_container.default.docker
(because ssh doesn't do DNS lookups)