Build custom Nginx Docker image

Normally, using Nginx with Docker, we would not install Nginx from the official image and then configure it manually. We will use the pre-configured Inginx Docker Image and all we need to do is start the container from those Nginx Docker images and use them. In this tutorial, I will show you how to build a custom Nginx Docker Image!

I will build a custom Nginx Docker Image so that when I start the container from this Docker image, I will have a web application like in the tutorial Basic web application configuration with Nginx on CentOS.

I will create a new Dockerfile using the Ignix official image to get started:

The default Nginx site configuration when we install Nginx from Docker is contained in the /etc/nginx/conf.d/default.conf file of that Docker container! The contents of this file are as follows:

I will not use this default configuration so I will remove it in the Dockerfile as follows:

I will replace it with the configuration in the tutorial Basic web application configuration with Nginx on CentOS:

by creating a new nginx.conf file containing the above configuration and then copying this file to the Docker container as follows:

I also copy the index.html file with the content:

to the /usr/share/nginx/html directory as follows:

It’s done!

Now you can build Docker Image from this Dockfile:

Run this Docker image with the command:

and then access the address http://localhost, you will see the following results:

One thought on “Build custom Nginx Docker image

  1. Thanks a lot for the comprehensive tutorial.
    Can you please suggest how can we deploy this application on K8s and add automated test to ensure app is running on the cluster?

Add Comment