How do docker respond to a domain that does not exist in / etc / hosts?

1

In my hosts file I have only the following following domains:

127.0.0.1   localhost
127.0.0.1   abc.com

When I run the image I use the following command: docker run -p 80:80 -v /Users/userName/Documents/siteABC:/var/www/html my_image . I can type in the URL abc.com or localhost and the site works using the docker server, as expected.

I was wondering if there is any way to dynamically build the host when I squeeze the docker image.

Example I would like to be able to type in the URL abcphp56.com without having to create this domain in my /etc/hosts .

According to this post , if I understood correctly, run the image with the following command: docker run -p 80:80 --add-host abcphp56.com:127.0.0.1 -v /Users/userName/Documents/siteABC:/var/www/html my_image and I would have the system responsive to the 3 domains: localhost , abc.com and abcphp56.com >, but it is not working.

How could I do this?

    
asked by anonymous 17.11.2017 / 18:09

2 answers

1

What you are trying to do is not possible without you creating some sort of script that does recurrent searches in docker to create the configuration of your host's hosts file.

When we talk about --add-host or when we think about name resolution inside docker, we are talking exclusively about docker, not your host. Regardless of whether you are on a windows or linux host, docker does not interfere with your host hosts file.

So one solution to your question is to do pooling in docker to look for modifications (new containers) and re-create hosts (hosts) based on existing containers.

Another solution is to use a service discovery such as etcd, consul, linkerd (the main ones have their own DNS), and add an entry in your host's network configuration to use this DNS server as secondary dns in your network stack the host. You need to test this solution calmly, but in theory it would work.

    
20.11.2017 / 03:39
0

Also add - network="host" .

- add-host is an option that can be used with the network in host mode.

By default, the container network is created in bridge mode.

Noting that:
in host mode you will have better network performance, but in contrast this mode is considered unsafe and should be used only in cases where performance is essential (eg a Load Balancer in production)

docker run -p 80:80 --network="host" --add-host abcphp56.com:127.0.0.1 -v /Users/userName/Documents/siteABC:/var/www/html my_image

Alternative 2

You can create links between containers. When you create links, you can refer to the other container by an Alias. When using link, the file / etc / hosts is changed and assigns the alias to the ip of the container. Example:
docker run -d --name backend imagembackend
docker run -d --name container_hosts --link backend:www.google.com imagem ping www.google.com

Within the container_hosts the / etc / hosts file will have the wwww.google.com entry pointing to the backend IP.

    
19.11.2017 / 03:26