How to Build Docker Images with DockerFile

In our previous article, we got to know how to install Docker engine and covered some important Docker commands we use to get the things done from Docker containers.

As you are already aware, Docker images are the base of containers. Whenever you provision or launch a container, Docker will search for the image locally on your Docker host and try to use them. In case the image is not found locally then it will download images from Docker public registry.

Docker images you get from public registry may not have something you want, may have something you don’t want. So in this case, you can build Docker images with a set of application on it and launch a container with that set of application which will help to improve your productivity.

Dockerfile is a simple text file which contains all the commands (set of Linux commands and Docker keywords) which are executed automatically when we build a Docker image.

Docker Keywords:

FROM:

The From keyword tells the Docker to use this image as Base Image for building the Docker container image. If the Base image is not found locally, Docker will fetch the image either from Docker Hub or Private Registry server.
Docker file must have FROM on the first line of instruction; you can use FROM multiple times within a single Dockerfile to create multiple images.

The RUN instruction is used to execute commands on the Docker image (centos:7) and commit the results. For simple understanding, use this RUN to install packages, run shell commands, and almost any commands you use in an operating system.

Example,

RUN yum makecache
RUN yum install -y httpd
RUN echo "This Page Designed by ITzGeek, for Docker Build"

CMD:

The CMD is used to run the application installed on your Docker image. For Ex, if you want to run a Apache server when a container is launched from a Docker image; you should add the following line.

Note: JSON format is used when you want to run a “command” without a shell.

Example,

CMD ["apache2","-DFOREGROUND"]

To run an application with a shell and get a bash prompt,

CMD service httpd restart && /bin/bash

PS: If you have more than one CMD then only the last CMD will take effect.

ENTRYPOINT:

The ENTRYPOINT is also similar to CMD.

Example,

ENTRYPOINT service httpd restart && /bin/bash

You can also use ENTRYPOINT and CMD together. When combining ENTRYPOINT and CMD, ENTRYPOINT will take arguments from CMD. To start the httpd service when a container is launched from a Docker image, you need to add the following line.

ENTRYPOINT [ "/usr/sbin/httpd" ]
CMD ["-D", "FOREGROUND"]

EXPOSE:

The EXPOSE instruction will be used to expose the application (httpd) port from a container.

Example,

EXPOSE 80

ENV:

Use ENV keyword to set an environmental variable or shell variable for your application. If you want to set a JAVA variable, you can add the following line.

Example,

ENV JAVA_HOME=/usr/jdk1.8.0_05

ADD Or COPY:

The function of ADD or COPY are similar, COPY is preferred. COPY helps you to copy the local files into the container. ADD has some advanced features such as local file auto-tar extraction to the image and remote download support.

With VOLUME, you can create a mount point inside the container and make it available to native hosts or other containers. “VOLUME /data” creates “/data” mount point inside the container, the files inside “/data” will be persistent and same can be attached to an external host using -v or –volumes-from when launching a container from Docker image.

VOLUME /data

USER:

The USER sets the username or UID for RUN, CMD and ENTRYPOINT keywords.