How to reach build host via docker container using a hostname?

use-case:

The host runs an integration test for a container that involves checking whether a webhook is called by the container or not.

On my local machine, I can use host.docker.internal as the hostname for the docker host.

On Travis this does not work. I tried adding the following to my travis configuration, but it did not change anything:

addons:
  hosts:
    - host.docker.internal

Alternative Approach:

An alternative would be to run the tests inside a container aswell (and link those via a bridge network), however, as this is done for a framework that needs to support both I would like to have a solution that allows running tests inside an docker container aswell as outside a docker container. Since this is already possible with Docker Desktop (using host.docker.internal), I assume there must be a way to do the same with travis.

According to

this feature only exists in Docker for Windows and Docker for Mac.

1 Like

Thank you for the resource!

I set up my own workaround:

docker exec ${runner.containerId} \
   /bin/sh -c "ip -4 route list match 0/0 | awk '{print $3" host.docker.internal"}' >> /etc/hosts"

It seems a bit more robust than the one mentioned in the article you shared.

Imprint