2017-01-10

In our previous articles we have discussed how to Install and setup Docker on CentOS 7.x and Ubuntu Server 16.04. We have also learn how to pull Docker images from Docker Hub / registry. There are some circumstances and use cases that we want to make changes in the Docker images and that changes should be available whenever we launch or provision a Docker container. This can be achieved by three different ways:

Commit

Dockerfile

Docker Compose

Commit is an option in docker command which allows us to save the changes of a container to a Docker image. One of the limitations of commit is that it is not scaleable and very difficult to automate the tasks.In our last article we have also learn how to use Docker commit command. Apart from Docker Commit we can build the Docker images using “Dockerfile“.

Dockerfile is a text file or a script which contains sets of Keywords and set of Linux commands which are executed automatically whenever we build the Docker Image. Creating Docker images using Docker file is similar to template concept of Virtualization world. In this article we will discuss how to create docker images from the Docker file. Some of the Keywords that are generally used in a Dockerfile are listed below:

FROM

From Keyword specify the name of image that will be used as base image while building the Docker Image, Docker command will search this image from local image repository if it is not available in local repository then it will fetch from Registry server

Example:

MAINTAINER

Name of the Community or Guy who maintains docker image is specified under MAINTAINER Keyword.

Example:

RUN

Commands mentioned after RUN keyword will be executed during creation of Docker image

Example :

CMD

Commands mentioned after CMD keyword will executed when a container is launched from a docker image

Example:

Whenever we use multiple CMD keywords then the latest one will get the preference. If you have noticed when we launch a container we get a bash shell this because of CMD keyword (CMD /bin/bash).

ENTRYPOINT

ENTRYPOINT is similar to CMD, Commands after entrypoint will take arguments from CMD or in other words we can say that CMD is the argument for ENTRYPOINT. Commands in ENTRYPOINT will always be executed.

Example:

ENV

With ENV keyword we can set the environmental or shell variables as per requirement, let’s suppose I want build Docker image where I will install java and need to set JAVA path, this can be achieved by using ENV keyword.

Example:

VOLUME

With VOLUME keyword we can attach a folder from Docker engine host to a container.

Example:

EXPOSE

With EXPOSE keyword we can expose application port from Container

Example:

COPY

Using COPY keyword we can copy the files from Docker Host to a container.

Example:

ADD

With ADD keyword we can specify the name of our cloud qcow2 image, which we want to use to build the docker image from the scratch.

Example :

To view the keywords and command that are being used for building docker image use “docker history” command. Let’s see the keywords and commands of a docker image Ubuntu:14.04



In the above output if Keyword is not mentioned in the commands then these commands will be executed in RUN keyword.

Refer the following steps to build the Docker Image using above discussed Keywords.

Step:1 Create a Dockerfile

Let’s first create a folder with the name mycode under / file system and then create Dockerfile under this folder

Note : Name of docker file must be “Dockerfile”, if we don’t follow this convention our docker build command will not work.

Step:2 Add Keywords and Commands to Dockerfile

Open the Dockerfile with vi editor and the following keywords

Save and Exit the file.

Step:3 Build the image using ‘docker build command’

Go to the /mycode folder and run the beneath command, in the below  command -t option is used to set tag name of docker image, in my case i am setting docker image name and tag as “mycentos:apachev1”



Step:4 Verify the new Docker Image and launch a container

Let’s first see whether new docker image is available in local image repository using following command



Now launch a container and see whether apache web server server is running or not and also verify whether the volume is mounted or not.

In the above scenario I have used already downloaded docker image as a base for building new Docker image. In case you want to use your own cloud image (qcow2) as base image for docker then it is also possible with ADD keyword. Make sure you copy the qcow2 file in same folder where your “Dockerfile” resides. Repeat all the above steps and the only change is in the Dockerfile.

Contents of Sample Dockerfile is list below

That’s all, Hope you got an idea how to build your docker images using Dockerfile. Please share your feedback and comments

Show more