Docker

How to Build a Docker Image From a Dockerfile

Docker images are referred to as a template or framework that is used to build the Docker containers. The Docker image not only builds the container but also instructs the container how to containerize and deploy the application or service. Docker images are created by Dockerfile. More specifically, Dockerfile is an instructions file that specifies the application and its dependencies to build the Docker image.

This post will provide the method to build or generate the Docker image from the Dockerfile.

Building a Docker Image from a Dockerfile

Dockerfile is referred to as an instructions file that includes basic and essential instructions to generate the Docker image, such as base image, source application, entry points, working directory, essential dependencies, and many more.

To create the image by defining the Dockerfile instructions, go through the listed steps.

Step 1: Create a Program File
First, create a program file “index.html” and paste the below snippet into the file. This code will execute the simple HTML page or program:

<html>
 <head>
  <style>
   body{
    background-color: black;
   }
   h1{
    color:aquamarine;
    font-style: italic;
   }
  </style>
 </head>
 <body>
  <h1> Hello! Welcome to Linuxhint Tutorial</h1>
 </body>
</html>

Step 2: Create Dockerfile
To containerize the HTML program, first, create a file named “Dockerfile” and copy the below code block into the file:

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]

In the above code:

  • FROM” statement specify the “nginx:latest” as base image.
  • COPY” instruction copies the “index.html” file into the container path.
  • ENTRYPOINT” sets the executable for the container. For instance, we have set the three executables or default, and “nginx” will execute the copied source file “index.html”:

Step 3: Build a Docker Image From Dockerfile
Next, build the Docker image by reading the Dockerfile instructions using the mentioned command. The “-t” defines the image name:

> docker build -t html-img .

Note: If the Dockerfile exists in another directory, you can specify the Dockerfile with path using the “-f” option as follows:

> docker build -t  -f Dockerfile .

Step 4: Run Docker Image
After creating the image, run the images to create and fire up the container:

> docker run -d -p 80:80 html-img

For verification, open the browser and navigate to an allocated port and check if the application is deployed or not. You can see we have successfully generated and run the image from Dockerfile instructions:

That’s all about how to create or generate the image from the Dockerfile.

Conclusion

To build the Docker image, first create a simple Dockerfile that contains some essential instructions such as base image, source file, required dependencies, and many more. Then, generate the image using the “docker build -t <image-name>” command. This write-up has demonstrated how to build or create a Docker image from the Dockerfile.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.