8

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.

To test this I am using:

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

where I have tried, for hostnames:

  • 'localhost'
  • '127.0.0.1'
  • '192.168.99.100' (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?

3
  • What if you try 0.0.0.0:9000? Commented Apr 19, 2016 at 20:43
  • Sadly not, I don't know (I would be curious to, though), sorry, I wasn't even sure it would work. I'm glad it helped though. :) Commented Apr 19, 2016 at 20:46
  • So it apparently doesn't work. Maybe it has something to do with your iptables? :/ (not sure at all, just searching an explanation too and giving hypothesis) Commented Apr 19, 2016 at 20:57

2 Answers 2

8

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

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

docker exec testcontainer curl --data "foobaz=foo" http://10.100.20.32:9000/

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 0.0.0.0 without having to use the actual machine IP.

Sign up to request clarification or add additional context in comments.

Comments

-1

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>.

2 Comments

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. I am aware of how to do what you are saying. I want to go the opposite way...
Can you confirm if the webserver does accept connections from the localhost at least, and also that you can access the server using its IP address (not localhost or 127.0.0.1). I suspect that perhaps you need to start the web server with the 0.0.0.0 binding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.