1

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.

Régis B.
  • 10,092
  • 6
  • 54
  • 90
  • You could also assign static ip address to the container as discussed [here](https://stackoverflow.com/questions/39493490/provide-static-ip-to-docker-containers-via-docker-compose) – Ayushya Jul 23 '17 at 20:29

2 Answers2

2

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.

Alexandre Fenyo
  • 4,526
  • 1
  • 17
  • 24
-1

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
Siddharth Kumar
  • 2,672
  • 1
  • 17
  • 24
  • Unless I misunderstand your answer, this is not really what I need. I need to access the container from the host, not the opposite. – Régis B. Dec 15 '17 at 13:19