Skip to content
Theory · Your first container · lesson 2 of 7

A container's life: ps, stop, rm, logs

A cookie cut with the cutter can be put aside for later, or thrown away. A container works the same way: it lives from creation to removal, and each step has its own command.

A container is usually in one of three states:

  • created (Created) — the container exists, but its program has not run yet;
  • running (Up) — the program inside is running;
  • stopped (Exited) — the program has ended, while the container and its data stay on disk.

The last step is removal: after it the container is gone. Let's go through the commands in order on the web container we started in the previous lesson.

Listing containers: docker ps

Shell
docker ps
TEXT
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
9298410138c0   nginx:1.27.3   "/docker-entrypoint.…"   9 seconds ago   Up 8 seconds   80/tcp    web

What the columns mean:

  • CONTAINER ID — the first 12 characters of the id; commands accept either it or the name;
  • IMAGE — the image the container was created from;
  • COMMAND — the program it started;
  • CREATED — when the container was created;
  • STATUS — the state: Up 8 seconds means "running for 8 seconds";
  • PORTS — the container's ports: those the image declares and those published on the host (publishing comes two lessons later);
  • NAMES — the container's name.

The program's output: docker logs

A container in the background does not write to your terminal. Docker keeps everything its program prints, and the docker logs command shows it. nginx writes some of its messages to the error stream, so it is merged with the normal output before head:

Shell
docker logs web 2>&1 | head -n 9
TEXT
/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.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-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 up

The last line, Configuration complete; ready for start up, means nginx is configured and ready for requests. When a container behaves oddly, docker logs is the first place to look.

Stopping: docker stop

Shell
docker stop web
docker ps
docker ps -a
TEXT
web
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                              PORTS     NAMES
9298410138c0   nginx:1.27.3   "/docker-entrypoint.…"   10 seconds ago   Exited (0) Less than a second ago             web

Let's go step by step:

  1. docker stop web asks the program to exit and waits up to 10 seconds; if it has not finished by then, Docker kills it. The command prints the container's name.
  2. docker ps prints an empty table: it shows running containers only.
  3. docker ps -a shows stopped ones too. Exited (0) means the program ended with code 0, that is, without an error.

The stopped container has not gone anywhere. Its writable layer with the changed files, its settings (name, image, run flags) and its logs are still on disk. That is why docker start web starts the same container with the same id, and docker logs web still works after the stop.

This is where the cookie picture stops working. A cookie put aside is still the same cookie, while a stopped container has no running program, only its saved state on disk.

Removing: docker rm

The docker rm web command removes the container. Its writable layer and logs go with it, while the image stays, and a new container can be created from it.

docker rm refuses to remove a running container: it has to be stopped first. docker rm -f web stops and removes it in one command. And the --rm flag of docker run removes the container by itself as soon as its program ends — handy for one-off runs.

A stopped container still shows in docker ps -a and takes disk space because docker stop ends only the program, while the container itself lives until docker rm.

Common mistake

The container was stopped, and the same command is run again:

Shell
docker run -d --name web nginx:1.27.3
TEXT
docker: Error response from daemon: Conflict. The container name "/web" is already in use by container "a84a9a8fd8fabcd9b0b2e398bb239b70150d32b7b36a94a11fb7d1a97507c322". You have to remove (or rename) that container to be able to reuse that name.

The stopped web container still exists, and its name is taken. There are two ways out: start it again with docker start web, or remove it with docker rm web and create a new one.

Key points

  • docker ps shows running containers, docker ps -a the stopped ones too.
  • docker logs shows the output of the container's program, after a stop as well.
  • docker stop ends the program, while the container with its writable layer, settings and logs stays on disk.
  • docker rm removes a container, docker rm -f a running one, and --rm removes it by itself once it ends.