In the Docker-world there are such concepts as images, containers and layers. Despite the fact that they are closely related – they are different, and at the beginning of the Docker study, there are often difficulties in understanding these terms. This article explains the difference between images, containers and layers.
What is an Image in a Docker?
Do not Confuse: The Docker image itself can not be “run”. The command
docker runtakes the Docker image as a template and creates a container from it, which is launched. Images are created from
Dockerfileusing the command
Images are stored in Docker registries, such as Docker Hub and can be downloaded using the command
$ docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
50aff78429b1: Pull complete
f6d82e297bce: Pull complete
275abb2c8a6f: Pull complete
9f15a39356d6: Pull complete
fc0342a94c89: Pull complete
Status: Downloaded newer image for ubuntu:latest
To view the downloaded Docker images, run
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 00fd29ccc6f1 2 weeks ago 111MB
What Are Layers in a Docker?
The images in Docker are so arranged that they consist of several layers. Each statement from
Dockerfilecreates a new layer of the image. Each layer is a collection of differences (diff) from the previous layer.
To view all layers of the image, run the command
$ docker history 00fd29ccc6f1
IMAGE CREATED CREATED BY SIZE
00fd29ccc6f1 2 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 2 weeks ago /bin/sh -c mkdir -p /run/systemd && echo '... 7B
<missing> 2 weeks ago /bin/sh -c sed -i 's, except the top one,/... 2.76kB
<missing> 2 weeks ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B
<missing> 2 weeks ago /bin/sh -c set -xe && echo '#!/bin/sh' >;... 745B
<missing> 2 weeks ago /bin/sh -c #(nop) ADD file:f5a2d04c3f3cafa... 111MB
Each layer of the Docker image is read-only.
What is a Docker Container?
Main Difference: The main difference between the image and the container is in the writeable top layer.
Containers are created from images using the command
docker run, and by running the command
docker psyou can find out which containers are currently running.
$ docker run -it ubuntu /bin/bash
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af588b25a4ad ubuntu "/bin/bash" 24 seconds ago Up 23 seconds jovial
To create a container, the Docker engine takes an image, adds a writeable top layer, and initializes various parameters (network ports, container name, identifier, and resource limits).
All write operations inside the container are stored in this top layer and when the container is deleted, the top layer that was available for writing is also deleted, while the lower layers remain unchanged.
Because each container has its own layer for writing, and all changes are stored in this layer, several containers can share access to the same basic image while remaining each in their own state.
Join our reader's list to start receiving latest updates.
Get free Tech, Linux tips and resources delivered directly to your inbox.