Docker Community Forums

Share and learn in the Docker community.

Auto-assign container IP to host hostname


(Yogesh Pandit) #1

Hi,

I use a single hostname for all my web-services on the Docker image. Lets say, “sandbox”. Currently whenever I run the container I pass -h sandbox to docker run and then edit /etc/hosts on host with sandbox mapped to the container IP. Web-service on the container are routed through nginx so only port 80 is exposed to the host.

I am guessing there is a better (not manual) way to achieve this. Any suggestions would be great.
Thanks!


(Jeff Anderson) #2

I haven’t personally used it, but the dnsdock project might be what you’re looking for.

It basically takes your docker container IPs and publishes them in a dns service.

I am a bit curious why you are connecting from the docker host directly to the container ip addresses instead of using the port publishing features. For example, I could do docker run -d -p 80:80 nginx instead of docker run -d nginx, and access my nginx service on my host’s port 80. I can even add multiple IPs to my host and bind different docker container services to different IPs.

Is there anything you can share on your use case for taking this approach?


(Yogesh Pandit) #3

Thanks Jeff. I will look into dnsdock.
Just using localhost in the container never worked for me. I am new to using docker and this worked for me so I kinda got stuck with it. When you do this - docker run -d -p 80:80 nginx what is the host for nginx service on the container?


(Jeff Anderson) #4

When you do -p 80:80 you are implying 0.0.0.0:80 on the docker host, which is an alias for all interfaces on the host.

So if my docker engine is running on some remote host, say 192.168.99.103, then I could reach that service by going to 192.168.99.103:80.

If the docker engine is running on the host that I am on, then 127.0.0.1 would work too.

You can even specify which host ip to bind to when you launch the container: docker run -d -p 192.168.99.103:8080:80 nginx would bind to the docker host’s 192.168.99.103 on port 8080, and connect that up to the container’s port 80. That would mean the service is only accessible if you can connect to 192.168.99.103. Trying to connect to that host’s 127.0.0.1 wouldn’t work.


(Yogesh Pandit) #5

Thanks a lot Jeff. This works well for what I need. Thanks for the explanation too; I understand things better now.