How do D-Ocker respond to a domain that does not exist in /etc/hosts?

Asked

Viewed 423 times

1

In my hosts file I have only the following domains:

127.0.0.1   localhost
127.0.0.1   abc.com

When I will rotate 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 create the host dynamically when I rotate the Docker image.

Example I wish I could write to the URL abcphp56.com without me having to create that domain in my /etc/hosts.

According to this post, if I understood correctly, just rotate 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 with. and abcphp56.com, but it’s not working.

inserir a descrição da imagem aqui

How could I do that?

2 answers

1


What you are trying to do is not possible without you creating some kind of script that does recurring searches on Docker to create your host’s hosts file configuration.

When we talk about --add-host or when we think about the resolution of names within Docker, we are talking exclusively about Docker, not your host. Whether you’re on a windows or linux host, Docker doesn’t interfere with your host hosts file.

Therefore, a solution to your question is to poole on Docker to search for modifications (new containers) and recreate the hosts file (from the host) based on existing containers.

Another solution is to use a Discovery service such as etcd, Consul, linkerd (the main ones have their own DNS), and add an entry in their host network configuration so they use that DNS server as secondary dns in their host network stack. It is necessary to test this solution calmly, but in theory it would work.

  • So Luiz, this was a request from the programmer manager here at the company. So usually programmers access the system as abc.com. This container will be used as the default for all programmers, but he does not want each of the programmers to have to go to the /etc/hosts to edit some dns. He asked to do something like, create a list of dns inside the container that will respond when the user writes one of these urls in the host browser. So the list of dns already comes from the image.

  • So from what I understand from your post, there’s no escape from editing the host hosts, right? Be automatic or manual.

  • Dendro from your company you have a DNS, so doing on your DNS is all solved. I believe that your programmer does not know what you are asking, maybe I can help you: @luizcarlosfaria on Telegram. I think at the end of the day, he’s asking for something that could be simpler.

0

Also add --network="host".

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

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

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 (for example, 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 /etc/hosts file is changed and assigns the alias to the container ip.
Example:
docker run -d --name backend imagembackend
docker run -d --name container_hosts --link backend:www.google.com imagem ping www.google.com

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

  • Hi Senio, I’m a beginner in Docker. From what I understand from the documentation --add-host seems to insert DNS into the /etc/hosts inside the container. And as I see the error in the browser the DNS cannot be found.

  • I added an alternative 2

  • 1

    Noting that these alternatives are to start your understanding about Docker without using more complete solutions like a Service Discovery, Scalability, etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.