Régis B.
Régis B.

Reputation: 10608

Point host to Docker container with static domain name

I have setup a web service in a Docker container such that it only responds to HTTP requests made to http://pouac.localhost.

So, currently, when I want to test this Docker container, I have to manually add the IP address of that container to the /etc/hosts file of the host. This IP address changes from one run to another, so I need to get it every time with:

docker inspect mycontainer

... and then add it to /etc/hosts.

It works, but it's a real pain. I'm pretty sure there is a better way to do it.

If I understand correctly, Docker includes a DNS server. So I guess I could try to point the host to the Docker DNS, and that would be a start... but I have no idea at which address runs the Docker DNS.

For information, the host is running Ubuntu 16.04, while the Docker containers are started with docker-compose.

Upvotes: 1

Views: 8669

Answers (2)

Siddharth Kumar
Siddharth Kumar

Reputation: 698

You can try --add-host flag while running docker container example

docker run --add-host=google.com:8.8.8.8 -td <image_name>

the hosts mapping will be appended to /etc/hosts of the container.

example

docker run --rm \
--hostname mycontainer \
--add-host docker.com:127.0.0.1 \
--add-host test:10.10.10.2 \
alpine:latest \
cat /etc/hosts

output

172.17.0.45 mycontainer
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
10.10.10.2 test
127.0.0.1 docker.com

Upvotes: -1

Alexandre Fenyo
Alexandre Fenyo

Reputation: 4809

First, add the following line at the end of /etc/hosts:

127.0.0.1 pouac.localhost

Secondly, expose the TCP port 80 of your container. For this to be done, add something like that in your docker-compose.yml file:

ports:
    - "80:80"

Then, TCP port 80 will be exposed for each IP address of your host, so it will be exposed to TCP port 80 on 127.0.0.1, so connecting to http://pouac.localhost will connect to your container.

Upvotes: 2

Related Questions