How to create a Docker File

Last updated on Sep 26 2022
Neha Warghade

Table of Contents

How to create a Docker File

Docker – File

We have seen the various Image files such as Centos which get downloaded from Docker hub from which you can spin up containers. An example is again shown below.

doc 95

If we use the Docker images command, we can see the prevailing images in our system. From the above screenshot, we can see that there are two images: centos and nsenter.

But Docker also gives you the capability to create your own Docker images, and it can be done with the help of Docker Files. A Docker File is a simple text file with instructions on how to build your images.

The subsequent steps explain how you should go about creating a Docker File.

Step 1 − Create a file called Docker File and edit it using vim. Please note that the name of the file has to be “Dockerfile” with “D” as capital.

doc 96

Step 2 − Build your Docker File using the subsequent instructions.

#This is a sample Image FROM ubuntu MAINTAINER demousr@gmail.com  RUN apt-get update RUN apt-get install –y nginx CMD [“echo”,”Image created”]

The subsequent points need to be noted about the above file −

  • The first line “#This is a sample Image” is a comment. You can add comments to the Docker File with the help of the # command
  • The next line has to start with the FROM It tells docker, from which base image you want to base your image from. In our example, we are creating an image from the ubuntu image.
  • The next command is the person who is going to maintain this image. Here you specify the MAINTAINER keyword and just mention the email ID.
  • The RUN command is employed to run instructions against the image. In our case, we first update our Ubuntu system and then install the nginx server on our ubuntu
  • The last command is employed to display a message to the user.

Step 3 − Save the file. Within the next chapter, we will discuss how to build the image.

doc 97

Docker – Building Files

We created our Docker File within the last chapter. It’s now time to build the Docker File. The Docker File can be built with the subsequent command −

docker build

Let’s learn more about this command.

docker build

This method allows the users to build their own Docker images.

Syntax

docker build  -t ImageName:TagName dir

Options

  • -t − is to mention a tag to the image
  • ImageName − This is the name you want to give to your image.
  • TagName − This is the tag you want to give to your image.
  • Dir − The directory where the Docker File is present.

Return Value

None

Example

sudo docker build –t myimage:0.1.

Here, myimage is the name we are giving to the Image and 0.1 is the tag number we are giving to our image.

Since the Docker File is within the present working directory, we employed “.” at the end of the command to signify the present working directory.

Output

From the output, you will first see that the Ubuntu Image will be downloaded from Docker Hub, because there is no image available locally on the machine.

doc 98

Finally, when the build is complete, all the necessary commands would have run on the image.

doc 99

You will then see the successfully built message and the ID of the new Image. When you run the Docker images command, you’d then be able to see your new image.

doc 100

You can now build containers from your new Image.

So, this brings us to the end of blog. This Tecklearn ‘How to create a Docker File’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Docker and build a career in DevOps domain, then check out our interactive, Containerization using Docker Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/containerization-using-docker/

Containerization using Docker Training

About the Course

Tecklearn has specially designed this Containerization using Docker Training Course to advance your skills for a successful career in this domain. his Docker training online course will help you learn Docker containerization, running Docker containers, Docker image creation, Dockerfile, Docker orchestration, security best practices and more through hands-on projects and case studies. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Docker.

Why Should you take Containerization using Docker Training?

  • Average salary of Docker Expert is $110k – Indeed.com
  • According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.
  • Intuit, PayPal, Splunk, Uber & many other MNC’s worldwide use Docker across industries

What you will Learn in this Course?

Introduction to DevOps

  • What is Software Development
  • Software Development Life Cycle
  • Why DevOps?
  • What is DevOps?
  • DevOps Lifecycle
  • DevOps Tools
  • Benefits of DevOps
  • How DevOps is related to Agile Delivery
  • DevOps Implementation

Containerization using Docker – Part 1

  • Introduction to Docker
  • Understanding Docker Lifecycle
  • Docker Architecture
  • Components of Docker Ecosystem
  • Common Docker Operations
  • Committing changes in a Container
  • Hands On

Containerization using Docker – Part 2

  • Building Custom Docker Images
  • Docker Image
  • Introduction to Docker Swarm
  • Deploying a 2-Node Cluster using Docker Swarm
  • Hands on

 

0 responses on "How to create a Docker File"

Leave a Message

Your email address will not be published. Required fields are marked *