Site icon LCDUNG

How to configure Nginx reverse proxy to host multiple Docker containers on a Single Server Cluster

I. The reason we need it!

I need server cluster host multiple Docker containers for multiple service like the system above.

II. How to make it?

1. Installing platform

Server cluster and server node, we have to install PacemakerCorosync, and Pcs on Ubuntu 18.04.

Next, Install Docker and Nginx for server node.

You can view detail here: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04

2. Run your Docker containers

For the same of simplicity, I will run a simple and I’ll run 2 small httpd containers.

docker run -dit --name container-1 -p 8080:80 httpd:2.4

Now if you visit http://your-dropets-ip:8080, you should be able to see a message saying It Works!.

Just so that we could differentiate the two containers, let’s update the It works! message with Container 1 for example:

docker ps

Then run the following sed command to update the message:

docker exec CONTAINER_ID sed -i 's/It works!/Container 1/' /usr/local/apache2/htdocs/index.html

This would basically run a search and replace for the It works! string and update it with Container 1 in the default index.html file in the container itself.

If you visit your Droplet’s IP again in your browser the message should change from It works! to Container 1.

Let’s do the same thing for container 2, but map it to port 8081 instead:

docker run -dit --name container-2 -p 8081:80 httpd:2.4

Then agian get your container ID

docker ps

Then run the sed command again to update the It works! message to Container 2:

docker exec CONTAINER_ID sed -i 's/It works!/Container 2/' /usr/local/apache2/htdocs/index.html

Now if you visit http://your-dropets-ip:8081, you should be able to see a message saying Container 2.

You can view detail here: https://www.digitalocean.com/community/questions/how-to-host-multiple-docker-containers-on-a-single-droplet-with-nginx-reverse-proxy

3. Configure Nginx

Now that we have our containers up and running we can go ahead and configure our Nginx server blocks, I will go ahead and use the following two subdomain names for this example:

To keep things as simple as possible, I will create 2 server blocks with the following content:

Create a new file called container1.bobbyiliev.com.conf in the /etc/nginx/sites-available/ directory and add the following content:

server {
  listen        80;
  server_name   container1.bobbyiliev.com;

  location / {
    proxy_pass  http://localhost:8080;
  }
}

Create a new file called container2.bobbyiliev.com.conf in the /etc/nginx/sites-available/ directory and add the following content:

server {
  listen        80;
  server_name   container2.bobbyiliev.com;

  location / {
    proxy_pass  http://localhost:8081;
  }
}

Then once you have the two config files ready cd to the /etc/nginx/sites-enabled directory, and run the following commands:

ln -s ../sites-available/container1.bobbyiliev.com.conf .

ln -s ../sites-available/container2.bobbyiliev.com.conf .

Run a config test to make sure that there are no errors:

nginx -t

And if you get Syntax OK message, restart Nginx:

systemctl restart nginx

Note, for more information about Nginx server blocks, I would recommend taking a look at this tutorial here: https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

4. Install Certbot on Ubuntu to generate SSL Cert

I would recommend taking a look at here: https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx.html

You can read more here: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

After Install Certbot for domain, we will update Nginx config like sample below:

View Docker container port

III. Final

Exit mobile version