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?