Setting up an NGINX application with Docker

Filed Under: Random
NGINX Application With Docker

Hello, readers! In this article, we will be focusing on Setting up an NGINX application using Docker, in detail.

So, let us get started!:)

What is Nginx?

Nginx is a well-known, lightweight web application that is mostly used to develop and host server-side applications. As Nginx is lightweight and compatible with most of the platforms, Docker has strong support on configuration levels for the same.

In the course of this topic, we will be covering the Set up plan for Nginx with Docker using two techniques–

  1. Using Nginx image from public repository
  2. Setting up Nginx using Dockerfile

Recommended read: Top 10 Docker Commands

Method 1: Using Nginx image from Public repository

In this method, we make use of the already available NGINX image from the Docker Public Repository i.e. DockerHub.

You can visit DockerHub and type NGINX to view various public nginx images available for configuration.

Image 1
Docker_hub – NGINX

Make use of docker pull command to pull the nginx image from docker hub to you locally system

docker pull nginx

Having done that, we make use of docker run command to run the above image locally as a container in detatched mode using -d. Also, we have it on port 8080 (HTTP) to host the nginx application on localhost

docker run –p 8080:80 –d nginx

Review the presence of nginx locally by typing localhost on the local browser.

Image 3

Method 2: Setting up Nginx using Dockerfile

In this technique, we set up the nginx application using Dockerfile.

  1. At first, we create directory to have all the nginx related files in place using the mkdir command.
mkdir nginx-folder
 cd nginx-folder/

2. Using vi editor, we create a index.html file and paste the below content in the file as shown–

vi index.html
<!DOCTYPE html>
    body {
                    width: 35en: 0 auto;
                            fontana, Arial, sans-serif;
    <h1>Welcome to tom nginx!</h1>
    <p>If you he nginx web server is successfully installed and
            working. Further confn is required.</p>

    <p>For online documentation and support pleaseo
            <a href=""></a>.<br/>
    Commercial support is available at
            <a href=com/"></a>.</p>

    <p><em>Thank you for using nginx.</em></p>

3. In the same directory, we also create a Dockerfile and paste the below content into it–

vi Dockerfile
FROM nginx:latest
LABEL version="1.0"
ADD index.html /usr/share/nginx/html/
RUN chmod +r /usr/share/nginx/html/index.html
CMD ["nginx","-g", "daemon off;"]

4. Further, we build the Dockerfile using docker build command and tag it as 1.0 for our convenience.

docker build -t nginx_image:1.0 .


[+] Building 111.9s (8/8) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                   0.2s
 => => transferring dockerfile: 202B                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                      0.2s
 => => transferring context: 2B                                                                                                                                        0.0s
 => [internal] load metadata for                                                                                                        6.4s
 => [internal] load build context                                                                                                                                      0.1s
 => => transferring context: 639B                                                                                                                                      0.0s
 => [1/3] FROM                                                102.2s
 => => resolve                                                  0.0s
 => => sha256:f0b8a9a541369db503ff3b9d4fa6de561b300f7363920c2bff4577c6c24c5cf6 7.74kB / 7.74kB                                                                         0.0s
 => => sha256:69692152171afee1fd341febc390747cfca2ff302f2881d8b394e786af605696 27.15MB / 27.15MB                                                                      94.6s
 => => sha256:5f97dc5d71ab2675126dba76dbe161c839043fb3e3ccaaf58ba78b394cdd37b0 602B / 602B                                                                             0.6s
 => => sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2 1.86kB / 1.86kB                                                                         0.0s
 => => sha256:eba373a0620f68ffdc3f217041ad25ef084475b8feb35b992574cd83698e9e3c 1.57kB / 1.57kB                                                                         0.0s
 => => sha256:49f7d34d62c18a321b727d5c05120130f72d1e6b8cd0f1cec9a4cca3eee0815c 26.58MB / 26.58MB                                                                      94.6s
 => => sha256:cfcd0711b93a41eb07234fa93b0c2ce39be93a71a743dd76766e67a0133163e8 894B / 894B                                                                             2.9s
 => => sha256:be6172d7651be2625df008a2bb6cbaf2139cbf1e50ae16256791e54e8dde8449 666B / 666B                                                                             3.8s
 => => sha256:de981387034241b38875f3151654b3bd9ef4afe396eec23dce7cb73134d2867b 1.39kB / 1.39kB                                                                         4.9s
 => => extracting sha256:69692152171afee1fd341febc390747cfca2ff302f2881d8b394e786af605696                                                                              3.3s
 => => extracting sha256:49f7d34d62c18a321b727d5c05120130f72d1e6b8cd0f1cec9a4cca3eee0815c                                                                              1.7s
 => => extracting sha256:5f97dc5d71ab2675126dba76dbe161c839043fb3e3ccaaf58ba78b394cdd37b0                                                                              0.0s
 => => extracting sha256:cfcd0711b93a41eb07234fa93b0c2ce39be93a71a743dd76766e67a0133163e8                                                                              0.0s
 => => extracting sha256:be6172d7651be2625df008a2bb6cbaf2139cbf1e50ae16256791e54e8dde8449                                                                              0.0s
 => => extracting sha256:de981387034241b38875f3151654b3bd9ef4afe396eec23dce7cb73134d2867b                                                                              0.0s
 => [2/3] ADD index.html /usr/share/nginx/html/                                                                                                                        1.4s
 => [3/3] RUN chmod +r /usr/share/nginx/html/index.html                                                                                                                1.2s
 => exporting to image                                                                                                                                                 0.2s
 => => exporting layers                                                                                                                                                0.1s
 => => writing image sha256:a738dbef66efd653c6ac336779a47bd88f6c81fe2c27ec90ae48f5a8aa51e363                                                                           0.0s
 => => naming to

5. Once done, we check for the presence of the above nginx built image locally using docker images command.

docker images
REPOSITORY                                                      TAG                 IMAGE ID            CREATED             SIZE
nginx_image                                                     1.0                 a738dbef66ef        15 seconds ago      133MB

6. Finally, we run the docker image locally as a container using docker run command and have it in detached mode using -d. We make it to server at port 80 using -p 80:80 and provide the above built image name to it.

docker run --name mywebsite -d -p 80:80 nginx_image:1.0

7. Then, docker ps command helps us check for the running container processes across the system.

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
443848c30b74        nginx_image:1.0     "/docker-entrypoint.…"   7 seconds ago       Up 6 seconds>80/tcp   mywebsite

In order to stop the running NGINX process, we make use of docker stop command as shown below–

docker stop mywebsite

As seen above, the nginx application is currently running on port 80 as localhost on our workstation. We can confirm that the NGINX piece is up by typing localhost onto the local borwser.


By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to Docker, Stay tuned with it.

Do let us know about you understanding in Setting up a NGINX application locally in the below comment box.

Till then, Happy Learning!! 🙂

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors