• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

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

  • 00:35 lsb_release -a
  • 00:53 apt install nginx -y
  • 01:07 docker create httpd
  • 01:46 docker run -dit --name container1 -p 8080:80 httpd
  • 02:30 docker run -dit --name container2 -p 8081:80 httpd
  • 03:10 docker ps
  • 03:26 docker exec 7142c8363839 sed -i 's/It works!/Container 1/' /usr/local/apache2/htdocs/index.html
  • 03:58 docker exec 8356d6fc71d0 sed -i 's/It works!/Container 2/' /usr/local/apache2/htdocs/index.html
  • 04:30 vi /etc/nginx/sites-available/test1.conf
  • 05:05 vi /etc/nginx/sites-available/test2.conf
  • 05:36 ln -s /etc/nginx/sites-available/test1.conf /etc/nginx/sites-enabled/
  • 06:11 ln -s /etc/nginx/sites-available/test2.conf /etc/nginx/sites-enabled/
  • 06:37 nginx -t
  • 06:50 systemctl restart nginx
{{postValue.id}}

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 Snap 1

Step 16: Ping container2.test.com in Browser Snap 2

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

Tags:
aiden
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

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.

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

How to execute a command on Container?

A

To execute a command on Container by "docker exec "

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 Map the container port to the host port?

A

To Map the container port to the host port use flag docker run -p with :

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Luke ?
workbench for debian

I am using workbench in CentOS whereas now I need to use Debian Operating system so could you please help to install and use in Debian?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.