Course outline · 0% complete

0/29 lessons0%

Course overview →

Images and containers

lesson 1-2 · ~12 min · 2/29

Images and containers

Docker's two core nouns. Every docker command you will ever type creates, runs, lists, or deletes one of these two things, so the distinction is worth locking in before touching the CLI. Mixing them up is also behind the most common beginner error messages: trying to run a container that is actually an image, or delete an image that is actually a container.

An image is a frozen, read-only package: a complete filesystem (OS files, Python, your libraries, your code) plus a bit of metadata like which command to start. Think of it as a snapshot of a machine that is known to work. Images are built once and never change.

A container is a running instance of an image. Docker takes the image, adds a thin writable layer on top, and starts your program inside it, isolated from the rest of the machine.

One image can start many containers, the same way one class can create many objects, or one git commit can be checked out into many working copies.

imageread-onlyOS + deps + codebuilt oncecontainer 1 · running · + writable layercontainer 2 · running · + writable layercontainer 3 · stopped · + writable layerone imagemany independent containers, each started from the same image
One read-only image can start many containers. Each container is the image plus its own thin writable layer and its own running process.

The commands that map to the nouns

You will meet these properly in unit 2, but here is the shape:

docker pull nginx        # download an image
docker run nginx         # create + start a container from it
docker ps                # list running containers
docker images            # list images on this machine

Two facts to lock in:

  1. docker run never modifies the image. Each container writes into its own private top layer.
  2. Containers are disposable. Delete one and the image is untouched, so you can start a fresh identical one in about a second.

That disposability is the workflow: broken container? Do not repair it. Kill it and start a new one from the known-good image.

One image, three containers

A concrete example you will meet again when we deploy in unit 8:

docker run -d --name web1 -p 8081:80 nginx
docker run -d --name web2 -p 8082:80 nginx
docker run -d --name web3 -p 8083:80 nginx

Three identical web servers from one download. Each container has its own name, its own writable layer, and its own published port, but the read-only nginx image exists once on disk. This is how real services scale out: not by installing the software three times, but by starting three instances of one image. (The -d, --name, and -p flags get full explanations in the next unit.)

Quiz

You run `docker run nginx` twice. What do you have now?

Problem

You delete a container with `docker rm`. Is the image it was created from deleted too? Answer yes or no.