Exposing Docker applications with Nginx
Feb 21, 2024
Written by: Cristina Rancaño
In this post we want to share some basic examples of how to use Nginx to expose applications running in Docker.
To start, we give a quick overview of the services we are going to see:
Nginx: is an open-source web server that is also used as a reverse proxy, HTTP cache, and load balancer.
Docker: is an open-source tool that allows developers to create, deploy, run, and manage containers.
Required Software
A Linux server (In the example, we will use one based on Debian)
Nginx installed:
apt update && apt install nginx
Docker installed: docu.
Nginx with Docker
This is the infrastructure we will set up:
2 applications running in docker:
App1 - which exposes port 1234
App2 - which exposes port 1235
An Nginx server where we will have the following domains:
app1.test.com
app2.test.com
To understand this, first we need to see the following:
Thanks to Nginx Vhosts we are redirecting traffic and requests to our containers.
Each Vhost has its own configuration, usually found in:
/etc/nginx/sites-available
→ are the available vhosts on our server./etc/nginx/sites-enabled
→ are the active vhosts, usually symbolic links to the previous directory.
In this example, we will see a basic configuration for each of the Vhosts. For example, we will configure the listener, the hostname, and the corresponding proxy. Example:
Vhost for App1
Vhost for App2
Possible Errors
Port conflict: It is important to note that each container must have a different port than the host.
The ports of the applications must be different from the Nginx web server port.
Nginx with Docker and system services
In addition to configuring Vhosts that lead to containers, we can also direct traffic to services running on the system itself.
Vhost for App3
Summary
In this post, we have different basic examples of how with Nginx we can direct traffic to containers and also to services running within our server. We hope you found it useful.
Thank you for reading!