Listing Docker Containers with docker ps and docker ps -a
📖 Scenario: You are managing Docker containers on your local machine. You want to see which containers are currently running and also check all containers including those that have stopped.
🎯 Goal: Learn how to list running Docker containers using docker ps and list all containers including stopped ones using docker ps -a.
📋 What You'll Learn
Use the docker ps command to list running containers
Use the docker ps -a command to list all containers including stopped ones
Understand the difference between the two commands
💡 Why This Matters
🌍 Real World
Docker containers are used to run applications in isolated environments. Knowing how to list running and stopped containers helps you manage and troubleshoot your applications.
💼 Career
DevOps engineers and system administrators frequently use these commands to monitor and manage containerized applications in development and production.
Progress0 / 4 steps
1
Create a running Docker container
Run a Docker container in detached mode using the nginx image with the container name webserver. Use the command docker run -d --name webserver nginx.
Docker
Hint
Use docker run with -d to run the container in the background and --name to assign the container a name.
2
List running containers with docker ps
Use the command docker ps to list all currently running containers.
Docker
Hint
The docker ps command shows only containers that are running.
3
Stop the running container
Stop the container named webserver using the command docker stop webserver.
Docker
Hint
Use docker stop followed by the container name to stop it.
4
List all containers including stopped with docker ps -a
Use the command docker ps -a to list all containers including those that are stopped.
Docker
Hint
The -a option with docker ps shows all containers, running or stopped.
Practice
(1/5)
1. What does the command docker ps show by default?
easy
A. Only running containers
B. All containers, running and stopped
C. Only stopped containers
D. Docker images available locally
Solution
Step 1: Understand the default behavior of docker ps
The docker ps command lists containers that are currently running.
Step 2: Compare with other options
To see all containers, including stopped ones, you use docker ps -a. The command does not list images.
Final Answer:
Only running containers -> Option A
Quick Check:
docker ps = running containers [OK]
Hint: Remember: docker ps shows running containers only [OK]
Common Mistakes:
Thinking docker ps shows all containers
Confusing containers with images
Assuming stopped containers show without -a
2. Which of the following commands correctly lists all containers, including stopped ones?
easy
A. docker ps --stopped
B. docker ps
C. docker ps --running
D. docker ps -a
Solution
Step 1: Identify the flag for listing all containers
The -a flag with docker ps lists all containers, running or stopped.
Step 2: Check other options
docker ps alone shows only running containers. The options --running and --stopped are invalid flags.
Final Answer:
docker ps -a -> Option D
Quick Check:
docker ps -a = all containers [OK]
Hint: Use -a to list all containers, not just running [OK]
Common Mistakes:
Using docker ps without -a to see stopped containers
Trying invalid flags like --running
Confusing container listing with image listing
3. Given the following output from docker ps -a:
CONTAINER ID IMAGE STATUS NAMES
abc123 nginx Up 2 hours webserver
def456 redis Exited (0) 3 days ago cache
What will docker ps show?
medium
A. Both containers: webserver and cache
B. Only the cache container
C. Only the webserver container
D. No containers
Solution
Step 1: Analyze the status of containers
The container named webserver is "Up 2 hours" meaning it is running. The container named cache is "Exited", meaning it is stopped.
Step 2: Understand docker ps output
docker ps shows only running containers, so it will list only webserver.
Final Answer:
Only the webserver container -> Option C
Quick Check:
Running containers = webserver only [OK]
Hint: Check STATUS: 'Up' means running, 'Exited' means stopped [OK]
Common Mistakes:
Listing stopped containers with docker ps
Assuming all containers show without -a
Confusing container names with images
4. You run docker ps -a but get an error: unknown flag: --all. What is the likely cause?
medium
A. You typed --all instead of -a
B. Docker daemon is not running
C. You need to run the command as root
D. The container list is empty
Solution
Step 1: Check the flag syntax
The correct flag to list all containers is -a, not --all. The error shows the flag is unknown.
Step 2: Rule out other causes
If the daemon was not running, the error would be different. Running as root is not required for listing containers. An empty list does not cause a flag error.
Final Answer:
You typed --all instead of -a -> Option A
Quick Check:
Use -a flag, not --all [OK]
Hint: Use short flag -a, not --all [OK]
Common Mistakes:
Using invalid flag --all
Assuming root is needed for listing
Ignoring daemon status errors
5. You want to list all containers but only see their IDs and names, not other details. Which command achieves this?
hard
A. docker ps -a --short
B. docker ps -a --format "{{.ID}} {{.Names}}"
C. docker ps -a --quiet
D. docker ps --filter id --filter name
Solution
Step 1: Understand formatting options
The --format flag lets you customize output. Using {{.ID}} {{.Names}} shows only container IDs and names.
Step 2: Evaluate other options
--quiet shows only IDs, not names. --filter is for filtering, not formatting. --short is not a valid flag.
Final Answer:
docker ps -a --format "{{.ID}} {{.Names}}" -> Option B
Quick Check:
Use --format to customize output [OK]
Hint: Use --format to show only needed fields [OK]
Common Mistakes:
Using invalid flags like --short
Confusing --quiet with showing names
Trying to filter instead of format output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.