0

I need to access a domain name during my docker build, say example.com, which is currently running on my host machine (Mac), through the hosts file:

[/etc/hosts]

127.0.0.1      example.com

and exposed on port 8888. If I try to use this ip in the docker build process, it fails to establish the connection:

docker build  --add-host=example.com:127.0.0.1 .

Using my host's local ip address using ipconfig getifaddr en0 also fails:

docker build  --add-host=example.com:$(ipconfig getifaddr en0) .

Presumably this is because my host system is not allowing incoming connections on this port; I am also fine with this, ideally I would not need to open the port up externally to make this work. Indeed I can't even access the resource in the host terminal with this ip, for example with curl $(ipconfig getifaddr en0):8888 fails. However ping $(ipconfig getifaddr en0) does work, verifying the ip address is correct.

Since Docker 20.10, the "host-gateway" token was added to the cli. This means you can now access the host using host.docker.internal (yes, even on linux) with the command

docker build --add-host=host.docker.internal:host-gateway .

However I need to access the host by resolving the local domain example.com and the following does not work for me:

docker build --add-host=example.com:host-gateway .

During the build process we can get the ip address of the host by resolving host.docker.internal however updating the /etc/hosts file in the image, does not work as detailed in this answer. The accepted solution is to use --add-host to the docker build ... command that kicks off the build process.

Before the build process however, when we are able to configure example.com to point to an ip address with docker build --add-host, I don't know how to see where docker.host.internal would point to.

Currently, the solution I have is to run the docker build with the directive

RUN apt-get -y install iputils-ping && ping host.docker.internal

Then I quit the build, copy the ip address output from the directive (192.168.65.2) and into the command

docker build --add-host=example.com:192.168.65.2 .

Now, this does work, but it's a brittle, cumbersome and fairly desperate hack. What would be the best way to achieve this result?

Robino
  • 4,530
  • 3
  • 37
  • 40
  • What do you need to call out to during the build? Can you pass the host name as a Dockerfile `ARG`, and use `--build-arg whatever-host=host.docker.internal` if the external dependency happens to be running on the same machine? (I would avoid trying to set up `/etc/hosts` in favor of making the actual destination host name configurable.) – David Maze Jan 25 '23 at 16:12
  • @DavidMaze the resource is a package repository and is not only hard-coded in the configuration files for the install, but also hard-coded in the trees of dependency files pulled down through the install process. – Robino Jan 25 '23 at 16:25
  • @DavidMaze the resource is **currently** running on the same machine, but I want the ability to move it elsewhere without having to change the process. It allows me to have this process run as an integration / unit test. – Robino Jan 25 '23 at 16:27
  • 1
    If Docker weren't involved, how would you move it? Editing `/etc/hosts` on every system that could run this build seems unreliable; maybe you'd use sed(1) to change those files with the build configuration? – David Maze Jan 25 '23 at 18:25
  • Production will just use DNS. Test environment will use /etc/hosts. – Robino Jan 25 '23 at 19:05

0 Answers0