How to access webserver running on localhost from a docker container on a network?

I have the following system configuration:

  • Docker container running on user defined network
  • docker-machine (with VirtualBox on OS:X forwarding port 9000 to 9000)
  • Local webserver running on http://localhost:9000

I do not know how to make a basic http request against this webserver, from within my docker container.

  • Development workflow on Minikube and docker
  • Using network_mode='host' in docker-compose break run: host type networking can't be used with links
  • docker-compose volume not appearing in container
  • Docker registry notifications multiple push
  • Can't access pod logs
  • Windows based couchbase image for docker
  • To test this I am using:

    docker exec testcontainer curl --data "foobaz=foo" http://{hostname}:9000/

    where I have tried, for hostnames:

    • ‘localhost’
    • ‘’
    • ‘’ (docker-machine IP)

    Each time I receive errors or timeouts. When I run the curl command locally (not in docker and on my host OS:X machine) I am able to successfully post the http request.

    I cannot disconnect the docker container from my user-defined network. I also cannot add my webserver to that network, as it is not running in a container. Also, I know it is trivial to connect the other way (curl to a webserver running in a docker container) but this is not my use case.

    How can I successfully route that http request from the docker container which is part of a user defined network to my localhost webserver?

  • Html links to other pages, images and Css not working in Docker container?
  • Connect with ssh to docker daemon on windows
  • How to recover data from a deleted Docker container? How to reconnect it to the data?
  • Cannot find the enviromental parameter
  • Container process on host machine
  • docker-compose with a war via volume fails on digital ocean debian but not other debian distros (my home box, work box as well)
  • 2 Solutions collect form web for “How to access webserver running on localhost from a docker container on a network?”

    You can do this with the actual IP address of your local computer.

    So for example, if your en0 IP is on your host OS, you can run:

    docker exec testcontainer curl --data "foobaz=foo"

    which will successfully allow you to make the http requests.

    Note that if you are doing this from a container on the host docker network, this is trivial, as you can directly access localhost or without having to use the actual machine IP.

    You might want to check what the IP address of the container is – you can find this out by running docker inspect.
    However, if you want to access the server process running in your container using the docker machine IP, then you should “expose” port 9000 that your contained app is listening on (using Dockerfile) – in this way, you will still need to figure out which port the 9000 container port is mapped to (this shows when you list your containers via $ docker ps. You can also specify port binding option in command line when starting your container like this: $ docker run -p 9000:9000 <your-container>.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.