| by Arround The Web | No comments

How to Build Your Own Dockerfile, Image, and Container

Docker is an OS-virtualized software forum that allows developers to easily build, deploy, and run applications in Docker containers. The main components of Docker are Dockerfile, Docker images, Docker containers, Docker Hub, Docker registry, etc. Users can easily build Dockerfiles, images, and containers in Docker.

This write-up will illustrate:

How to Build Dockerfile?

Dockerfile is an instruction file that helps to create a snapshot of the container. To create/make a Dockerfile, follow the provided steps.

Step 1: Create Program File
First, create an “index.html” program file and paste the below-provided code into it:

<!DOCTYPE html>
<html>
<body>

<h2>Hello LinuxHint</h2>
<p>This is LinuxHint Logo</p>

<img src="linuxhint.png" alt="linuxhint" width="104" height="142">

</body>
</html>

Step 2: Create Dockerfile
Then, create a new file named as “Dockerfile” and paste the below snippet into it to containerize the HTML program:

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

In the above code:

  • FROM” command is used to specify a base image for the container.
  • COPY” instruction pastes the “index.html” file and “linuxhint.png” image into the container path.
  • ENTRYPOINT” sets the execution point for the container.

How to Build Docker Image?

Docker images are snapshots or templates used to create and manage containers. Utilize the given-below command to build the Docker image from the Dockerfile:

docker build -t linuximg .

Here, “-t” is used to specify the image name. For instance, we have specified the “linuximg” as a name for the Docker image:

Then, ensure that the image has been created successfully using the provided command:

docker images

In the below output, the Docker image can be seen, i.e., “linuximg”:

How to Build Docker Container?

Docker containers are lightweight and small executable packages of Docker used to containerize the application. To build and run a Docker container using the Docker image, execute the given-provided command:

docker run --name imgcontainer -p 80:80 linuximg

Here:

  • –name” is used to specify the container name.
  • imgcontainer” is the name of the container.
  • -p” is utilized to assign the port to the container.

Another way to simply create or build a container is to utilize the “docker create” command:

docker create --name linuxcontainer -p 80:80 linuximg

Lastly, open the desired browser and redirect to the allocated port. Then, verify whether the application is deployed or not:

As you can see, we have successfully built the Dockerfile, image, and container.

Conclusion

Dockerfiles are normal text files that include sets and instructions to build docker images. To build docker images, the “docker build -t <image-name>” command is used. In order to create the Docker container from the Docker image, execute the “docker create –name <container-name> -p <port-no> <image-name>” command. Furthermore, if a user wants to create and run the container, utilize the “docker run” command. This write-up illustrated the procedure of building a Dockerfile, image, and container.

Share Button

Source: linuxhint.com

Leave a Reply