Docker images are the fundamental building blocks of containers. These images could be very basic operating environments such, as alpine
or Ubuntu
. Or, the images could craft advanced application stacks for the enterprise and cloud IT environments. An automated approach of crafting Docker images is using a Dockerfile
.
A Dockerfile
is a text-based build script that contains special instructions in a sequence for building the right and the relevant images from the base images. The sequential instructions inside the Dockerfile
can include the base image selection, installing the required application, adding the configuration and the data files, and automatically running the services as well as exposing those services to the external world. Thus, a Dockerfile-based automated build system has remarkably simplified the image-building process. It also offers a great deal of flexibility in the way in which the build instructions are organized and in the way in which they visualize the complete build process.
The Docker engine tightly integrates this build process with the help of the docker build
subcommand. In the client-server paradigm of Docker, the Docker server (or daemon) is responsible for the complete build process and the Docker command line interface is responsible for transferring the build context, including transferring Dockerfile
to the daemon.
To have a sneak peek into the Dockerfile
integrated build system in this section, we introduce you to a basic Dockerfile
. Then, we explain the steps for converting that Dockerfile
into an image, and then launching a container from that image.
Our Dockerfile
is made up of two instructions, as shown here (there is also a copy in the GitHub repo in the chapter02/build_basic
folder):
FROM alpine:latest CMD echo Hello World!!
In the following, we cover/discuss the two instructions mentioned earlier:
apline:latest
image.CMD
, that instructs the container to echo Hello World!!
.Now, let's proceed towards generating a Docker image by using the preceding Dockerfile
by calling dockerimagebuild
along with the path of the Dockerfile
. In our example, we will invoke the dockerimagebuild
subcommand from the directory where we have stored the Dockerfile,
and the path will be specified by the following command:
dockerimagebuild
After issuing the preceding command, the build
process will begin by sending build context to the daemon and then display the text shown here:
Sending build context to Docker daemon 2.048 kB
Step 1/2 : FROM alpine:latest
The build process will continue and after completing itself, it will display the following:
Successfully built 0080692cf8db
In the preceding example, the image was built with IMAGE ID0a2abe57c325
. Let's use this image to launch a container by using the docker container run
subcommand as follows:
docker container run 0080692cf8db
Cool, isn't it? With very little effort, we have been able to craft an image with alpine
as the base image, and we have been able to extend that image to produce Hello World!!
.
This is a simple application, but the enterprise-scale images can also be realized by using the same methodology.
Now, let's look at the image details by using the dockerimage ls
subcommand. Here, you may be surprised to see that the IMAGE
(REPOSITORY
) and TAG
name have been listed as <none>
. This is because we did not specify any image or any TAG
name when we built this image. You could specify an IMAGE
name and optionally a TAG
name by using the docker image tag
subcommand, as shown here:
docker image tag 0080692cf8dbbasicbuild
The alternative approach is to build the image with an image name during the build
time by using the -t
option for the docker image build
subcommand, as shown here:
docker image build -t basicbuild
Since there is no change in the instructions in Dockerfile
, the Docker engine will efficiently reuse the old image that has ID0a2abe57c325
and update the image name to basicbuild
. By default, the build system would apply latest
as the TAG
name. This behavior can be modified by specifying the TAG
name after the IMAGE
name by having a :
separator placed in between them. That is, <image name>:<tag name>
is the correct syntax for modifying behaviors, wherein <image name>
is the name of the image and <tag name>
is the name of the tag.
Once again, let's look at the image details by using the docker image ls
subcommand, and you will notice that the image (Repository) name is basicimage
and the tag name is latest
. Building images with an image name is always recommended as the best practice.
Having experienced the magic of Dockerfile
, in the subsequent sections, we will introduce you to the syntax or the format of Dockerfile
and explain a dozen Dockerfile
instructions.
3.17.155.54