Learning Docker isn't just about running containers—it's about becoming fluent in the commands you'll use every day in real-world DevOps environments.
These are the Docker commands you'll use repeatedly throughout your DevOps career for troubleshooting, debugging, monitoring, and managing containers. Think of this as the Docker equivalent of learning Linux commands like ps, top, grep, and tail.
Let's dive in.
Why Docker Command Fluency Matters
Anyone can copy and paste a docker run command from documentation.
The real skill begins when:
- A container suddenly stops working
- An application crashes after deployment
- You need to inspect logs
- You need to troubleshoot inside a running container
- Multiple containers are running simultaneously
In these situations, command-line fluency becomes incredibly valuable.
The commands in this article form the foundation of everyday Docker operations.
Viewing Running Containers with docker ps
The first command every Docker user should know is:
docker ps
This shows all currently running containers.
Common Variations
docker ps
docker ps -a
docker ps -q
docker ps --filter "status=exited"
What They Do
| Command | Purpose |
|---|---|
docker ps |
Show running containers |
docker ps -a |
Show all containers, including stopped ones |
docker ps -q |
Show only container IDs |
docker ps --filter "status=exited" |
Show only stopped containers |
Understanding Docker PS Output
Example:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a1b2c3d4e5f6 nginx "/docker-entrypoint…" 5 minutes ago Up 5 minutes 0.0.0.0:8080->80/tcp my-nginx
Here's what each column means:
| Column | Description |
|---|---|
| CONTAINER ID | Unique identifier for the container |
| IMAGE | Image used to create the container |
| COMMAND | Process running inside the container |
| STATUS | Current container state |
| PORTS | Port mappings between host and container |
| NAMES | Human-friendly container name |
Why docker ps -a Is Important
Suppose a container crashes.
Running:
docker ps
won't show it because it's no longer running.
Instead use:
docker ps -a
to find stopped containers and begin troubleshooting.
This is often the first step when debugging container issues.
Viewing Local Images with docker images
Containers run from images.
To see which images exist on your machine:
docker images
Example output:
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest a1b2c3d4e5f6 2 weeks ago 142MB
redis latest f6e5d4c3b2a1 3 weeks ago 117MB
Understanding the Columns
| Column | Description |
|---|---|
| REPOSITORY | Image name |
| TAG | Version label |
| IMAGE ID | Unique image identifier |
| CREATED | When the image was built |
| SIZE | Storage size |
Understanding Docker Tags
Docker images use tags to identify versions.
Examples:
docker pull nginx:1.25
docker pull nginx:alpine
docker pull nginx:latest
Why You Should Avoid latest in Production
Many beginners deploy:
docker pull nginx:latest
The problem?
latest changes over time.
Tomorrow it might point to a completely different image.
Instead, use explicit versions:
docker pull nginx:1.25.3
This guarantees consistent deployments and improves reproducibility.
Managing Container Lifecycles
Docker containers move through different states.
The most common lifecycle commands are:
docker stop my-nginx
docker start my-nginx
docker restart my-nginx
docker kill my-nginx
docker rm my-nginx
What Each Command Does
Stop a Container
docker stop my-nginx
Gracefully shuts down the container.
Start a Container
docker start my-nginx
Starts a previously stopped container.
Restart a Container
docker restart my-nginx
Stops and starts the container in one command.
Force Kill a Container
docker kill my-nginx
Immediately terminates the container.
Use this only when a normal stop fails.
Remove a Container
docker rm my-nginx
Permanently deletes a stopped container.
Visualizing the Container Lifecycle
docker run
|
v
RUNNING
|
docker stop
|
v
STOPPED
|
docker start
|
v
RUNNING
docker rm
|
v
DELETED
A common misconception is:
Stopping a container deletes it.
It doesn't.
A stopped container still exists.
Only docker rm permanently removes it.
Cleaning Up Docker Resources
As you practice Docker, you'll accumulate containers and unused resources.
Docker provides cleanup commands.
Remove All Stopped Containers
docker container prune
Remove All Containers
docker rm $(docker ps -aq)
Clean Everything Unused
docker system prune
This removes:
- Stopped containers
- Unused networks
- Dangling images
- Build cache
It's a great command when you want a clean Docker environment.
Viewing Container Logs
When containers misbehave, logs are your best friend.
The command:
docker logs my-nginx
shows everything the container has written to standard output and standard error.
Useful Log Options
docker logs my-nginx
docker logs -f my-nginx
docker logs --tail 50 my-nginx
docker logs --since 10m my-nginx
docker logs -t my-nginx
Common Usage
Follow logs in real time:
docker logs -f my-nginx
This works similarly to:
tail -f logfile.log
on Linux.
Why Docker Logs Are So Important
When a container:
- Crashes
- Fails startup
- Returns errors
- Behaves unexpectedly
Your first troubleshooting step should usually be:
docker logs <container-name>
The logs often contain the exact error message causing the issue.
Entering a Running Container with docker exec
One of Docker's most powerful commands is:
docker exec -it my-nginx /bin/bash
This opens an interactive shell inside the container.
Think of it as SSH for containers.
Understanding the Flags
docker exec -it my-nginx /bin/bash
-i
Keeps standard input open.
-t
Allocates a terminal session.
Together they create an interactive shell experience.
Some Containers Don't Have Bash
Minimal images such as Alpine Linux often don't include Bash.
If this fails:
docker exec -it container-name /bin/bash
try:
docker exec -it container-name sh
instead.
This is completely normal.
Useful Commands Inside Containers
Once inside a container, you can use standard Linux commands.
ls /
cat /etc/nginx/nginx.conf
ps aux
pwd
To exit:
exit
The shell closes, but the container continues running.
Running Multiple Containers
One of Docker's greatest strengths is isolation.
You can run multiple services on the same machine.
Example:
docker run -d --name my-nginx -p 8080:80 nginx
docker run -d --name my-redis -p 6379:6379 redis
docker run -d --name my-mongo -p 27017:27017 mongo
Each service runs independently.
Understanding Port Conflicts
Notice each container uses a different host port:
8080 -> nginx
6379 -> redis
27017 -> mongo
Docker won't allow:
8080 -> nginx
8080 -> redis
because only one process can listen on a host port at a time.
If you try, Docker will return:
port is already allocated
Seeing Everything Running
Use:
docker ps
to view all active containers.
Example:
CONTAINER ID IMAGE
abc123 nginx
def456 redis
ghi789 mongo
This confirms all services are running successfully.
Real-World DevOps Workflow
A typical troubleshooting session might look like this:
docker ps
docker logs my-app
docker exec -it my-app sh
docker restart my-app
Notice how these commands work together.
You inspect.
You investigate.
You fix.
You verify.
This workflow becomes second nature over time.
Final Thoughts
Docker containers are only useful if you know how to manage them effectively.
The commands covered in this article are the foundation of everyday Docker operations:
docker psdocker imagesdocker stopdocker startdocker rmdocker logsdocker exec
Master these commands and you'll be far more comfortable troubleshooting, debugging, and operating containerized applications.
As you continue your DevOps journey, these commands will become muscle memory—and they'll prepare you for the next step: building your own Docker images and applications.
Great Docker users don't memorize commands. They understand how containers behave and know exactly which command to reach for when something goes wrong.
Top comments (0)