Proxy Pass from Nginx to Tomcat using docker container:

Suryansh Mathema
5 min readJul 1, 2021

--

System Requirements:

VMWare — If you wish to work on a virtualized environment.

CentOS Machine — You can use any Linux distribution like Ubuntu, Kali Linux, etc. [I recommend you use the latest version. Also, try to work on other Linux distributions like Fedora, Arch Linux and Kubuntu.]

Docker — You can use the latest version of Docker.

Docker-Compose

Some general terms before we get started:

What is Docker?

  • Whenever you hear the term Docker, keep in mind that it is a platform as a service product that uses OS-level virtualization. It is basically a tool designed to make it easier to create, deploy, and run applications by using Containers. Containers allow a developer to package up an application with all the parts it needs, such as libraries and other dependencies, and deploy it as one package (Opensource.com, 2021).
Docker

To install the latest version of Docker on CentOS: [Its better you update and upgrade your system at first]

$ sudo yum update$ sudo yum upgrade$ sudo yum install yum-utils device-mapper-persistent-data lvm2$ sudo yum-config-manager — add-repo https://download.docker.com/linux/centos/docker-ce.repo$ sudo yum install docker-ce docker-ce-cli containerd.io$ sudo systemctl start docker

What is Docker-Compose?

  • So, Docker-Compose is used for running multi-container applications. With Docker-Compose, you can create a YAML file (YAML is a data serialization language designed for human interaction click here for more information) to define the services and with a single command, can spin everything up or tear it all down (Docker, 2021).
Docker-Compose

To install Docker-Compose on CentOS: [I used Github repository to install Docker-Compose]

$ sudo yum install curl$ sudo curl -L “https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)” -o /usr/local/bin/docker-compose$ sudo chmod +x /usr/local/bin/docker-compose$ docker–compose –-version

Practical demo on the topic:

Contents on docker folder:

  • Here, I have created a folder named ‘docker’. In which three files are created; a docker file named ‘Dockerfile’ which is a text file, a docker-compose file named ‘docker-compose.yml’ which is a YAML file (be very careful with the syntax while writing a YAML file) and a nginx configuration file named ‘nginx.conf’.
Figure 1: Docker folder with all the necessary files.

Contents on Dockerfile:

  • Here, the docker file will download the nginx alpine image and will replace the default nginx configuration file with the modified configuration file.

From nginx:alpine

COPY nginx.conf /etc/nginx/nginx.conf

Figure 2: ‘Dockerfile’ file content.

Contents on nginx.conf:

  • Here, on this nginx configuration file which contains worker processes, events and HTTP block and I have one server block defined which is listening on port 80. I have a proxy pass configuration which is doing proxy pass to Tomcat’s 8080 port. Here, I have not specified any IP address. I have specified the name of the container which I have named as tomcat.

worker_processes 1;

events { worker_connections 1024; }

http {

sendfile on;

server {

listen 80;

location / {

proxy_pass http://tomcat:8080

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Host $server_name;

}

}

}

Figure 3: ‘nginx.conf’ file content.

Note: If you are logged in as root user, you need not to use the sudo command and vice-versa.

Building the docker image:

Command: docker build -t nginxproxy .

  • Here, I have built the image naming the image as ‘nginxproxy’. Do not forget to give the dot (.) at last as it builds the image on the current location/directory.
$ docker build -t nginxproxy .

Listing the docker images:

Command: docker images

$ docker images
Figure 4: Using the ‘docker images’ command to list all the docker images.

Contents on docker-compose.yml file:

  • Here, I have two services defined, one for nginx and other for tomcat. Nginx is using the ‘nginxproxy’ image which was built using docker and it is having port configuration containing host Port 80 to container’s Port 80. The restart is set to always. The tomcat is depending upon the nginx container. The downloaded image for tomcat will be ‘tomcat 8’. The restart for tomcat is also set to always. Note that, whatever name is given for tomcat, the same name should be configured inside the nginx configuration file.

version: ‘2’

services:

nginx:

image: nginxproxy

ports:

- 80:80

restart: always

tomcat:

depends_on:

- nginx

image: tomcat:8

restart: always

Figure 5: ‘docker-compose.yml’ file content.

Creating multi-container using docker-compose command:

  • Here, the following command is creating both the containers.

Command: docker-compose up -d

$ docker-compose up -d
Figure 6: Using the ‘docker-compose up -d’ command to run multi-container according to the docker-compose file I have created.

Listing the docker containers:

  • Here, two containers are listed, tomcat and nginx.

COMMAND: docker container ls

$ docker container ls
Figure 7: Listing the docker container(s).

Apache Tomcat Running Successfully:

  • Here, proxy pass has been done and redirected to tomcat.
Figure 8: Apache Tomcat running successfully on Localhost:8080.

Thank You!! 😊

--

--

Suryansh Mathema
Suryansh Mathema

Written by Suryansh Mathema

I believe in rectitude and moral uprightness in character. I strongly want to establish myself in the field of DevOps and System Security.

No responses yet