List all running containers
In this step, you will learn how to list all currently running Docker containers. This is a fundamental command for managing your containers and understanding what is active on your system.
First, let's pull a simple image that we can use to run a container. We will use the hello-world
image, which is a very small image designed to test your Docker installation.
docker pull hello-world
You should see output indicating that the image is being pulled and extracted.
Now, let's run a container using this image. The docker run
command creates and starts a new container.
docker run hello-world
This command will run the hello-world
container. The container will print a message and then exit. Even though it exits, it is still considered a container that exists on your system, just not running.
To see only the containers that are currently running, you use the docker ps
command.
docker ps
Since the hello-world
container exited immediately after running, the docker ps
command will likely show no output under the "CONTAINER ID", "IMAGE", "COMMAND", etc. columns, because there are no containers currently running.
Let's run a container that stays running. We will use the ubuntu
image and run a simple command that keeps the container alive. First, pull the ubuntu
image.
docker pull ubuntu
Now, run an Ubuntu container that stays running by executing the sleep infinity
command. The -d
flag runs the container in detached mode (in the background).
docker run -d ubuntu sleep infinity
You will see a long string of characters, which is the container ID. This indicates that the container has been started in the background.
Now, run docker ps
again to see the running container.
docker ps
This time, you should see output listing the Ubuntu container you just started. The output includes information like the container ID, the image used, the command being run, when it was created, its status, ports, and a randomly generated name.
The docker ps
command is essential for quickly checking which containers are active and consuming resources on your system.