Docker: Image vs Container – What is What? – The Main Difference

In the Docker’s world there are such concepts as images, containers and layers.

They are closely related, but distinct and at the beginning of studying Docker there is often a confusion in understanding of these concepts.

This article explains the differences between images, containers and layers.

Cool Tip: How do i list ( running | stopped | all ) Docker containers! Read More →

What’s a Docker Image?

Do Not Confuse: Docker images themselves are never “started” and never “running”. The docker run command takes the Docker image as a template and produces a container from it.

Images are created from a Dockerfile with the docker build command.

Images are stored in a Docker registry, such as Docker Hub and can be downloaded with the docker pull 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 
Digest: sha256:ec0e4e8bf2c1178e025099eed57c566959bb408c6b478c284c1683bc4298b683
Status: Downloaded newer image for ubuntu:latest

To show the downloaded Docker images, run docker images:

$ docker images
REPOSITORY         TAG                IMAGE ID           CREATED            SIZE
ubuntu             latest             00fd29ccc6f1       2 weeks ago        111MB

Cool Tip: Clean up a Docker host by removing unused Docker images! Read More →

What are the Docker Layers?

Docker images are designed to be composed of a series of layers.

Each instruction in a Dockerfile creates a layer in the image.

Each layer is a set of differences from the previous layer.

To show the layers of a Docker image, run the docker history 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 a Docker image is read-only.

What’s a Docker Container?

The Main Difference: The main difference between a container and an image is the top writable layer.

Containers are created from images with the docker run command and can be listed with the docker ps command.

$ docker run -it ubuntu /bin/bash

$ docker ps
af588b25a4ad   ubuntu   "/bin/bash"  24 seconds ago  Up 23 seconds          jovial

To create a container, Docker engine takes an image, adds the top writable layer and initializes various settings (network ports, container name, ID and resource limits).

All write operation inside the container are stored in this writable layer, so when the container is deleted, the writable layer is also deleted while the underlying image remains unchanged.

As each container has its own writable container layer, and all changes are stored in this container layer, multiple containers can share access to the same underlying image and yet have their own data state.

Cool Tip: Clean up a Docker host by removing unused Docker containers! Read More →