Search code examples
dockerautomated-testse2e-testingweb-testingtestcafe

How can I make a request coming from inside of a docker container appear to be coming from my local machine?


A browser running in docker container needs to make a POST to a login service running on a test API in our network. The service is very picky about where POST can come from so it's rejecting the POST because it's coming from host.docker.internal instead of localhost.company.com.

It's very unlikely I'd be able to get host.docker.internal added to the whitelist.

The POST will work fine if the browser is running on my local machine but fails when the browser is running inside a container on my local machine.

I've tried docker run --add-host='localhost.mycompany.com:127.0.0.1' and docker run --add-host='localhost:127.0.0.1', neither one worked. The latter seems silly; it was kind of a shot in the dark...

A possible further complication: the browser is running in testcafe inside a Docker container, so my request will have headers like 'Origin: http://172.17.0.2:1337' 'Referer: http://172.17.0.2:1337/WBrtZV38p/http://host.docker.internal:3000/app/'

Short of making a proxy of some sort on my local machine, is there a way to make a POST from the docker container appear to be coming from my local machine?


Solution

  • Start container in the host OS network space with docker run --network host ... - container will be running in the network of your local machine directly. But you will lose container network isolation, so you should to review security of this approach.

    Doc: https://docs.docker.com/network/host/