Skip to content
Related Articles
Open in App
Not now

Related Articles

Docker – Container for NGINX

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 11 Aug, 2021
Improve Article
Save 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 as compared to a virtual machine. The reason being that a container shares the OS kernel with only its binaries/libraries loaded with it. To know more about docker, read Introduction to docker.

In this article, we will create and run a docker container for NGINX. After that, we will host a very simple and small website on the Nginx container. 

Let’s start with a few useful concepts and definitions.

Why Docker Containers?

To perfectly answer this question we should ask what problems do they solve. 

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, 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.

A Docker container for NGINX

What is  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 load like e-commerce, cloud storage and search engines.

Create a docker container for NGINX

1. 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.

2. 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.

3. Now run the docker image using the following command.

docker run -p 8000:80 nginx

Explanation: 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.

4. Verify the installation by visiting the localhost at the port 8000. We will get the default page for nginx web server as follows:

5. To stop the docker container press CTRL + C.

Host a simple website on NGINX running on Docker container

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

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

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



<!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 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 webpages.
  • –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:

The home page 

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

The about me page

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

docker stop my-nginx-server


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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!