Sub topic 5
Listing and inspecting containers
This chapter assumes you can start containers with docker run, including detached mode, and that you know the container states: created, running, exited. You also need a terminal with Docker installed.
Listing containers with docker ps
docker ps lists running containers. Start a container first so there is something to see:
docker run -d --name web -p 8080:80 nginx:1.27
docker psThe output has these columns:
| Column | Meaning |
|---|---|
| CONTAINER ID | Shortened unique ID |
| IMAGE | Image the container was created from |
| COMMAND | Process the container runs |
| CREATED | Time since creation |
| STATUS | State and uptime, such as `Up 2 minutes` |
| PORTS | Published ports, such as `0.0.0.0:8080->80/tcp` |
| NAMES | Container name |
Stopped containers are hidden by default. Add -a to include them:
docker ps -aA container that has exited shows a status like Exited (0) 5 minutes ago. The number in brackets is the exit code. 0 means the main process finished normally, and anything else usually means an error.
Filtering and formatting
On a busy host the full list is hard to read. --filter narrows it, -q prints only IDs, and --format takes a Go template that chooses the columns.
docker ps -a --filter status=exited
docker ps --filter name=web
docker ps -a --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
docker ps -aq --filter status=exitedThe last command prints only IDs, so you can pass the result to other commands. Use it with care, because docker rm $(docker ps -aq --filter status=exited) deletes every exited container.
Inspecting a container
docker inspect prints a JSON document with everything Docker knows about a container: configuration, state, mounts, and network settings.
docker inspect webThe full output is long, so pull out single fields with --format:
docker inspect --format '{{.State.Status}}' web
docker inspect --format '{{.State.ExitCode}}' web
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web
docker inspect --format '{{json .Config.Env}}' webThe range loop in the third command is needed because a container can be attached to several networks, each with its own IP address. The json function in the fourth prints a value as JSON, which is useful for lists and maps.
Quick checks without the full JSON
Two shorter commands cover common questions:
docker port web
docker stats --no-streamdocker port shows the published port mappings of one container. docker stats shows live CPU, memory, and network use. With --no-stream it prints one snapshot and exits instead of refreshing.
Common errors
- A missing container.
docker psshows only running containers. If a container crashed, look for it withdocker ps -a. - An empty IP address.
.NetworkSettings.IPAddressis empty for containers on user-defined networks. Read the address from.NetworkSettings.Networksas shown above. - Name conflicts. Container names must be unique, even among stopped containers. A leftover
webmakes a new--name webfail until you remove it. - Template errors. Quote the whole
--formatstring in single quotes so your shell does not interpret the braces.
Exercise
A container named job ran a script and exited. How do you list it, then print only its exit code?
Show answer
Run docker ps -a --filter name=job to list it, because docker ps alone hides exited containers. Then run docker inspect --format '{{.State.ExitCode}}' job to print the exit code.