Docker series (part 2 of 4): Docker images and containers
One of the important components in the containerization process is an Image. Building an image is a mandatory step in your journey of creating a container.
Thankfully Docker Desktop makes it quite easy to build an image from a Dockerfile. These images can also be stored in a central repository that can be used by anyone to create their application on top of your image.
To jog your memory, in the previous post, we had seen the basics of Docker, how it is useful and the steps to install a Docker Desktop in your machine. We also began our journey of containerization in the Docker Desktop by creating a simple Dockerfile using some popular commands.
In this post, we are going to continue our journey from where we left off by first building an image from the Dockerfile. Then we will convert the image into a container to start using it. But first, we are going to start by elaborating on the difference between image and container.
Table of Contents
- What is Docker image?
- How is a Docker image different from a container?
- Multiple Layers of Docker Images
- How to create a Docker image?
- How to pull and push an image?
What is a Docker Image?
A Docker image is a template with multiple layers that has instructions for creating a Docker container. These instructions are described in the Dockerfile.
These images typically house all the elements such as code, config files, libraries, environment variables, etc which are needed to run a containerized application. They have to be deployed to a Docker environment first to be executed as a container. The
run command is used to create a container from an image.
A Docker image is reusable and can be used on multiple host systems. Another interesting feature of a Docker image is its immutability or in other words, being read-only. However, they can be duplicated, shared with other systems or even be deleted. The immutability feature helps while you test new applications as it safeguards the base image from any malicious modifications. The original image will be always retained and can be used any number of times.
How is a Docker image different from a container?
A container can be defined as a running instance of an image. A container can also be defined as a standard unit of application that contains all the necessary executable files and configuration files required to run an application. Containers can share their own resources and access the resources of other containers. A Command Line Interface is used to start, run, stop, move and delete a container.
Let us understand the difference between image and container with a simple analogy. Imagine you need a family picture to hang on your wall. Thankfully, you have the picture in the digital form stored on your laptop. This digital picture is sent to a printer to generate the printout of the image. Ultimately, the printout is the actual usable form that will serve your requirement and not the digital image. Here, the printer represents the Docker Desktop and the printout is the container while the digital image is the Docker Image. The Docker image is converted into a container with the help of the Docker Desktop.
Multiple Layers of Docker Images
A Docker image is typically multi-layered. An image often starts with a base layer. On top of the base layer, other layers are added one by one. These intermediate layers are cached to ensure that you can view the top layers easily. Whenever a container is created, a writable layer is also created. This layer is known as the container layer and it stores all the changes done to the running container. It can store new files, modifications to existing files and the records of newly deleted files. The best part is that any changes done to the container layer are saved only on that layer. The container layers enable the possibility of many containers sharing the same underlying images with their own data superimposed on it.
How to create a Docker Image?
Let us quickly revisit the Dockerfile we had created in our previous post. The below snippet displays the Dockerfile we had created using some popular commands and saved the file without an extension.
Once this is done, the next step is to convert it into an image. You can run the below command from the directory where the Dockerfile is located in the Command Line Interface of your Docker Desktop–
docker build -t partech
–t command here specifies the image name (partech). You can also optionally use a username and tag the image in
username/imagename: tag format. Once this is done, you can always verify whether your image has been created or not through the below command:
This will display the details about the image you just created.
The next step is to create the container. Always remember that an image is just a template. Your job is not complete by building an image. A container is what we finally need. To convert an image you need to use the
run command as below
docker run –d --name mycontainer partech
In the above command, the container is created in detached mode. Here,
mycontainer is the name of the container while
partech is the name of the image we are running.
The output will be
Hello World!! My first Docker image. You might be wondering from where did we get this output. Recall our Dockerfile and the CMD command that instructed to display this text once the container runs successfully.
How to Pull and Push an Image?
Pushing an image is the process of uploading a Docker image into the Docker registry. Remember, the Docker image that you have created is still in your local machine. It cannot be used anywhere else. To facilitate this, you need to push it into a Docker registry which houses all the Docker images that can be used by everyone.
One of the frequently used Docker registries is Docker Hub. To use Docker Hub, you need to create an account, log in to it and create a repository before pushing an image.
Post this you need to log in to the Docker Hub through the Command-Line:
docker login --username=ruud2020 [email protected]
Once you have successfully logged in, you need to use the push command as below to complete the process:
docker push ruud/partech
Pulling an image is the exact opposite of pushing. You retrieve an image from the Docker registry for your own use. Pulling is achieved through the below command:
docker pull ubuntu
In the above command, the official Ubuntu image is pulled from the Docker registry and you can make your own modifications on top of the image without affecting the base image stored in the registry.
In this post, we have successfully created a Docker from a Dockerfile and also pushed it into the Docker repository. Also, you would have understood the difference between image and container by now. In the next post, we will discuss about Docker Swarm which is a container orchestration tool that enables you to manage multiple containers easily.