/    /  Docker – File

Docker File

 

As we’ve seen in earlier chapters, Docker hub provides a variety of image files including Centos that you can use to spin up containers.

Docker File

When we run the Docker images command, we can view the existing images on our system. In the screenshot above, we can see one image: centos

Docker also lets you create your own Docker images using Docker Files. Docker Files are just simple text files with instructions on how to build your images.

How to create a Docker File is explained in the following steps.

Step 1 − Make a Docker File and edit it with vim.

$ vi dockerfile

Step 2 − Create your Docker file.

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

The above file has the following points to note:

  • In this Docker File, “#This is a sample Image” is a comment. You can add comments by using the # command.
  • Using the FROM keyword tells Docker from which base image we want to create our image. We’re creating an Ubuntu image in our example.
  • Using the RUN command, we install nginx on our Ubuntu image after updating Ubuntu.
  • Displays a message to the user with the last command.

Docker File

Step-3: Let’s build the dockerfile using this command

$ docker build . 

Here’s what the dockerfile looks like

Docker File

We can see the Ubuntu image has been created in the name of none.

Docker File