Docker – How to proxy API request to the docker container

apache-2.2dockerhaproxyPROXY

I have a docker container that is running a node.js application. From the system the container is running on I can do things like …

curl http://<IP address of my docker container>/users ....

… and they the expected json response. Now I have installed an apache web server on my system I have some angular.js where I am attempting to communicate to the REST API on the docker container. But the docker container, of course, is not accessible from the rest of the hosts on my network. Is there an easy way to set up some sort of proxy? I am kinda of familiar with HAProxy. Could I set up HAProxy somehow?

UPDATE:

I start my docker container like so:

[red@vm-red ~]$ sudo docker run -p 3000:8080 -t -i redsimage
...

From the command line on host vm-red I can execute this curl …

[red@vm-red ~]$ curl -H "Content-Type: application/json" http://172.17.0.6:3000/user

… and get the expected results. But if I use something like 'postman' from my laptop or my browsers on my laptop and point them to http://vm-red:8080/user I get nothing. What am I doing wrong?

Best Answer

You can easily map a port in you docker container to host machines port. For example say your host machines ip is 10.20.30.40, you can make your api in docker machine available in that ip by mapping the api's port to port 80 of the host machine. Now if you are already have something hosted in the host machine, then you can map it to some other port, say 8080.

By mapping the docker images port to port 80 on the host, your api will be accessible at 10.20.30.40/user, but if you map it to a different port like 8080, your api will be accessible at 10.20.30.40:8080/users

This can be done using the simple command below:

# Maps the container port to a random host port.
docker run -d -P yourImage 

# Maps the host port of 8080 to the container port 80
docker run -d -p "8080:80" yourImage 

you could read more about it here.