Home > Software > How to Deploy NGINX with Docker Compose: A Step-by-Step GuideHow to Deploy

How to Deploy NGINX with Docker Compose: A Step-by-Step GuideHow to Deploy

Anastasios Antoniadis

Share on X (Twitter) Share on Facebook Share on Pinterest Share on LinkedInNGINX, known for its high performance, stability, and rich feature set, is one of the most popular web servers and reverse proxies in the IT world. Integrating NGINX with Docker Compose not only simplifies the deployment process but also enhances the scalability and …

Docker (1)

NGINX, known for its high performance, stability, and rich feature set, is one of the most popular web servers and reverse proxies in the IT world. Integrating NGINX with Docker Compose not only simplifies the deployment process but also enhances the scalability and manageability of web applications. Docker Compose allows you to define and run multi-container Docker applications using a simple YAML file, making it an ideal tool for deploying NGINX alongside other services. This guide will walk you through the process of deploying an NGINX server using Docker Compose, demonstrating how to configure NGINX as both a standalone web server and a reverse proxy for a web application.

Understanding Docker Compose and NGINX

Docker Compose is a tool for defining and managing multi-container Docker applications. With a single command, you can create and start all the services defined in a docker-compose.yml configuration file. NGINX, on the other hand, excels at serving static content, load balancing, caching, and proxying to other web servers or applications, making it a versatile choice for both development and production environments.

Prerequisites

Before proceeding, ensure you have Docker and Docker Compose installed on your system. Basic knowledge of Docker concepts and familiarity with YAML syntax will also be helpful.

Step 1: Create a Docker Compose File for NGINX

Create a Project Directory: Start by creating a directory for your NGINX deployment.

mkdir nginx-docker-compose && cd nginx-docker-compose

Create the Docker Compose File: Inside the directory, create a docker-compose.yml file.

touch docker-compose.yml

Define Your NGINX Service: Open the docker-compose.yml file in a text editor and add the following configuration:

version: '3.8'
services:
  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./html:/usr/share/nginx/html
    restart: always

In this example:

  • The service is based on the official NGINX image from Docker Hub.
  • Port 80 on the host is mapped to port 80 in the container, making the NGINX server accessible from the host.
  • The volumes directive mounts a custom NGINX configuration file and a directory with HTML files to serve.

Create a Custom NGINX Configuration (Optional): If you want to customize the NGINX configuration, create an nginx.conf file in the same directory as your docker-compose.yml file. For example:

events {}
http {
    server {
        listen 80;
        server_name localhost;

        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }
}

This simple configuration sets up NGINX to serve static files from the /usr/share/nginx/html directory inside the container.

Add Static Content (Optional): Create an html directory and add an index.html file to serve:

mkdir html
echo "<h1>Hello, Docker Compose and NGINX!</h1>" > html/index.html

Step 2: Deploy Your NGINX Server

With your docker-compose.yml (and optionally nginx.conf) ready, deploy your NGINX server using Docker Compose:

docker-compose up -d

Step 3: Verify the Deployment

To ensure your NGINX server is running correctly, open a web browser and navigate to http://localhost. You should see the content of your index.html file or the default NGINX welcome page if you didn’t provide custom HTML content.

Best Practices

  • Security: Always use the latest version of NGINX and Docker Compose to benefit from security patches. For production deployments, consider using HTTPS by mounting SSL certificates into your container and configuring NGINX to use them.
  • Configuration Management: Keep your NGINX configuration files and static content under version control to track changes and simplify deployments across different environments.
  • Performance Tuning: Customize your nginx.conf based on the specific needs of your application to optimize performance. NGINX offers various directives for caching, compression, and other performance enhancements.

Conclusion

Deploying NGINX with Docker Compose offers a streamlined approach to setting up web servers and reverse proxies. By defining your entire application stack in a docker-compose.yml file, you can achieve consistent, reproducible deployments with minimal effort. Whether you’re serving static content, load balancing between multiple web applications, or setting up a reverse proxy, NGINX and Docker Compose make a powerful combination for modern web infrastructure.

Anastasios Antoniadis
Follow me
Latest posts by Anastasios Antoniadis (see all)
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x