Open In App

Docker – Container for NGINX

Last Updated : 28 Nov, 2023
Like Article

Docker is an open-source platform that enables developers to easily develop, ship, and run applications. It packages an application along with its dependencies in an isolated virtual container which usually runs on a Linux system and is quite light compared to a virtual machine. The reason is that a container shares the OS kernel with only its binaries/libraries loaded with it. To know more about docker, read Introduction to docker.

What is NGINX and What Are its Benefits?

Nginx is mostly used as reverse proxy software. Nginx is an open-source web server you can perform multiple tasks with the nginx like scalability, efficiency, and flexibility it is specially designed for high performance which can be handled in scenarios where there is a high amount of incoming requests.

Benefits of Nginx

  • High Performance: Nignix is an event-driven architecture that can handle high loads at a time wit more efficiently. Nginx will consume very little memory which will help in improving overall efficiency.
  • Scalability: Nginx can be scaled up and down depending on the incoming traffic. To manage growing workloads, it can be installed on single servers or clusters of servers.
  • Flexibility: Nginx is very flexible to use apart from the web serving it can also be used for other things like reverse proxy, load balancer, caching server, and media streaming server.
  • Low Resource Consumption: Nginx consumes very less amount of resources like CPU and Memory when compared to the other web servers.
  • Open Source and Community Support: Nginx is an open-source platform so its users are very huge so the documentation, tutorials, and support are available for new users and students if they want to learn.

What are Docker Containers and Why Use Them For NGINX?

Containerization is OS-based virtualization that creates multiple virtual units in the userspace, known as Containers. Containers share the same host kernel but are isolated from each other through private namespaces and resource control mechanisms at the OS level. Container-based Virtualization provides a different level of abstraction in terms of virtualization and isolation when compared with hypervisors. Hypervisors use a lot of hardware which results in overhead in terms of virtualizing hardware and virtual device drivers. A full operating system (e.g -Linux, Windows) runs on top of this virtualized hardware in each virtual machine instance. Dockers provide a consistent and isolated environment for running the application, making them a popular choice for deploying the Nginx.

Benefits of Using Docker Containers for NGINX

  • You can the nginx in the different types of underlying OS platforms which makes it light in weight and portable for different environments even though you change the nginx container from one platform the behavior will also remain the same in each and every platform.
  • Docker container completely isolates nginx container this conflicts will be reduces because of the isolation it will reduces the risk of security it will stop the affecting the other applications.
  • Containers are light in weight there will share the host system kernel which make them more efficient when compared to others like virtual machine and son on.
  • Docker is widely adopted in the DevOps methodology which make it easier to integrate the nginx deployment into continuous integration and continuous delivery (CI/CD) pipelines.

What Is Docker Nginx?

When a user requests a page from a web server, the web server takes the request and sends an appropriate response back to the user. Nginx can be that web server. NGINX is an open-source web server that is also used for reverse proxy, HTTP load balancing, and email proxy. It is very efficient in using the system’s resources and can handle a huge number of simultaneous requests using event-driven and asynchronous architecture. That is the reason why Nginx is an excellent choice for websites that deal with high loads like e-commerce, cloud storage, and search engines. To know more about Nginx refer to What is Nginx (Web Server) and how to install it?

How To Pull Docker Nginx Image?

Follow the steps mentioned below to pull the Nginx image by using Docker.

Step 1: Open the Docker server where it was installed.

Step 2: You can pull the docker nginx image type depending on the requirement of your organization like you use the community version or enterprise version depending on the requirement of the organization.

Step 3: By using the command mentioned below you can pull the nginx image by using Docker which is located in the DockerHub.

docker pull nginx:<Version>

Docker pull is a command that is used to pull any image you want to use and Nginx is the image that you can run as a container <version> you have to mention the version of the image based on your requirement. If you didn’t mention any version then docker will automatically pull the latest version of the image.You can write your own Dockerfile to create a customized Docker Nginx image

Why Docker Containers?

One common problem that most developers face is when an application runs on one machine but doesn’t on another. This can be due to different OS and different versions of libraries like a developer developed an application using nodejs 14.1 but the cloud instance has nodejs 9.2 installed.This is the exact problem that a docker container solves, it packages the app’s libraries and all the dependencies, prebuilt and ready to be executed. It is isolated from other containers and makes the application feel that it is the only application running on the system.Many organizations are now moving to containers from virtual machines as containers are lightweight and easy to maintain using the provided CLI.It also helps in an easy adaptation of microservices architecture moving away from traditional monolithic systems. Other benefits include scalability, modification, and maintenance.

Docker Images And Docker Hub

A docker image is just like a snapshot in VM environments. It records information about the docker container at a specific time like all the libraries along with their particular versions needed for an application. It is immutable but can be easily duplicated and shared with others.An image is usually shared with others in order to enable someone else to run the application in the same environment it is supposed to run and the image holds all the information about that environment.A Docker hub is one such platform where you can find and share container images with others. Some of the most common images are for Nginx, Nodejs, Mongo DB, and many more.

How To Run Docker Nginx Image In Detached Mode?

You can run Docker nginx image in by using following commad. But when you press CTRL +C then containers stops running because you are running the Docker container in the form of Attached mode when exit the container will stop running automatically.

docker run nginx

To over come the above stitutation you need to run the containers in the form of detached mode. You can do that by using following command.

docker run -d nginx

-d indiactes that you are running the image in the form of detachd mode. To Know more about Docker Commands you can refer to Docker – Instruction Commands.

How To Push The Docker Nginx Image To DockerHub?

After pulling the image form the DockerHub you can make some changes to the image according to your byssienss operations

Downloading The Nginix From Docker Hub

Download the official image for NGINX from the docker hub using the following command.

docker pull nginx:latest

This command will fetch the latest version of the nginx image but you can replace “latest” with the version you need and that will be downloaded.

How To List The Docker Images?

Confirm that the image has been downloaded using the following command.

docker images

The result should include nginx along with your other images like this.

Docker Images LIst

How to create and run an NGINX container with Docker?

Now run the docker image using the following command.

docker run -p 8000:80 nginx

Using the above command you are running the nginx server where -p is the flag for mapping the port 8000 (local machine) to the port 80 in the container as 80 is the port on which nginx serves by default.Verify the installation by visiting the localhost at the port 8000. We will get the default page for nginx web server as follows:


Building a Web Page to Serve on Nginx

We will create two files one is index.html and aboutme.html and host these files on a new container running Nginx server.

The file structure after creating the two files will be as follows:


The index.html and aboutme.html contain the following code:

Sample Index.html For Web page for Docker Nginx


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NGINX Example</title>
    <h3>This is NGINX</h3>
        <a href="./aboutme.html">About ME</a>

Explanation: This webpage contains two headers and a footer that has a link to about me page.



<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>About me</title>
    <h3>This is ganesh prasad!</h3>

Explanation: This page shows an h1 header with “Hello” as text and an h3 header with “This is Ganesh Prasad” as its body.

3. Use the following command to create a docker container that maps the local machine’s port to the port on which nginx serves and mount these two files to a location in the container that nginx uses to host files (/usr/share/nginx/html).

docker run -d -p 8000:80 -v address_to_folder_with_files_locally:/usr/share/nginx/html --name my-nginx-server nginx


  • The flag -d is used to run the server in detached mode means in the background.
  • -p is used to map the local port to the container’s port where the server is serving.
  • -v is used to mount the folder that contains the files to a location in the container that Nginx uses to serve the web pages.
  • –name is to provide the name of the container and finally, we provide the image name.


Visit the address localhost:8000 in your browser and you will see the following:

Nginix with Html

The home page 

And on clicking the About ME link takes you to the about me page.

Nginx Server

How To Stop To Docker Nginx Container?

To stop the docker container you can use the following command.After completation

docker stop my-nginx-server

Stop is the command used to some the containers which are running and “my-nginx-server” is the name of the container.

Customizing NGINX configuration in Docker containers

Follow the steps mentioned below to crate an coustm nginx images.

Step 1: Create a Custom NGINX Configuration File

Create an coustm nginx file like example GFG.conf. as following.

# custom.conf

server {
listen 80;

location / {
proxy_pass http://your_backend_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# Add other proxy settings as needed

# Add other server block configurations as needed

Step 2: Create A Dockerfile for Nginx as follows.

# Use the official NGINX base image
FROM nginx:latest

# Copy the custom NGINX configuration file to the container
COPY <Path>

Step 3: Build the docker image using the dockerfile with the following command.

docker build -t <Nmae of the file> .

Step 4: Run the Docker image as an Container.

docker run -p 8080:80 <Name of the Image>

Advanced Docker concepts for NGINX management

In docker advanced concepts you can use docker more efficiently can deploy the application with more feature and security. Following was the concepts of nginx deployment in docker containers.

  • Volume Mounts for Persistent Data.
  • Networking Configuration for Multi-Container Applications.
  • Health Checks for Container Monitoring.
  • Image Building for Customized Deployments.
  • Resource Management for Scalability and Performance.
  • Orchestration Tools for Large-Scale Deployments.
  • Continuous Integration and Continuous Delivery (CI/CD).


In this article, we learned in brief about docker, container, images, and docker hub. Then we created a docker container using the official Nginx image from the docker hub and ran it. We also learned how we can list all the images on our system and how to stop a docker container. After that, we hosted a small webpage on the Nginx server running on a docker container.

FAQ’s On Docker Nginx

1. Can I Run Docker Nginx As A Container?

Yes you can run the Nginx image as an Docker Container with the coustmized image.

2. Is Nginx Used In Kubernetes?

For Kubernetes clusters, NGINX is the most often used ingress controller.

3. Docker Nginx Reverse Proxy

In this docker nginx will runs as an docker container and acts as a reverse proxy for other backend applications.Followinga re the some of the benefits of docker nginx container..

  • Isolation and Encapsulation
  • Scalability and Flexibility
  • Portability and Reusability

4. Docker Nginx Hello World

A Docker Nginx Hello World is a simple example of how to use Docker to run an Nginx web server that displays a “Hello World” message.From the above example you achieve this easily.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads