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:
<h1> Hello! Welcome to Linuxhint Tutorial</h1>
Step 2: Create Dockerfile
To containerize the HTML program, first, create a file named “Dockerfile” and copy the below code block into the file:
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:
Note: If the Dockerfile exists in another directory, you can specify the Dockerfile with path using the “-f” option as follows:
Step 4: Run Docker Image
After creating the image, run the images to create and fire up the container:
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.
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.