Skip to content
KALANBLON
Show contents

Chapter 1 of 7 · 20 min

Containers and images

By the end of this chapter, you'll be able to

  • Explain what a container is and what sets it apart from a virtual machine
  • Tell an image from a container
  • Run, list, stop and remove containers
  • Read an image's full name: repository, name and tag

What is a container?

A container is a process (a running program) that runs isolated on a Linux machine: it has its own file system, its own visible processes and its own network, but it shares the host system's kernel. The kernel is the heart of the operating system, the part that talks to the hardware.

A virtual machine, on the other hand, simulates a complete computer, with its own kernel. It is more isolated, but much heavier.

                Virtual machine              Container
Contains        a complete system            the application and its files
Kernel          its own                      the host's (shared)
Size            several GB                   a few dozen MB
Start-up        tens of seconds              under a second
Isolation       very strong                  strong, but shared kernel

On macOS and Windows, Docker Desktop runs a small Linux virtual machine, in which the containers live.

Image and container

  • An image is a read-only template: a frozen file system (a Linux base, Python, your code, your libraries) and the command to run. It is the recipe.
  • A container is an image that is running: a live instance, with a writable layer on top of the image. It is the dish cooked from the recipe.

From a single image, you can run as many containers as you want, just as you can cook several dishes from the same recipe.

The container is removed with everything it wrote. The image stays.

Step 5 of 5
Text description of the animation
  1. The image is downloaded from a registry, an online library of images such as Docker Hub.
  2. The image is stored on your machine. It does not run: it is a template.
  3. docker run creates a container from the image and starts it.
  4. The container is stopped. Its modified files still exist.
  5. The container is removed with everything it wrote. The image stays.

The first commands

docker pull python:3.13-slim                 # download an image
docker run --rm python:3.13-slim python -c "print('Hello from a container')"
docker run -d --name web -p 8080:80 nginx    # run a web server in the background
docker ps                                    # list running containers
docker ps -a                                 # also list stopped containers
docker logs web                              # read what the container printed
docker stop web && docker rm web             # stop then remove
docker images                                # list the images present

The options to know:

  • --rm: remove the container as soon as it stops (ideal for a trial);
  • -d: run in the background (detached);
  • --name web: give the container a name, instead of a random one;
  • -p 8080:80: publish a port. Port 8080 of your machine is connected to port 80 of the container. Open http://localhost:8080: the container's web server answers.

Reading an image's name

docker.io/library/python:3.13-slim
└ registry ┘└ repo ┘└ name ┘└ tag ┘
  • the registry: where the image is stored (docker.io is Docker Hub, the default);
  • the name: python, postgres, nginx… official images have no prefix;
  • the tag: the version, here 3.13-slim (Python 3.13 on a reduced Debian base).

Your turn

Worked exercise

Worked exercise: the right words, the right commands

For each need, give the Docker command, then answer the two questions.

1. Download the official Node.js image, version 24.
2. Run an nginx server in the background, named "showcase",
   reachable on port 3000 of your machine.
3. See all containers, including stopped ones.
4. Read what the "showcase" container printed.
5. Stop and remove "showcase".

Question A: after step 5, is the nginx image still on your machine?
Question B: can you run two containers from the same image?
Hint 1
In -p A:B, A is your machine's port and B the container's. nginx listens on port 80.
Hint 2
An image is a template, a container an instance: removing the instance does not remove the template.
Show the solution and common mistakes
docker pull node:24
docker run -d --name showcase -p 3000:80 nginx:1.29
docker ps -a
docker logs showcase
docker stop showcase && docker rm showcase
  • A. Yes. docker rm removes the container, not the image. To remove the image: docker rmi nginx:1.29.
  • B. Yes, as many as you want, as long as you give them different names and, if they publish a port, different ports on your machine.

Common mistakes:

  • Swapping the ports: -p 80:3000 would connect port 80 of your machine to port 3000 of the container, where nginx does not listen.
  • Believing a stopped container is gone: it remains visible with docker ps -a until it is removed.

Summary

  1. 1A container is an isolated process that shares the host's kernel: light and quick to start.
  2. 2An image is a read-only template; a container is an image that is running.
  3. 3docker run creates and starts a container; -d, --name, -p and --rm are the essential options.
  4. 4-p 8080:80 connects port 8080 of the machine to port 80 of the container.
  5. 5Always pin an image's tag instead of relying on latest.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.