Ben
Ben

Reputation: 15

Make an http request to a docker container that lives in cloud with the use of nginx

I am new to docker. I have this docker container run on port 80 and living in a cloud instance. Docker assigns a private IP addresses to its containers by default. I was wondering what would be the necessary actions to take in order to have access to the running docker container outside of my cloud environment (something like http://mydockercontain.org). I have tried to use nginx to recreate a reverse proxy server in the cloud environment but it is not enough for someone outside of the cloud environment to make an http request to the running docker container. I know I have to setup some sort of DNS record mapping to the docker ip address but I'm really new to this technology and don't know what's the right approach to solve this problem.

Upvotes: 0

Views: 764

Answers (1)

ImTryingMyBest
ImTryingMyBest

Reputation: 372

I was in your shoes about 2-3 months ago, and at some point it just all made sense so bear with me here.

My favorite thing to do is to SSH in and run something to get the containerID's and see what containers are indeed running, like:

sudo docker ps -a

Next, run this to get into that container's logs so you can at least visually see what's going on in there:

sudo docker logs -f [CONTAINERID]

So now you'll at least be able to see a little about what's going on inside that given container. using the docker ps -a command, you can see what port it's running on and try to add that port onto the IP address you're trying to reach. ie. xxx.xx.xx.xx:8000 or something like that.

Still can't reach it?

I couldn't at this point... so from here I went back to square 1. In my Dockerfile, I found it easiest to use EXPOSE 80 to expose that port for when I want to build/run the container. Not sure how proficient you are with the DockerFile but it'd be a line that's only:

EXPOSE 80

The last change you might need to make is to control where your application is running. I'm a big python guy, so if its something like flask then you can easily change that in your app.run() by setting the host to 0.0.0.0 like app.run(host=0.0.0.0). Otherwise, your application will be running on 127.0.0.1, which would mean your app would only be reachable from inside the container which is the hard thing to grasp there if you're just starting off with Docker.

Once you have that ready, go back to run a few more commands... just make sure you have those containerID's handy from the docker ps -a command.

if its just one docker container:

sudo docker kill [CONTAINERID] # kill the old container
sudo docker rm [CONTAINERID] # remove the old container
sudo docker build [same build thing as before]
sudo docker run -d -p 80:80 [CONTAINER ID FROM BUILD ABOVE]

by using docker run -d -p 80:80, you're essentially binding it to the TCP/IP port available at the host and usually I can just access it at whatever the hosts IP address is by entering it in as the url. Of course, you may want to set up a VPC or something that restricts access to only a given whitelist of computers that you provide... I'm not entirely what cloud platform you're on but I'd recommend looking into that.

Next, depending on the cloud environment you're on, create a load balancer that can act as a reverse proxy in front of the containers running on those machines. That part is relatively plug and play as long as those containers are both running somewhere that the load balancer can reach them - so you may need to create a mini network/vpc that allows that communication. I'll be honest with you and say I haven't had enough experience in that realm to really give you many solid pointers beyond that.

I hope this helps point you in the right direction at the very least. Just keep on trying new things and experimenting and you'll solve it - trust me I know where you are and eventually you'll hit that point where everything suddenly makes perfect sense.

Upvotes: 2

Related Questions