How to Host Multiple Docker Containers with Nginx Reverse Proxy on Ubuntu 21.04

To Host Multiple Docker Containers with Nginx Reverse Proxy on Ubuntu 21.04

Introduction:

Docker is an open source software platform to create, deploy and manage virtualized application containers. A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server

Installation Procedure

Step 1:Check the OS version by using the below command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Install Nginx to the Host system by using the below command

root@linuxhelp:~# apt install nginx -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following package was automatically installed and is no longer required:
  libllvm11
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
  libnginx-mod-http-geoip2 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libnginx-mod-stream-geoip2 nginx-common nginx-core
Suggested packages:
  fcgiwrap nginx-doc
The following NEW packages will be installed:
  libnginx-mod-http-geoip2 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libnginx-mod-stream-geoip2 nginx nginx-common nginx-core
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 645 kB of archives.
After this operation, 2,382 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 nginx-common all 1.18.0-6ubuntu8.2 [38.7 kB]
Setting up libnginx-mod-http-image-filter (1.18.0-6ubuntu8.2) ...
Setting up libnginx-mod-stream (1.18.0-6ubuntu8.2) ...
Setting up libnginx-mod-stream-geoip2 (1.18.0-6ubuntu8.2) ...
Setting up nginx-core (1.18.0-6ubuntu8.2) ...
 * Upgrading binary nginx                                                                                       [ OK ] 
Setting up nginx (1.18.0-6ubuntu8.2) ...
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for ufw (0.36-7.1ubuntu1) ...

Step 3: Create the Httpd image from the Docker Hub by using the below command

root@linuxhelp:~# docker create httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete 
dcc4698797c8: Pull complete 
41c22baa66ec: Pull complete 
67283bbdd4a0: Pull complete 
d982c879c57e: Pull complete 
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
48b57d2183886950e0c143ee15431857c58408269c1554c9d3b917f7abbe0c2d

Step 4: Run a container from Httpd image named “container1” by using the below command

root@linuxhelp:~# docker run -dit --name container1 -p 8080:80 httpd
7142c8363839a4d61fcf690f5e61cfc00dab36c73905329bc85f51d2b4f5c009

Step 5: Run a container from Httpd image named “container1” by using the below command

root@linuxhelp:~# docker run -dit --name container2 -p 8081:80 httpd
8356d6fc71d020e59efac50c7bd680d84db3ef3605550a207f1f299377971fdf

Step 6: View the running containers by using the below command

root@linuxhelp:~# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                                   NAMES
8356d6fc71d0   httpd     "httpd-foreground"   10 seconds ago   Up 9 seconds    0.0.0.0:8081->80/tcp, :::8081->80/tcp   container2
7142c8363839   httpd     "httpd-foreground"   47 seconds ago   Up 46 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   container1

Step 7: Change the contents of the default Nginx web page to the “container1” by using the below command

root@linuxhelp:~# docker exec 7142c8363839 sed -i 's/It works!/Container 1/' /usr/local/apache2/htdocs/index.html

Step 8: Change the contents of the default Nginx web page to the “container2” by using the below command

root@linuxhelp:~# docker exec 8356d6fc71d0 sed -i 's/It works!/Container 2/' /usr/local/apache2/htdocs/index.html

Step 9: Create a Server Block for container1 by using the below command

root@linuxhelp:~# vi /etc/nginx/sites-available/test1.conf


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

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

Step 10:Creating a Server Block for container2

root@linuxhelp:~# vi /etc/nginx/sites-available/test2.conf


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

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

Step 11: Enable the Server Block by creating short link of Server Block file in sites enabled directory by using the below command

root@linuxhelp:~# ln -s /etc/nginx/sites-available/test1.conf /etc/nginx/sites-enabled/

Step 12: Enable the Server Block by creating short link of Server Block file in sites enabled directory by using the below command

root@linuxhelp:~# ln -s /etc/nginx/sites-available/test2.conf /etc/nginx/sites-enabled/

Step 13: Test the Nginx Configuration by using the below command

root@linuxhelp:~# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 14: Restart the Nginx by using the below command

root@linuxhelp:~# systemctl restart nginx

Step 15: Ping container1.test.com in Browser

Step 16: Ping container2.test.com in Browser

By this to Host Multiple Docker Containers with Nginx Reverse Proxy on Ubuntu 21.04 has been completed.

Tag : Docker
FAQ
Q
How to Map the container port to the host port?
A
To Map the container port to the host port use flag docker run -p with :
Q
What is the purpose of Flag -t on docker run Command?
A
The purpose of Flag -t on docker run Command is to Allocate a pseudo-TTY
Q
How to execute a command on Container?
A
To execute a command on Container by "docker exec "
Q
How to enable the Nginx Server Block?
A
To enable the Nginx Server Block by "ls -s /etc/nginx/sites-available /etc/nginx/sites-enabled"
Q
What is a Reverse proxy server?
A
A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network.