docker-listar-parar-borrar-contenedores

Listing, Stopping and Deleting Containers

  • 4 min

We already know how to create containers. If you’ve been following the previous articles, you’ve probably launched a few hello-world’s, some nginx, and maybe an ubuntu.

And now you’re wondering: Where are they? Are they still running? How do I get rid of them?

Docker never deletes anything unless you tell it to. A container that has finished its task doesn’t disappear; it remains in a “Stopped” (Exited) state, taking up disk space and reserving its name.

In this article, we are going to learn how to manage the chaos and keep our environment clean.

CommandAction
docker psLists running containers.
docker ps -aLists ALL (running and stopped).
docker stop <id>Stops a container gracefully.
docker rm <id>Removes a stopped container (deletes data).
docker run --rmCreates a container that auto-deletes upon finishing.
docker container pruneDeletes all stopped containers on the system.

Listing Containers (docker ps)

The command to see what’s happening is docker ps (Process Status).

If you simply execute:

docker ps
Copied!

You will see only the containers that are currently running (Running).

CONTAINER ID   IMAGE     STATUS          PORTS                NAMES
a1b2c3d4e5     nginx     Up 5 minutes    0.0.0.0:80->80/tcp   mi-web
Copied!

To see all containers, running or not, we use the -a (All) flag:

docker ps -a
Copied!

Now you’ll see a much longer list:

CONTAINER ID   IMAGE         STATUS                     NAMES
a1b2c3d4e5     nginx         Up 5 minutes               mi-web
f9e8d7c6b5     hello-world   Exited (0) 2 hours ago     nervous_bhabha
Copied!

See that Exited? That’s a stopped container. It takes up little space, but its writable layer is still on your hard drive.

Stopping Containers (stop vs kill)

To stop a running container, we have two ways: the “graceful” one and the “brute force” one.

The Correct Way: docker stop

docker stop mi-web
Copied!

This sends a SIGTERM signal to the main process. It tells the application: “Please, finish what you’re doing, save the data, and shut down.” Docker gives it a 10-second grace period. If it doesn’t close, it kills it.

The Brute Force Way: docker kill

docker kill mi-web
Copied!

This sends a SIGKILL. It’s like pulling the power cord. The process dies instantly without saving anything.

Only use this if the container has hung and is not responding to stop.

Deleting Containers docker rm

Once a container is stopped (Exited), it still exists. To permanently delete it and free up the space taken by its writable layer, we use rm:

docker rm mi-web
Copied!

If you try to delete a container that is running (Up), Docker will give you an error:

Error response from daemon: You cannot remove a running container…

You must first stop it, and then rm it. Or use the brute force flag docker rm -f mi-web (not generally recommended).

General Cleanup (prune)

If it’s already late and you have 50 stopped containers that you can’t be bothered to delete one by one, Docker has the cleanup command:

docker container prune
Copied!

This will wipe out all containers that are in the Exited state in one go. It will ask for confirmation, report back the freed space, and you’ll be left with a wonderful feeling of inner peace (or not, if you deleted something you didn’t mean to 😉).