How to build Docker Container Images with Dockerfile

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 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:

FROM  Ubuntu:14.04

MAINTAINER

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

Example:

MAINTAINER  Linuxtechi Team <info@linuxtechi.com>

RUN

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

Example :

RUN  apt-get update
RUN apt-get install apache2 -y
RUN echo 'Web site Hosted inside a container' > /var/ww/html/index.html
RUN echo 'apache2ctl start' >> /root/.bashrc

CMD

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

Example:

CMD /bin/bash
CMD wget {url}

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:

ENTRYPIONT ["curl"]
CMD ["www.google.com"]

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:

ENV JAVA_HOME= /usr/jdk/jdk{version}

VOLUME

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

Example:

VOLUME  /mnt/dock_storage  /data

EXPOSE

With EXPOSE keyword we can expose application port from Container

Example:

EXPOSE 8080

COPY

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

Example:

COPY /my.txt   /etc

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 :

FROM scratch
ADD centos-7-3.qcow2

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

# docker history ubuntu:14.04

docker-history-command-output

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

[root@dke1 ~]# mkdir /mycode
[root@dke1 ~]# cd /mycode/
[root@dke1 mycode]# touch Dockerfile
[root@dke1 mycode]#

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

[root@dke1 mycode]# vi Dockerfile
FROM mycentos:v2

MAINTAINER  Linuxtechi Team <info@linuxtechi.com>

RUN yum -y install httpd php

RUN echo "Website is hosted inside a container" > /var/www/html/index.html

EXPOSE 80

VOLUME /mnt/docker_vol  /data

RUN echo "httpd" >> /root/.bashrc

CMD ["/bin/bash"]

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

[root@dke1 mycode]# docker build -t mycentos:apachev1 .

docker-build-command-output

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

[root@dke1 ~]# docker images

docker-images-command-output

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.

[root@dke1 ~]# docker run -it -p 80:80 --name=web_container1 mycentos:apachev1

docker-run-command-output

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

FROM scratch

MAINTAINER  Linuxtechi Team <info@linuxtechi.com>

ADD CentOS-7-x86_64.qcow2 /

RUN yum -y install httpd php

RUN echo "Website is hosted inside a container" > /var/www/html/index.html

EXPOSE 80

VOLUME /mnt/docker_vol  /data

RUN echo "httpd" >> /root/.bashrc

CMD ["/bin/bash"]

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

Leave a Reply

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