How to Set Up Docker Reverse Proxy Server?

How to Set Up Docker Reverse Proxy Server: A Step-by-Step Guide by OpsNexa

Setting up a reverse proxy server in a Dockerized environment is a common practice, especially when you want to manage multiple services behind a single domain or IP address. Using a reverse proxy allows you to route client requests to different backend services based on the URL or request type. This is especially useful when deploying multiple containers running different applications.

In this guide, OpsNexa will walk you through setting up a Docker reverse proxy server using Nginx, one of the most popular reverse proxy tools, combined with Docker Compose for orchestration. By the end of this article, you’ll be able to configure and deploy your reverse proxy setup with ease.

What is a Docker Reverse Proxy Server?

A reverse proxy server is an intermediary server that sits between client requests and backend servers. It accepts client requests and forwards them to appropriate backend services. Unlike a traditional proxy, which forwards client requests to the internet, a reverse proxy server forwards requests to internal resources.

When you use Docker to deploy multiple services, each running in its container, you need a way to expose those services securely and efficiently. A reverse proxy allows you to:

  • Route traffic to the correct container based on the domain name or URL.

  • Manage SSL certificates for secure HTTPS traffic.

  • Load balance traffic across multiple containers.

  • Provide a unified entry point for all services.

In a Docker environment, the reverse proxy server will route requests to different containers that are running different applications, all while keeping the user experience seamless and efficient.

Step 1: Install Docker and Docker Compose

Before setting up the reverse proxy server, you need to ensure that Docker and Docker Compose are installed on your system. Docker Compose is a tool that allows you to define and run multi-container Docker applications.

Installing Docker:

  1. Visit the official Docker installation page to download Docker for your platform (Linux, macOS, Windows).

  2. Follow the installation instructions specific to your OS.

  3. Verify the installation by running the following command:

bash
docker --version

Installing Docker Compose:

  1. Visit the official Docker Compose installation page and follow the installation instructions for your platform.

  2. After installation, verify it by running:

bash
docker-compose --version

Once both Docker and Docker Compose are installed, you can move forward with setting up the reverse proxy.

Step 2: Set Up a Simple Docker Application

To demonstrate the reverse proxy functionality, we’ll need a basic Docker application running in one or more containers. Let’s use two simple applications running in Docker containers:

  1. A basic web server (e.g., Nginx or Apache) serving content on one container.

  2. Another container running a different web application or service.

For this, let’s create two simple Docker containers, one for an Nginx web server and another for a basic application (for example, a Python Flask app).

Dockerfile for the Flask Application:

Create a directory for the Flask app and add the following Dockerfile:

Dockerfile

FROM python:3.8-slim

WORKDIR /app

COPY . /app

RUN pip install -r requirements.txt

CMD [“python”, “app.py”]

Create a requirements.txt file with the following content:

ini
Flask==2.0.1

Create a basic app.py file:

python
from flask import Flask
app = Flask(__name__)

@app.route(‘/’)
def hello_world():
return ‘Hello from Flask!’

if __name__ == “__main__”:
app.run(host=‘0.0.0.0’, port=5000)

Docker Compose for Both Services:

Now, let’s define the services using Docker Compose. Create a docker-compose.yml file to manage the web and Flask application containers:

yaml

version: '3'

services:
web:
image: nginx:latest
container_name: web
ports:
“8080:80”
volumes:
./nginx.conf:/etc/nginx/nginx.conf
networks:
proxy

flask:
build: ./flask-app
container_name: flask
networks:
proxy

networks:
proxy:
driver: bridge

In this setup:

  • The Nginx container listens on port 8080.

  • The Flask application runs on a different internal network and is not directly exposed.

Make sure to create the nginx.conf file to define how Nginx handles the reverse proxy configuration (this will be explained in the next section).

Step 3: Create Nginx Reverse Proxy Configuration

The key to a Docker reverse proxy is the Nginx configuration. You need to configure Nginx to route traffic to different services based on the request URL.

Nginx Configuration (nginx.conf):

Create an nginx.conf file with the following content:

nginx
server {
listen 80;

server_name localhost;

location / {
proxy_pass http://flask:5000;
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-Proto $scheme;
}
}

In this configuration:

  • The proxy_pass directive forwards requests to the Flask container.

  • The headers ensure that the client’s original IP and other information are forwarded properly.

  • This setup enables Nginx to act as a reverse proxy and forward requests to the Flask app running in the Docker container.

Step 4: Run the Docker Containers

With everything configured, it’s time to run the containers using Docker Compose.

Execute the following command from the directory containing your docker-compose.yml file:

bash
docker-compose up --build

This command will:

  • Build the Flask application image.

  • Start both the Nginx and Flask containers.

  • Link the containers within the same Docker network (proxy), allowing Nginx to communicate with the Flask app.

Once the containers are up and running, you can navigate to http://localhost:8080 in your browser. You should see the response from the Flask application, routed through Nginx.

Step 5: Advanced Configuration (Optional)

In real-world use cases, you may want to add more advanced features to your reverse proxy setup. Here are a few common configurations:

1. SSL Encryption with Let’s Encrypt

To ensure secure communication, you can configure SSL with Let’s Encrypt and automatically generate SSL certificates. This would involve additional setup for Nginx to handle HTTPS traffic.

2. Load Balancing

If you have multiple containers running the same service (e.g., multiple instances of the Flask app), you can configure Nginx for load balancing to distribute the traffic evenly among all containers.

3. Multiple Services with Path-based Routing

You may also want to route traffic to different services based on the URL path (e.g., /app1 routes to one service, /app2 routes to another). This can be easily achieved by modifying the Nginx configuration file to add multiple location blocks.

Step 6: Troubleshooting and Logs

To troubleshoot any issues with the reverse proxy setup, you can check the logs of the Docker containers:

  • View the Nginx logs with:

bash
docker logs web
  • View the Flask application logs with:

bash
docker logs flask

These logs will help you identify any issues, such as misconfigured routes or connectivity problems between containers.

Conclusion

Setting up a Docker reverse proxy server with Nginx is a powerful way to manage multiple applications running in containers. It simplifies routing, improves security, and enhances scalability. In this guide, OpsNexa showed you how to set up a reverse proxy with Nginx, configure Docker Compose, and troubleshoot potential issues.

By using this approach, you can efficiently manage multiple services, provide SSL encryption, and even scale your applications horizontally for better performance. Happy Dockerizing!