How to add container domain names to the Docker Host /etc/hosts file

Today I had a case where I was trying to access a container running Jenkins-CI server from the Docker Host.

X11 is installed on the Docker Host, and to access the Jenkins container I had to enter the IP address http://172.17.0.2:8080/.

I asked Firefox to save the login details to the Jenkins instance running in that container.

This works fine until the IP address of the container gets changed, like when I stopped and started the Jenkins container.

On restart the IP address had changed to something else, like 172.17.0.5:8080/

So Firefox thought this was a different website, and did not populate the password field for me to login to Jenkins-CI as admin.

Looking at the saved passwords in Firefox, I can see the password saved under the first IP address for the running container.

Saving the password again just repeats the password for the next IP address the container has been given.

Is there any way for the Docker Host to save the IP addresses and domain names for all the containers that are running on that host in it’s own /etc/hosts file please, so we can get access to those containers running on that host by domain name, and will not be affected by the IP addresses changing when containers running on that host get their IP addresses changed please?

Possibly in a similar way that the container /etc/hosts files are updated after a container they link to has been stopped and started, and the IP address has changed?

Hope that makes good sense.

TIA

Keith

Did you run your container with the -p option? You should, and then refer to the host’s IP address.

docker run -d -p 12345:8080 me/jenkins
curl http://127.0.0.1:12345

You can pick any port number you want (including the same port as inside the container, 8080) instead of 12345 here.

If you want to access the service from another system, use your physical host’s IP address or DNS name in the URL. If you want to make the service inaccessible from the network at large, -p 127.0.0.1:12345:8080.

(If you are running with Docker Machine, the IP address will be different; the Docker Toolbox VirtualBox setup usually uses 192.168.99.100 in the final URL.)