Images and containers
- Docker & Kubernetes course
- Beginner
- Lesson 3/82
- 4 min
Learn the relationship between images and containers.
Image and container
An image is the read-only template of an application: the file system, libraries and start command. A container is a running instance of that image. Think of a class and its objects: from a single nginx image you can start as many containers as you want.
docker run -d --name web1 nginx
docker run -d --name web2 nginx # same image, two separate containersThe writable layer
When a container starts, a thin writable layer is added on top of the image. Files created inside the container are written to this layer and disappear when the container is removed; the image never changes. Volumes are used for persistent data (you'll see them later).
docker run --name deneme alpine touch /tmp/not.txt
docker rm deneme # /tmp/not.txt is gone too
docker run alpine ls /tmp # the image is still clean: emptyPractice Two containers from one image
Write two commands, one per line, starting two containers named web1 and web2 from the nginx image in the background (-d).
Starter code
# buraya yazSample solution
docker run -d --name web1 nginx
docker run -d --name web2 nginxExpected output
3f2a9c1b7e44 8d1e0f6a2b93
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How many containers can run from one image at the same time?
- Only one
- At most two
- None; it must be copied first
- As many as wanted Correct answer
Explanation: The image is read-only; each container adds its own writable layer on top.
2 Give the container a name.
docker run -d ____ web1 nginx
docker run -d --name web1 nginx
Explanation: --name gives the container a readable name.
3 Match.
| Term | Meaning |
|---|---|
image |
Read-only template |
container |
Running instance |
writable layer |
Lost when the container is removed |
volume |
Persistent data |