DockerFile Nginx

Dockerize your Nginx with Custom index.html

What is Dockerfile?

  • Dockerfiles enable you to create your own images.
  • A Dockerfile describes the software that makes up an image.
  • Dockerfile is a simple text file that consists of instructions to build Docker images.
  • Each instruction present in the docker file represents a layer of the docker image.

Here, we will be creating an Nginx application and with custom index.html using a docker file.

This example includes the following steps.

Github link for Dockerfile -https://github.com/bikramatmedium/docker/tree/main/Dockerfile/nginx

  1. Create an index.html custom Nginx HTML file.
$ vi index.htmlMy first Dockerfile with Nginx!! Version-1.0

2. Create a Dockerfile that contains instructions for the Docker

# Base Image 
FROM nginx:alpine
# MAINTAINER of the Dockerfile
MAINTAINER Bikram <bikramatmedium@gmail.com>
#Copy the index.html file /usr/share/nginx/html/
COPY index.html /usr/share/nginx/html/
#Expose Nginx Port
EXPOSE 80
#Start NginxService
CMD ["nginx", "-g", "daemon off;"]

3. After creating Dockerfile, Create a Docker Image.

$ docker build -t nginx-app .

4. Now, we have created a docker image. let’s Create a Container

$ docker run -d -p 80:80 --name Nginx-app nginx-app:latest

An explanation of this command is as below,

  • docker run — command is used to create a docker container.
  • -d option is used to run a container in the background and print container ID.
  • -p option is used for port mapping between container and Host machine.
  • — name option is used to assign a name to the container.
  • In the end, we provide the name of the docker image from which we wish to run our container.

This will start serving the static site on port 80. If you visit http://ip:80 in your browser.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store