Open In App

How to Create Docker Image?

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Docker is a powerful containerization tool that enables developers to package their applications and their dependencies into a single unit called Docker image. The Docker image offers seamless deployment, scalability, and portability. In this article, I will make sure that you understand what is docker and guide you through the steps to create a Docker image.

What is Docker?

Docker is a tool that simplifies software packaging and deployment. It wraps application and their dependencies into compact units called containers. Docker containers share a host operating system kernel which makes them lightweight, fast, and resource-efficient. It provides a standardized way to package, distribute, and execute applications. It allows developers to create, test, and deploy applications on any operating system or cloud platform without any worry about compatibility issues or hardware requirements.

Steps To Create A Docker Image

Step 1 : Start the Docker engine . On windows just start the Docker Desktop .

docker-desktop

On Ubuntu start the docker service using the command:

sudo systemctl start docker

Step 2 : Create a html file . We are going to dockerize a html file here in the upcoming steps .

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>How to create docker image</title>
</head>
<body>
    <H1>Hey geeks to create a docker image you have to write a Dockerfile and then build it using
        docker command .
    </H1>
</body>
</html>

html-file

Step 3 : Then create a text document having name Dockerfile in the same folder as the html file. Here in this Dockerfile, all the instructions and commands will be written to create a Docker image . In the Dockerfile specify the base image , working directory and port number on which the website is running .

FROM nginx:alpine
COPY index.html /usr/share/nginx/html
EXPOSE 80

dockerfile

Step 4 : Build a docker image . Here mention a name for the Docker image and also mention the path of Dockerfile .

docker build -t gfg-image .

docker-build-successful

Step 5 : After successful build you can check whether the image is present or not .

docker images | grep gfg-image

verify-the-image-creation

Step 6 : Then you can run the image and access it through localhost .

docker run -d -p 80:80 gfg-image

Here -d indicates that container running in detached mode and -p indicates the port number through which you can access the website .

run-the-docker-image

Now access the website on http://localhost:80 .

accessed-website

Conclusion

You have successfully learned about what is Docker and also created a Docker image for a simple static website . As you learn more about Docker , you will find it to be a valuable tool in your development and deployment workflows .

Frequently Asked Questions On How To Create Docker Image

What Is a Dockerfile?

Dockerfile is a text document which contains a set of instructions to build a Docker image . It specifies the base image, working directory, dependencies , port number and other commands to configure the image .

How To Build a Docker Image From a Dockerfile ?

To build a Docker image from a Dockerfile , you have to use “docker build -t <image name> <path of Dockerfile>” command .

What Is Docker Registry ?

A Docker Registry is a storage for named Docker images . It also allows you to distribute Docker images to different environments .

How To View The Running Containers ?

Use command ‘docker ps’ to see the running docker containers .

Can I Edit a Docker Image After It Is Created ?

The answer is no . You have to make changes in the Dockerfile and then rebuild the image .



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads