NGINX Deployment Made Easy with Docker Containers

NGINX Deployment Made Easy with Docker Containers

Learn the fundamentals of Docker and how to launch NGINX within a containerized environment.

NGINX is a popular web server and reverse proxy that is known for its speed, efficiency, and reliability. Deploying NGINX can sometimes be a complex process, especially when it comes to configuring and managing multiple instances of the server. However, with Docker containers, deploying NGINX has become much easier.

In this article, we'll go over the basics of using Docker to deploy NGINX, including how to set up and run a containerized NGINX server.

Prerequisites:

  • Docker Desktop installed

  • Access to CLI

Install Docker Desktop

If you haven't installed Docker Desktop yet, please click on the link that corresponds to your local machine and then follow the instructions provided for installation.

Install Docker Desktop on Linux

Install Docker Desktop on Windows

Install Docker Desktop on Mac

Docker Basics

Docker is a powerful platform that allows you to create and run software applications in a containerized environment. In Docker, there are two main concepts that you should be familiar with: images and containers.

An image is essentially a blueprint or template that contains all the necessary instructions to create a container. It's a read-only, immutable file that includes the application code, libraries, and other dependencies required to run the application. You can think of it as a snapshot of a specific configuration of an application.

On the other hand, a container is a running instance of an image. It's a lightweight, standalone executable package that includes everything needed to run the application, including the application code, libraries, and system tools. Containers are isolated from the host system and from other containers, which means that they can be easily moved between environments without any dependencies or compatibility issues.

To interact with Docker, you can use various CLI commands.
For example, the "docker version" command will display the version of Docker installed on your machine, while "docker info" will provide more detailed information about the Docker engine configuration. Additionally, you can use the "docker" command to list available commands and perform various tasks related to images and containers.

Launch a Docker container that executes an NGINX web server

When you run a Docker container using the run command, the Docker engine searches for the specified image in the local image cache. If it can't find the image, it checks Docker Hub, which is a cloud-based registry that contains official Docker images. Once it finds the required image, it downloads the latest version of the image to your local machine (unless you specify a different version in the command). Then, it creates a new container based on that image and starts it. The container listens on a specific port and forwards incoming traffic to another specified port.

To use NGINX with Docker use this command in your command-line interface:

docker container run --publish 80:80 nginx

This command instructs Docker to run a container using the nginx image, which listens on port 80 for incoming traffic and forwards it to port 80 in the container. When the command completes, open a web browser and enter "localhost" in the address bar. You should see the NGINX landing page.

So here we wanted to use NGINX in a container, so we asked Docker to run it. Docker looked for NGINX on the internet and found an image of it on Docker Hub. It downloaded that image to your computer and created a new container using it. This container is running in the foreground.

To stop a Docker container running NGINX in the foreground, you can use the CTRL + C command in your command-line interface.

To run an NGINX web server in the background using Docker, use the below command

docker container run --publish 80:80 --detach nginx

This will start the container and return a unique container ID.

Launch a Docker container that starts an NGINX web server using a Dockerfile

Step 1: Create the Dockerfile

Create a Dockerfile that will define the configuration of the NGINX container. The Dockerfile is a text file that contains a set of instructions for building a Docker image.

Create a new file named "Dockerfile" in a new directory, and add the following contents:

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

This Dockerfile starts with the NGINX Docker image as the base image and copies the nginx.conf file from the local directory to the /etc/nginx/ directory in the container.

Step 2: Create the NGINX Configuration File

The NGINX configuration file is where you define how NGINX should handle incoming requests. Create a new file named "nginx.conf" in the same directory as the Dockerfile, and add the following contents:

events {
  worker_connections 1024;
}

http {
  server {
    listen 80;
    server_name localhost;
    location / {
      root /usr/share/nginx/html;
      index index.html;
    }
  }
}

This configuration file sets up a simple NGINX server that listens on port 80, and serves files from the /usr/share/nginx/html directory.

Step 3: Build the Docker Image

With the Dockerfile and the NGINX configuration file in place, the next step is to build the Docker image. Open a terminal window and navigate to the directory where the Dockerfile and nginx.conf files are located.

Run the following command to build the Docker image:

docker build -t my-nginx-image .

This command will build the Docker image using the Dockerfile and the nginx.conf file in the current directory. The -t flag specifies the name of the image (my-nginx-image in this example), and the . at the end specifies the build context.

Step 4: Run the NGINX Docker Container

The final step is to run the NGINX Docker container. Open a terminal window and run the following command:

docker run -p 80:80 my-nginx-image

This command will start a new container from the my-nginx-image image, and map port 80 of the container to port 80 of the host system.

You should now be able to access the NGINX server by opening a web browser and navigating to "localhost

Basic Docker Commands

To view a list of all currently running containers, you can use the command docker container ls. When you run this command, you'll see a list of containers along with their randomly generated names under the "NAMES" column. It's important to note that these names were generated because we didn't provide a specific name when we created the container.

List all containers: docker container ls -a

Stop a container: docker container stop <container id>

Create a container with a specific name: To assign a custom name to your container during creation, you can include the parameter --name in the command.

docker container run --publish 80:80 --detach --name <name_of_container>

By running the command "docker container ls -a", you can view the list of containers that you have created.

Container Logs: To view container logs, use the following command:

docker container logs <name_of_container>

View processes running inside a container: To view the processes that are currently running inside a particular container, you can use the below command.

docker container top <name_of_container>

Removing containers: Before removing any container, it's crucial to ensure that they are stopped since a running container can't be deleted. To stop the containers, execute the below command, and input the initial few unique characters of the containers you intend to stop. Once they are stopped, you can remove them safely.

docker container stop

One way to remove specific Docker containers is by using the below command and providing the first few unique characters of their IDs as identifiers.

docker container rm

Summary

In conclusion, deploying NGINX with Docker containers is a straightforward process that can simplify and streamline the deployment of web servers. By following the steps outlined in this article, you can create and run a containerized NGINX server quickly and easily.

With the flexibility and portability of Docker, you can deploy NGINX containers across multiple environments with ease, making it a valuable tool for web developers and system administrators.

Did you find this article valuable?

Support Dhrubajyoti Chakraborty by becoming a sponsor. Any amount is appreciated!