Managing Containers

As you start using Docker, you will create many containers over time. It is important to know which containers are running, which are stopped, and how to manage them.

Listing Running Containers

To see the containers currently running on your system, use this command:

docker container ls

The output will look something like this:

CONTAINER ID   IMAGE           COMMAND              CREATED        STATUS          PORTS         NAMES
b8f1e0b80096   pgadmin4        "/entrypoint.sh"     3 months ago   Up 15 mins     80/tcp        pgadmin4
a456acd23d64   postgres        "docker-entrypoint"  3 months ago   Up 15 mins     5432/tcp      postgres
f19bec238414   mongo           "docker-entrypoint"  8 months ago   Up 15 mins     27017/tcp     mongodb

Here is what each column means:

Column Description
Container ID A unique ID for the container.
Image The image the container was created from.
Command The command that runs inside the container.
Created When the container was created.
Status Current state: created, running, exited, or paused.
Ports Container ports mapped to your computer.
Names The container’s name. You can assign a custom name or let Docker create one automatically.

If you want to see all containers, including stopped ones, add the -a parameter:

docker container ls -a

This shows containers in any state: running, exited, or paused.

If you only want the container IDs, use the -q parameter:

docker container ls -q

This is useful for commands that work with multiple containers. For example, to remove all containers at once:

docker container rm -f $(docker container ls -a -q)

You can always check the help for the ls command:

docker container ls -h

Stopping and Starting Containers

To run a container in the background (detached mode):

docker container run -d --name trivia fundamentalsofdocker/trivia:ed2

To stop it:

docker container stop trivia  # or use the container ID

Stopping may take a few seconds. Here’s why:

Stopped containers still occupy space. To remove them:

docker container rm <container ID>
docker container rm <container name>

If a container is still running and you want to remove it, add the force parameter:

docker container rm -f <container name or ID>

This ensures the container is removed no matter its state.

You can find the container ID for more advanced commands:

export CONTAINER_ID=$(docker container ls -a | grep trivia | awk '{print $1}')

For example:

docker container stop $CONTAINER_ID
docker container rm $CONTAINER_ID

This stops and removes the container without typing the long ID manually.