15 min read
Docker image vs container, explained
In one sentence: An image is a read-only template: a program's files and the command to start it. A container is a running copy of that image, with its own layer to write to: what you change inside stays in it and is lost when you delete it, while the image stays untouched.
What you will learn
- Tell an image from a container, and know what survives docker stop and what is lost with docker rm.
- Start, view, read, enter, stop and delete containers with docker run, ps, logs, exec, stop and rm.
- Explain that a container is a process, with its PID 1, and which signals docker stop sends.
- Download images from Docker Hub with docker pull and list them with docker image ls.
Before you start, read: 00-introduction.md
The problem
Section titled “The problem”In lesson 6 of Phase 1 you installed nginx on your virtual machine: create it, apt install, systemctl… With Docker, the same nginx starts on your laptop with a single command, without installing it, and answers on localhost:8080.
Picture Ana doing it. She goes into the container and changes the home page to “Ana and Luis’s Shop”. The next day she deletes it with docker rm and starts it again with the same command. The home page says “Welcome to nginx!” again: her change is gone.
Where was that change, and why wasn’t it in what Docker downloaded? The answer is two words that get used all the time, and often mixed up: image and container.
The analogy
Section titled “The analogy”The analogy
Think of a gym’s sign-up form. The office keeps the original, and nobody writes on it. Everyone who comes in gets a photocopy and fills it in their own way.
Ana writes her name on hers; Luis’s is still blank, and so is the original. If Ana throws away her photocopy, what she wrote goes with it. To start from scratch, she asks for another copy of the original, a clean one.
The image is the original; each container, a photocopy in use.
Where the analogy breaks down
- Docker doesn’t photocopy the whole image: all the containers read the same files, and each one keeps only what it changes, separately. That’s why one starts in a second, and ten don’t take up ten times the space.
- A photocopy is still paper. A container also has a running program.
- Stopping a container isn’t throwing away the photocopy: with
docker stopit’s kept, with whatever was written. Onlydocker rmthrows it away.
How it really works
Section titled “How it really works”The image is the template; the container, the running copy
Section titled “The image is the template; the container, the running copy”This is how nginx reaches your computer and starts running:
- Docker Huban image registry
docker pull
- The nginx:1.31 imageon your disk, read-only
docker run
- The web containera running copy
An Docker imageA read-only template with the files a program needs (the program itself, its libraries and a minimal system) and the command to start it. You can start as many containers as you want from one image, and none of them changes it.Go to definition is a package with two things:
- Files: nginx itself, the libraries it uses and a minimal Debian with its usual folders (
/etc,/usr,/bin). Everything nginx needs, except the operating system’s kernel, which it shares with your machine, as you saw in the introduction. - Instructions: which command to run when it starts (
nginx -g 'daemon off;'), which port it listens on (80) and a few more details.
It’s read-only: nothing that happens afterwards changes it. It’s named with a name and a tag, separated by a colon: nginx:1.31 is version 1.31 of nginx.
A ContainerA program that runs isolated and carries inside everything it needs, with its own files, its own network and its own processes. It isn’t a virtual machine, because it doesn’t start a whole system; it shares the Linux kernel of the machine it runs on. That’s why it starts in less than a second.Go to definition is what comes out of docker run: the image’s files, its own writable layer on top and a running program. When something writes a file inside, nginx or you, the change goes to that layer. The image isn’t touched, and the other containers from the same image see nothing. How those layers stack up you’ll see in lesson 2.
| Image | Container | |
|---|---|---|
| What it is | A template: files and instructions | A running copy, with its writable layer |
| Can you write to it? | No | Yes, in its layer |
| You get it with | docker pull |
docker run |
| You list them with | docker image ls |
docker ps |
| You delete them with | docker image rm |
docker rm |
Images come from a registry
Section titled “Images come from a registry”Images are kept in a Image registryA service that stores Docker images so they can be downloaded with docker pull and published with docker push. The best known is Docker Hub, the one Docker uses unless you tell it another.Go to definition, a service you download them from, like an app store. The one Docker uses if you don’t name another is Docker Hub (hub.docker.com). Lesson 8 explains them in depth.
Download the nginx image. You need Docker running, as in the introduction:
Download the nginx image
docker pull nginx:1.31What you will see:
1.31: Pulling from library/nginxb496ef725cba: Pull complete95266651b1b5: Pull complete74db6078a267: Pull completea0eaca4f4a89: Pull complete848dbae53a0f: Pull completec6d8c112974b: Pull complete58f461baacaf: Download complete10ef391a6a66: Download completeDigest: sha256:f9ea18bfa4fad859e1ed38259d711da7ccad2c3516e875cec3351a57c859f571Status: Downloaded newer image for nginx:1.31docker.io/library/nginx:1.31Each line with a code is a part of the image, almost all of them layers (lesson 2). While it downloads, you’ll see progress bars.
The last line is the full name: docker.io is Docker Hub; library, the official images; and nginx:1.31, the name and the tag. Without a tag, Docker uses latest, which isn’t always the latest version (lesson 8).
If nginx has published a fix since then, your codes will be different. If you see Cannot connect to the Docker daemon, Docker isn’t running: start it as in the introduction.
If you don’t download it first, docker run does it for you, as you saw with hello-world.
docker run, word by word
Section titled “docker run, word by word”This command creates an nginx container and starts it:
docker run -d --name web -p 127.0.0.1:8080:80 nginx:1.31Word by word:
docker run: creates a container from an image and starts it.-d(detached): leaves it in the background, like the&from lesson 5 of Phase 1, and gives you back the prompt. Without-d, the terminal keeps showing what nginx writes, and Ctrl + C stops it.--name web: gives it a name. Without it, Docker makes one up at random.-p 127.0.0.1:8080:80(publish): joins your computer’s PortA number, from 0 to 65535, that says which program on a computer a connection is for. The computer has an address; each listening program has its port.Go to definition 8080 to the container’s port 80, where nginx listens. First the outside one, then the inside one. The127.0.0.1:makes it answer only your own computer: lesson 6 explains why.nginx:1.31: the image. Whatever you write after it replaces the start command the image comes with.
Three more options you’ll use often:
--rm: deletes the container as soon as it ends. Without it, every test leaves a stopped container behind that takes up its name and its space.-e NAME=value(from environment): creates an environment variable inside. That’s how almost every program is configured in Docker: PostgreSQL’s password or your API’sDATABASE_URL.-it:-i(interactive) lets you type to it, and-t(from TTY) gives it a terminal. You need them to use a shell inside.
A variable, and the container deletes itself
docker run --rm -e SHOP="Ana and Luis" nginx:1.31 printenv SHOPWhat you will see:
Ana and Luisprintenv SHOP replaces nginx: it prints the variable and ends. When the program ends, the container ends, and --rm deletes it.
Seeing, reading and getting in: ps, logs and exec
Section titled “Seeing, reading and getting in: ps, logs and exec”With the container running, three commands tell you what’s going on inside:
docker ps: the running containers. With-a(all), the stopped ones too.docker logs web: what the program has written to its output, the normal one and the error one (lesson 3 of Phase 1). With-f(follow) it stays waiting for new lines, likejournalctl -f. Ctrl + C stops watching, and the container keeps going.docker exec -it web sh: starts another program inside a container that’s already running, heresh, a ShellThe program that reads what you type in the terminal, interprets it and runs it. The most used are bash, the one on almost every Linux, and zsh, the one on macOS.Go to definition.exitleaves it, and the container keeps going.
Stopping, starting and deleting
Section titled “Stopping, starting and deleting”| Command | What it does | What happens to the container’s layer |
|---|---|---|
docker stop web |
Stops the program | It stays, with your changes |
docker start web |
Starts the same container again | It still has the earlier changes |
docker rm web |
Deletes the container | It disappears with it. If it’s running, it refuses; with -f (force), it stops it and deletes it |
docker image ls |
Lists the images (docker images is its old name) |
Nothing: images don’t change |
docker image rm nginx:1.31 |
Deletes an image | It refuses if any container uses it, even a stopped one |
That’s the answer to Ana’s problem: what you change inside a container lives in its layer. It survives stop and start, and goes away with rm. To keep something, either you put it in the image (lesson 3 teaches you to build them) or you keep it outside, in a volume (lesson 5).
A container is a process
Section titled “A container is a process”As you saw in the introduction, a container is a Linux ProcessA running program. When you start a server, the operating system creates a process that stays alive waiting for requests.Go to definition, isolated from the others. Inside, the image’s program has PIDPID stands for Process Identifier.The number the operating system uses to identify each process while it’s running. It lets you address a specific process, for example to stop it.Go to definition 1, the number of the first process, the one that was systemd on your virtual machine.
From outside, docker top shows a container’s processes with their real number:
nginx's processes, seen from outside
docker top webWhat you will see:
UID PID PPID C STIME TTY TIME CMDroot 4163230 4163207 0 01:35 ? 00:00:00 nginx: master process nginx -g daemon off;message+ 4163285 4163230 0 01:35 ? 00:00:00 nginx: worker processmessage+ 4163286 4163230 0 01:35 ? 00:00:00 nginx: worker processmessage+ 4163287 4163230 0 01:35 ? 00:00:00 nginx: worker processmessage+ 4163288 4163230 0 01:35 ? 00:00:00 nginx: worker processnginx’s main process, the master, is 1 inside and 4163230 outside. The workers, which handle the requests, are its children: their PPID, the parent, is 4163230. Your numbers will be different, and how many workers there are depends on your processors.
message+ and not nginx? To Linux, a user is a number (lesson 4 of Phase 1), and outside that number has another name, or none. On macOS and Windows, “outside” is Docker Desktop’s virtual machine: ps in your terminal doesn’t see them.
docker stop: first a signal, then SIGKILL
Section titled “docker stop: first a signal, then SIGKILL”docker stop does what you learned in lesson 5 of Phase 1: first kill and, if needed, kill -9. It sends a stop SignalA short notice the operating system delivers to a process, like “interrupt yourself” or “finish”. Each signal is a number with a name, like SIGTERM, and pressing Ctrl+C in the terminal sends one.Go to definition to PID 1, waits ten seconds and, if the process is still alive, sends it SIGKILL.
The stop signal is SIGTERM, unless the image asks for another one. nginx’s asks for SIGQUIT, which to nginx means “finish the requests you have halfway done and exit”.
PID 1 has a special rule in Linux: it ignores the signals it hasn’t prepared for, SIGTERM included, because if it goes down, the whole container goes down. A program that doesn’t handle them takes ten seconds to stop:
A container that doesn't handle the signal
docker run -d --name sleepy nginx:1.31 sleep 1000time docker stop sleepydocker ps -aWhat you will see:
9bcad290747d43f8a577c63fbdc3563f515b203c90dd47ed5dac988a41995e11sleepydocker stop sleepy 0.01s user 0.01s system 0% cpu 10.118 totalCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9bcad290747d nginx:1.31 "/docker-entrypoint.…" 10 seconds ago Exited (137) Less than a second ago sleepysleep 1000 replaces nginx: it waits a thousand seconds, and it’s PID 1. time measures how long a command takes: ten seconds, because sleep doesn’t handle the signal and Docker ended up sending SIGKILL. In bash, time prints real 0m10.118s.
Exited (137) is the exit code: 128 plus 9, the number of SIGKILL, as in Phase 1. docker ps -a also shows your other containers. Delete this one with docker rm sleepy.
In lesson 3 you’ll see how to start your API so that it stops right away.
Try it
Section titled “Try it”The goal: run the nginx from Phase 2 in a container, change its home page and find out what survives docker stop and what survives docker rm. You need the nginx:1.31 image from the earlier step.
The outputs are from Docker Engine 29 on a Mac, inside a virtual machine with Linux, like Docker Desktop’s. We captured them with another port and other names, replaced with the lesson’s: nothing else changes. Your ids, times and durations will be different. On Linux and WSL, the outputs are the same.
1. Start nginx in a container
docker run -d --name web -p 127.0.0.1:8080:80 nginx:1.31What you will see:
21b142f2b696638731a72cbf7b3e9a860c2d9d9c11dc2e15d0d48060ff7ba162It’s the container’s id, its full number. If you see port is already allocated, another program is already using 8080: see “Common mistakes.”
2. Look at it and ask it
docker pscurl -sI localhost:8080curl -s localhost:8080 | grep h1What you will see:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES21b142f2b696 nginx:1.31 "/docker-entrypoint.…" 11 seconds ago Up 11 seconds 127.0.0.1:8080->80/tcp webHTTP/1.1 200 OKServer: nginx/1.31.6Date: Wed, 14 Oct 2026 05:13:49 GMTContent-Type: text/htmlContent-Length: 896Last-Modified: Tue, 15 Sep 2026 12:54:15 GMTConnection: keep-aliveETag: "6aa93ff7-380"Accept-Ranges: bytes
<h1>Welcome to nginx!</h1>In docker ps, look at STATUS, Up (running), and at PORTS, your computer’s 8080 joined to the container’s 80. The id is shown shortened, and docker accepts both.
The nginx from Phase 2 answers, in another version: nginx/1.31.6, instead of Ubuntu’s 1.24.0.
3. Read its logs
docker logs webWhat you will see:
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d//docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh/docker-entrypoint.sh: Configuration complete; ready for start up2026/10/14 05:13:38 [notice] 1#1: using the "epoll" event method2026/10/14 05:13:38 [notice] 1#1: nginx/1.31.62026/10/14 05:13:38 [notice] 1#1: built by gcc 14.2.0 (Debian 14.2.0-19)2026/10/14 05:13:38 [notice] 1#1: OS: Linux 6.8.0-117-generic2026/10/14 05:13:38 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1024:5242882026/10/14 05:13:38 [notice] 1#1: start worker processes2026/10/14 05:13:38 [notice] 1#1: start worker process 292026/10/14 05:13:38 [notice] 1#1: start worker process 302026/10/14 05:13:38 [notice] 1#1: start worker process 312026/10/14 05:13:38 [notice] 1#1: start worker process 32172.17.0.1 - - [14/Oct/2026:05:13:49 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/8.7.1" "-"172.17.0.1 - - [14/Oct/2026:05:13:49 +0000] "GET / HTTP/1.1" 200 896 "-" "curl/8.7.1" "-"Three parts:
/docker-entrypoint.sh: a script in the image that prepares the configuration before starting nginx.[notice]: nginx starting up.1#1is PID 1.OS: Linux: even if you’re on a Mac, nginx runs on Linux.- Your two requests,
HEADandGET, in the format of Phase 1’saccess.log. They come from172.17.0.1, an address on Docker’s network (lesson 6).
To see them live: docker logs -f web in another terminal, and repeat the curl.
4. Go in and change the home page
docker exec -it web shWhat you will see:
# whoamiroot# cat /proc/1/cmdline; echonginx: master process nginx -g daemon off;# echo "<h1>Ana and Luis's Shop</h1>" > /usr/share/nginx/html/index.html# exitYou’re now inside, with the # prompt. Type the commands one by one:
whoami: inside the container you’reroot. Lesson 4 changes that for your API.cat /proc/1/cmdline: Linux keeps each process’s command in/proc/<PID>/cmdline, and 1 is nginx’s master. Theechoonly adds the line break it’s missing.echo "…" > …/index.html: replaces nginx’s home page with a line of your own.exittakes you back to your terminal. The container keeps running.
5. Check the change
curl -s localhost:8080What you will see:
<h1>Ana and Luis's Shop</h1>nginx serves the new home page. The change is in the container’s layer: the nginx:1.31 image knows nothing about it.
6. Stop it and start it again
docker stop webdocker ps -adocker start webcurl -s localhost:8080What you will see:
webCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES21b142f2b696 nginx:1.31 "/docker-entrypoint.…" 43 seconds ago Exited (0) 12 seconds ago webweb<h1>Ana and Luis's Shop</h1>Stopped, docker ps doesn’t show it, but docker ps -a does: Exited (0), it ended fine, with code 0. With no program running, PORTS is empty. When you start it, the home page is still yours: stop and start keep the layer.
7. Which signal did nginx get?
docker logs web 2>&1 | grep SIGQUITWhat you will see:
2026/10/14 05:14:08 [notice] 1#1: signal 3 (SIGQUIT) received, shutting downThe one the nginx image asks for, SIGQUIT (number 3), to PID 1. The 2>&1 is needed because nginx writes these notices to the error output.
8. Delete it and start over
docker rm webWhat you will see:
Error response from daemon: cannot remove container "web": container is running: stop the container before removing or force removeDocker doesn’t delete a running container: “stop it first, or force the removal”.
9. Force the removal and start another one
docker rm -f webdocker run -d --name web -p 127.0.0.1:8080:80 nginx:1.31curl -s localhost:8080 | grep h1What you will see:
web68e450280dad4884e2f17864ecbdff5fc44a36d8ace1d20a88430e7baa8bdefe<h1>Welcome to nginx!</h1>This is what happened to Ana. The new container, with another id, is born from the image, which never changed: your home page went away with the previous one’s layer.
And now, the challenge. This time you write the commands:
Challenge: with web running, start a second nginx, web2, on port 8081. Change the home page only in web and check that web2 still has nginx's. How many times has the image been downloaded?Show answer
docker run -d --name web2 -p 127.0.0.1:8081:80 nginx:1.31docker exec -it web shInside web, the same echo from step 4, and exit. Then, curl -s localhost:8080 shows your home page, and curl -s localhost:8081 | grep h1, <h1>Welcome to nginx!</h1>.
The image was downloaded only once. Both containers read its same files, and each one writes to its own layer: a change in one doesn’t reach the other.
To finish, clean up:
10. Delete the containers; the image stays
docker rm -f web web2docker image ls nginxWhat you will see:
webweb2IMAGE ID DISK USAGE CONTENT SIZE EXTRAnginx:1.31 f9ea18bfa4fa 261MB 65MBDeleting containers doesn’t delete the image. CONTENT SIZE, 65 MB, is what was downloaded, compressed; DISK USAGE, what it takes up on your disk. In EXTRA, a U (in use) marks the images some container uses. If you want the space, docker image rm nginx:1.31: if you need it again, Docker downloads it again.
You have already seen it
Section titled “You have already seen it”- The virtual machine from Phase 1.
multipass launch 24.04downloaded an Ubuntu image and created a machine from it. With the same image you could create ten. - nginx with systemd.
systemctl start,stopandstatus, andjournalctl -u nginx, did the same asdocker start,stop,psandlogs, for a program installed on the system. psandkill.docker psanddocker stopare their relatives: a container is a process.- This course. To test the guided projects, GitHub starts PostgreSQL in a
postgres:18container, and throws it away when it’s done.
Common mistakes
Section titled “Common mistakes”- Keeping something important inside a container.
docker rmtakes it away. Only what can be lost goes inside; the rest, in the image (lesson 3) or in a volume (lesson 5). - The name is already in use.
Conflict. The container name "/web" is already in use by container "68e45028…": a stopped container also takes up its name. Find it withdocker ps -aand delete it, or use another name. - The port is already taken.
Bind for 127.0.0.1:8080 failed: port is already allocated: another container, or another program, is using that port. Change only the outside one,-p 127.0.0.1:8081:80: nginx stays on 80 inside. And watch out: Docker leaves behind the container it couldn’t start (Createdindocker ps -a); delete it before trying again. - Looking for tools that aren’t there.
docker exec web psanswersexec: "ps": executable file not found in $PATH. An image brings what its program needs and little more: nopsand novim. To see processes,docker top. - Dropping the
127.0.0.1:. With-p 8080:80, nginx answers anyone on your network. Lesson 6 explains it.
Summary
Section titled “Summary”- An image is a read-only template: files and instructions to start. It’s downloaded from a registry, like Docker Hub, with
docker pull. - A container is a running copy of an image, with its own writable layer. From one image you can get as many as you want, and none of them changes it.
docker runcreates and starts;ps,logsandexeclook inside;stop,startandrmstop, start and delete.- What you change inside survives
stopandstart, and is lost withrm. - A container is a process: its program is PID 1, and
docker stopsends it its stop signal and, after ten seconds,SIGKILL.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- What is an image? and What is a container?, from Docker’s documentation.
- The references for
docker runanddocker stop: all their options. - The official nginx image on Docker Hub: its tags and how to serve your own pages.