In this article, you will learn how to build a Docker image using Dockerfile. The docker file is a text document that contains all the instructions users provide to assemble an image. Please, read our previous article before proceeding to this article where we learn Docker Networking.
What is an instruction, it can be, for example, installing a program, adding some source code, or specifying the command to run after the container starts up.
So, the docker can build images automatically by reading the set of instructions from a Dockerfile. every single instruction will create a new image layer to the image.
Basically Instructions specify what to do when building the image.
Let’s go ahead and create a Dockerfile. A Dockerfile must not have any extension and must be named as Dockerfile with capital D.
The Dockerfile gets created, let’s open it up. We add our first instruction, FROM instruction. So, the docker runs the instructions in a Dockerfile in order.
The first instruction must be FROM to specify the base image from which you are building. Here, we use Debian Jessie as our base image.
So, Debian Jessie is the argument for the FROM instruction. The instruction is not case-sensitive, however, the convention is for them to be uppercase in order to distinguish them from arguments more easily.
Let’s move to the next instruction, RUN instruction. Run instruction will specify a command to execute. It can be any commands you can run in a Linux terminal.
Here, we do apt-get update first, then we install git. Make sure we put -y option because we won’t be able to answer the prompt. then, install the Vim as well.
Now, we have our Dockerfile ready. then save the file. It is time for us to start building the Docker image.
We have a new command to learn: docker build. Docker build will build the image using the instructions given in the Dockerfile.
Here, we type the docker build. docker build takes a -t option to tag the new image we are building.
As we learn in the previous article, we will tag the image with our own repository name.
Docker build context
Docker build command also requires a path, which is the path to the build context. Actually, the path specifies where to find the files for the “context” of the build on the Docker daemon.
For example, if we would like to copy some source code from the local disk to the container, those files must exist in the build context path. So, the daemon could be running on a remote machine, and that no parsing of the Dockerfile happens on the client-side.
When the build process gets started, the Docker client will first pack all the files in the build context into a tarball and then transfer the tarball file to the Docker daemon.
So, by default, Docker will search for the Dockerfile in the root directory of the build context path.
If your Dockerfile doesn’t live in the build context path, no worries, you can tell Docker to search for a different file by providing a -f option.
Here my Dockerfile is at my current directory. so, we use the current directory as the path. Now, we start the build process.
Let’s go through the build output to get a better understanding of how Docker is building the image.
First, its output sending the build context to the Docker daemon. As we mentioned before, now the Docker client is transferring all the files inside the build context which is my current folder from the local machine to the Docker daemon.
Step 1, From Debian Jessie, that is from instruction. As you can see, Docker is going through the instructions in the Dockerfile.
Then step 2, run apt-get update. Docker is executing the run instruction.
It says running in followed by a container ID. What happens is that Docker starts a new container from the base Debian image and it is executing the apt-get command in the container.
Now, when step 2 is about to finish, it prints out “Removing intermediate container” and followed by a container ID. You might have already noticed the two container IDs are exactly the same.
So, Docker spins up a new container and afterward just removes it.
Docker daemon runs each instruction inside a container. A container is a writable process that will write file system change to an image, in our case it installs a program.
Once Docker has written changes to the image and committed that image, Docker removes the container. So, for instruction, Docker creates a new container, runs the instruction, commits a new layer to the image, and removes the container.
Basically, containers are ephemeral. We just use a container to write image layers and once they are finished, we get rid of them. Images are persistent and read-only.
Now, it says step 3: apt-get install dash y git and followed by a container ID. What happens is that at the end of step 2, Docker committed our container as a new image and it starts a new container from that image for the next instruction.
Now it is installing git.
When step 3 is about to finish, it is repeating the same process to commit the intermediate container as a new image and remove the container.
Let’s move to step 4, it says run apt-get install -y vim and Docker is executing the command in a new container from the image it committed in the previous instruction.
Once all the steps are finished, the build completes successfully. Let’s run docker images to make sure the new image is created successfully.
Thank you for reading this article, I hope you will understand how to build docker images by using docker file. We’ll get into more details about this when we’ll learn how to Create a Containerized .NET Core Web Applications in the next article.