• In this chapter, you will study the form and function of a Dockerfile and
its directives, including FROM, LABEL, and CMD, with which you will
dockerize an application.
• The chapter will provide you with knowledge of the layered filesystem of
Docker images and the use of caching during the Docker build process.
• By the end of this chapter, you will be able to write a Dockerfile using
the common directives and build custom Docker images with the
Dockerfile.
• While it is useful to get pre-built Docker images from Docker Hub, we
must know how to create custom Docker images.
• This is important for running our applications on Docker by installing
new packages and customizing the settings of the pre-built Docker
images.
• In this chapter, we are going to learn how to create our custom Docker
image and run a Docker container based on it.
• This will be done using a text file called a Dockerfile.
• This file consists of commands that can be executed by Docker to
create a Docker image.
• Docker images are created from a Dockerfile using the docker
build (or docker image build) command.
• A Docker image consists of multiple layers, each layer representing the
commands provided in the Dockerfile.
• These read-only layers are stacked on top of one another to create the
final Docker image.
• Docker images can be stored in a Docker registry, such as Docker
Hub, which is a place where you can store and distribute Docker
images.
• A Docker container is a running instance of the Docker image.
• One or more Docker containers can be created from a single Docker
image using the docker run (or docker container run)
command.
• A Dockerfile is a text file that contains instructions on how to create
a Docker image.
• These commands are known as directives.
• A Dockerfile is a mechanism that we use to create a custom Docker
image as per our requirements.
• The format of a Dockerfile is as follows:
• A Dockerfile can contain multiple lines of comments and directives.
• These lines will be executed in order by the Docker Engine while
building the Docker image.
• All statements starting with the # symbol will be treated as a comment.
• Instructions within the Dockerfile are not case-sensitive.
• Even though the DIRECTIVE is case-insensitive, it is a best practice to
write all directives in uppercase to distinguish them from arguments.
• The FROM directive
• The LABEL directive
• The RUN directive
• The CMD directive
• The ENTRYPOINT directive
• A Dockerfile usually starts with the FROM directive.
• This is used to specify the parent image of our custom
Docker image.
• The parent image is the starting point of our custom Docker
image. All the customization that we do will be applied on
top of the parent image.
• The parent image can be an image from Docker Hub, such as
Ubuntu, CentOS, Nginx, and MySQL. The FROM directive
takes a valid image name and a tag as arguments.
• A LABEL is a key-value pair that can be used to add
metadata to a Docker image.
• These labels can be used to organize the Docker images
properly.
• An example would be to add the name of the author of the
Dockerfile or the version of the Dockerfile.
• The RUN directive is used to execute commands during the
image build time.
• This will create a new layer on top of the existing layer,
execute the specified command, and commit the results to
the newly created layer.
• The RUN directive can be used to install the required
packages, update the packages, create users and groups,
and so on.
• A Docker container is normally expected to run one process.
• A CMD directive is used to provide this default initialization
command that will be executed when a container is created
from the Docker image.
• A Dockerfile can execute only one CMD directive.
• If there is more than one CMD directive in the Dockerfile,
Docker will execute only the last one.
• Similar to the CMD directive, the ENTRYPOINT directive is
also used to provide this default initialization command that
will be executed when a container is created from the Docker
image.
• The difference between the CMD directive and the
ENTRYPOINT directive is that, unlike the CMD directive, we
cannot override the ENTRYPOINT command using the
command-line parameters sent with the docker
container run command.
• A Docker image is the template used to build Docker containers.
• This is analogous to how a house plan can be used to create multiple
houses from the same design.
• If you are familiar with object-oriented programming concepts, a
Docker image and a Docker container have the same relationship as a
class and an object.
• A Docker image is a binary file consisting of multiple layers based on
the instructions provided in the Dockerfile.
• These layers are stacked on top of one another, and each layer is
dependent on the previous layer.
• All the layers of the Docker image are read-only.
• The ENV directive • The USER directive
• The ARG directive • The VOLUME directive
• The WORKDIR directive • The EXPOSE directive
• The COPY directive • The HEALTHCHECK directive
• The ADD directive • The ONBUILD directive
• The ENV directive in Dockerfile is used to set
environment variables.
• Environment variables are used by applications and
processes to get information about the environment in
which a process runs.
• One example would be the PATH environment variable,
which lists the directories to search for executable files.
•
•
•
•
•
•
•
•
•