A Docker image is the base of the container and without it, the container doesn’t exist. In simple terms, a Docker image is a set of instructions written in a text file in a sequence that is executed to create a container. This set of instructions is treated as a template to create millions of containers.
An image is a read-only template with a set of instructions to create a Docker container. User can create own images and can download the images from an image registry. The most famous repository is Docker Hub. When the user pulls the images from the registry, Docker keeps the copy of that image on the Docker localhost machine and if the user tries to pull it again, it will be referred from the local host machine.
A Docker image can contain anything this is used to run an application such as code, dependencies, configuration files, environmental variables, etc, and this image is used to create the containers.
A single Docker image is compromised of many layers which makes the image lightweight. Every docker image is based on the base image and the parent image of each image is a scratch image. The layered architecture of the image speed up the Docker build and increase usability and eventually save disk space.
The following images shows the layered architecture of an image:
For example: If two images have a common layer, and you download these two images then the common layer will be downloaded once and other different layers will be downloaded separately. So being the common layer of two images saves the disk space.
The following picture shows two images (nginx and httpd) that have a common layer Debian. The Debian layer will be downloaded once and all the layers from 1-4 will be downloaded separately for each image.
Having said that, if each image size is 50 MB out of which 5 MB is used by the Debian layer then instead of consuming 100 MB size, only 95 MB size will be consumed of the disk space.
The images are immutable in nature and once created can’t be modified. You have to create a new image for further changes.
The build command is used to create a new image using the Dockerfile:
[root@instance-20191018-2102 docker]# docker image build .
You can pull the images from the registry using the pull command as following:
[root@instance-20191018-2102 docker]# docker image pull mysql
Ways to create images
There are various ways using which the image can be created:
- Using the Dockerfile: Write a set of instruction in a file and run this using the build command to create an image.
- Using the commit command: Create a container from the image, modify the changes in the container and use commit command to save the changes in a new image.
- Using the import command: Export the container as a tar and use import command of image to create a file system image.
You can subscribe to my YouTube channel for further reading.