How to Build CentOS Docker Image using Dockerfile

Posted on

Introduction

This is an article for describing about how to build a docker image. The image is a CentOS docker image. Building the image itself is done using Dockerfile. It is actually a file descriptor for building the docker image. The file name is Dockerfile. There are several steps for building the docker image. In the following section, the discussion is focusing on the process of building the docker image of CentOS operating system.

Creating Dockerfile for Building CentOS Docker Image

The following is the steps for building CentOS docker image using the file with the name of ‘Dockerfile’. Just create a file with the name of ‘Dockerfile’. Just place it in any location. The content of that file is available as follows :

#Getting base image from CentOS
FROM centos:7

MAINTAINER maintainer_name 

LABEL Remarks=”xxxxxxxxxxxx”

The first line as the content of the Dockerfile, it is just a normal comment or remark. It is informing that the following syntax is for getting base image of CentOS.

In the above content, the next line has a reserved keyword syntax of ‘FROM’. This syntax, FROM is very useful to inform docker to build the image from a certain base image. In the context of the above docker image build description, it will use ‘centos’ as its base image. Furthermore, it will actually use CentOS with a specific version. It is using CentOS 7. There can be another base image besides centos, it can be for example ubuntu:16.04, etc.

The next line, the reserved keyword syntax is the MAINTAINER. It is only giving information about the maintainer of docker image. The maintainer name and even the email contact for that maintainer.

Continue on the next line, it is the reserved keyword of LABEL. There is an attribute of the reserved keyword ‘LABEL’ and it is the ‘Remarks’ one. Basically it just giving some remarks. There is also another label for number but just keep it short to avoid for giving unnecessary labels.

Using the above content’s pattern, the following is the example of a Dockerfile content :

#Getting base image from CentOS
FROM centos:7

MAINTAINER myself <myself@mymail.com> 

LABEL Remarks=”This is a dockerfile example for Centos system”


Building CentOS Docker Image using the reference from Dockerfile

After creating the necessary file describing for the CentOS docker image build process, execute the following command. It is a specific command for building docker image from the ‘Dockerfile’ file description :

user@hostname:~/docker/centos$ docker build -t centos:7 . 
Sending build context to Docker daemon  5.632kB
Step 1/3 : FROM centos:7
7: Pulling from library/centos
Digest: sha256:307835c385f656ec2e2fec602cf093224173c51119bbebd602c53c3653a3d6eb
Status: Downloaded newer image for centos:7
 ---> 67fa590cfc1c
Step 2/3 : MAINTAINER myself <myself@mymail.com>
 ---> Running in f2e33e07a7ee
Removing intermediate container f2e33e07a7ee
 ---> 8b934da37b75
Step 3/3 : LABEL Remarks="This is a dockerfile example for Centos system"
 ---> Running in 8d43600ffdd8
Removing intermediate container 8d43600ffdd8
 ---> 75c59985d814
Successfully built 75c59985d814
Successfully tagged centos:7
user@hostname:~/docker/centos$ 

The execution of the above command is showing a success for building a docker image. In order to prove that the docker image exist after successfully building the docker image above, execute the following command :

user@hostname:~/docker/centos$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
centos                7                   75c59985d814        25 seconds ago      202MB
...
user@hostname:~/docker/centos$ 

Just read the article in this link to read more about listing the available docker images. It is an article with the title of ‘How to Show Available Docker Images in the Local Machine’.

Leave a Reply