theCode
theCode

Reputation: 2347

How to setup a simple reverse proxy in docker?

I am new in docker. I have of applications running on multiple container. Now, I would like to publish all my apps. What I am planning to do is do make a cluster containning all my application. I want at least 4 containers.

  1. Nginx container that is facing internet like a reverse proxy. He is responsible to redirect traffic to other containers, since there are not directly accessible through internet.
  2. Node_js container that publishes a web in nodejs (http://www.node-app.me).
  3. java_EE container that publishes Java EE application (http://www.java_ee-app.me).
  4. Django container that publishes a Django application(http://www.django-app.me).

This is the idear I have, but I don't no how to set nginx container to play the proxy role and make the request to the correct container so that if user send a request like http://www.node-app.me, the container nginx will return result from Node_js, and so on. Can you please give idear on where to start ?

The setup could look like this (sorry I am not very good at drawing) : enter image description here

Upvotes: 0

Views: 1796

Answers (1)

King Chung Huang
King Chung Huang

Reputation: 5594

Unless you have a specific need for nginx, I suggest you use Træfik to do the reverse proxy. It can be configured to dynamically pick up reverse proxy rules via labels on your containers. Here's a basic example.

First, create a common network for Træfik and your three containers.

docker network create traefik

Run Træfik with port 80 exposed and the docker backend enabled.

docker run --name traefik \
-p 80:80 \
-v /var/run/docker.sock:/var/run/docker.sock \
--network traefik \
traefik:1.2.3-alpine \
--entryPoints='Name:http Address::80' \
--docker \
--docker.watch

Run your three services with the appropriate labels. Make sure they share a common network with Træfik so that Træfik can reach it. The node_js one might look something like this.

docker run --name node_js \
--network traefik \
--label 'traefik.frontend.rule=Host:www.node-app.me' \
--label 'traefik.frontend.entryPoints=http' \
--label 'traefik.port=80' \
--label 'traefik.protocol=http' \
your_node_js_image

Træfik will dynamically create a frontend rule that matches on the Host header for www.node-app.me when it sees this container running. The traefik.port and traefik.protocol labels let Træfik know how to communicate with your container.

See the documentation for Træfik's Docker backend for more options and details.

Upvotes: 3

Related Questions