“It works on my machine”
What exactly a container carries along with the program.
A program almost never runs on its own. It needs an interpreter or runtime of a particular version, system libraries, environment variables and configuration files. On a developer's laptop all of this was installed by hand at some point, and nobody remembers the order any more. The server has a different library version, and the same program crashes. “It works on my machine” is usually honest: the program really works, but together with an environment that never moved to the server.
Docker moves the program together with its environment. Everything it needs is described in an image: an immutable template with a file system and a default start command. A container is started from an image — an ordinary process that sees the image's files, its own variables and its own network settings. You can start as many containers from one image as you like, and they do not get in each other's way.
The difference between an image and a container is roughly that between an installation package and a running program. The image sits on disk and never changes. The container lives: it writes files, holds connections and finishes with an exit code. Nothing a container writes inside itself ends up in the image, so the next container from the same image starts from a clean state.
An example: check the Python version without installing Python on your computer.
docker run --rm python:3.12-slim python --version
# Python 3.12.x
Docker downloads the image if it is not available locally yet, starts a container, runs python --version and removes the container when it finishes, thanks to the --rm flag.
A common mistake is to think that a container erases every difference between machines. The container takes the operating system kernel from the host, and the processor architecture matters too: an image built only for amd64 will not run directly on an arm64 server. A container removes differences in libraries and settings, but not in hardware or the kernel.
Practice
Image and container
How does an image differ from a container in Docker?
- An image is a running process, a container is a file on disk
- An image is an immutable template, a container is an instance started from it
- They are two names for the same thing
- A container stores source code and an image stores only logs
What is inside an image
What usually goes into an application image? Mark all that apply.
- A runtime of the required version
- System libraries
- The host's operating system kernel
- The default start command
Three runs
docker runwith the same image was executed three times in a row, without--rm. How many containers appeared? Answer with a number.