This is where Dockerfile comes into the picture; it will help you create custom Docker images. Hence, knowing about Dockerfile is essential.

What is Dockerfile?

It is a simple text file with a set of command or instruction. These commands/instructions are executed successively to perform actions on the base image to create a new docker image. comments and commands + arguments are two kinds of main line blocks in Dockerfile syntax Comments Syntax Commands + Arguments Example Below is how your workflow will look like.

Create a Dockerfile and mention the instructions to create your docker image Run docker build command which will build a docker image Now the docker image is ready to be used, use docker run command to create containers

Basic Commands

FROM – Defines the base image to use and start the build process. RUN – It takes the command and its arguments to run it from the image. CMD – Similar function as a RUN command, but it gets executed only after the container is instantiated. ENTRYPOINT – It targets your default application in the image when the container is created. ADD – It copies the files from source to destination (inside the container). ENV – Sets environment variables.

How to create a Docker Image with a Dockerfile?

Firstly, let’s create a Dockerfile. Put the below commands/instructions in it and save it. In this Dockerfile, ubuntu is set as the base image. Then necessary commands and arguments are mentioned to install MongoDB. Port 27017 is exposed to MongoDB with default container command as usr/bin/mongodb Next, I will run it to create a docker image.

Running a Dockerfile

The following command will create a docker image called geekflare_mongodb after successful execution. Let us check if the docker image got created with the name geekflare_mongodb. Run the docker image geekflare_mongodb inside a container mongo_container. Open a new terminal and check if mongo_container is running. As you can see, the container created from geekflare_mongodb image is up and running. I hope this gives you an idea about dockerfile and its benefits. You may also check out this documentation on Dockerfile best practices to learn more.

What is Dockerfile and How to Create a Docker Image  - 14