NGINX Docker Containers Service on Linux Server: An Overview
In this article, we will discuss how to deploy NGINX on a Linux server using Docker Compose, focusing on the site with the global topic of containers and microservices. The NGINX web server is a popular choice for containerized applications due to its speed, low memory footprint, and ease of configuration. By the end of this article, you will have a solid understanding of how to create and deploy a Docker Compose project that includes an NGINX service with an exposed port.
Prerequisites
To follow along with this article, you will need the following:
- A Linux server with Docker and Docker Compose installed.
- Basic knowledge of Docker and Docker Compose.
Creating a Docker Compose Project
Docker Compose is a tool for defining and running multi-container Docker applications. With a YAML file, you can configure your application's services, networks, and volumes. This makes it easy to spin up a complete environment with just one command.
To create a new Docker Compose project, you need to make a directory for your project and initialize it with a docker-compose.yml file.
$ mkdir poc-do
$ cd poc-do
$ touch docker-compose.yml
Defining the NGINX Service
Now, you can define your NGINX service in the docker-compose.yml file. The following example shows how to define the NGINX service:
version: '3'
services:
nginx:
image: nginx:latest
container_name: poc-nginx
expose:
- "12345"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
ports:
- "12345:12345"
Here's what each configuration option does:
version: specifies the Docker Compose file format versionservices: defines a list of services for the projectnginx: the name of the NGINX serviceimage: specifies the NGINX image to usecontainer_name: the name of the NGINX containerexpose: exposes the specified port within the containervolumes: mounts thenginx.conffile from your host to the containerports: maps the exposed port from the container to your host
Creating the NGINX Configuration File
The nginx.conf file defines the NGINX server configuration. The following example shows a basic NGINX configuration that serves the content of the public directory:
worker\_processes 1;
events { worker\_connections 1024; }
http {
server {
listen 12345;
server\_name localhost;
root /public;
index index.html;
}
}
Running the Docker Compose Project
Now you can start your Docker Compose project with the following command:
$ docker-compose up -d
Verifying the NGINX Service
You can verify that your NGINX service is running and accessible by visiting http://<server\_ip>:12345 in your web browser.
Stopping the Docker Compose Project
To stop your Docker Compose project, use the following command:
$ docker-compose down
- In this article, you learned how to deploy NGINX on a Linux server using Docker Compose for a site focused on containers and microservices.
- You created a Docker Compose project that defines the NGINX service and its configuration options.
- You learned how to start, verify, and stop your Docker Compose project.