Docker Commands Explained with Real-World Examples, Tricks, and Tips

Docker commands look simple on the surface.
But in real environments, how and when you use them matters far more than memorizing syntax.

This post walks through the most commonly used Docker commands, with practical examples, real-world usage patterns, and tips that actually help in production.


Docker Container Management Commands

These are the commands you’ll use daily.


docker ps

Lists running containers.

docker ps

Real-world use:
Quickly verify if your application container is actually running after a deployment.

Tip:
Use formatting for clarity:

docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

docker ps -a

Lists all containers (including stopped ones).

docker ps -a

Real-world use:
Find containers that exited unexpectedly during startup.

Tip:
Look for containers stuck in Exited (1) — usually misconfig or crash.


docker run <image>

Run a container from an image.

docker run nginx

Real-world use:
Quickly test an image locally before using it in CI/CD or Kubernetes.


docker run -d <image>

Run in detached mode.

docker run -d nginx

Tip:
Always use -d for background services. Forgetting this is a common beginner mistake.


docker run -it <image> /bin/bash

Run an interactive container.

docker run -it ubuntu /bin/bash

Real-world use:
Debug images, test scripts, or validate OS-level changes.


docker exec -it <container> /bin/bash

Run a command inside a running container.

docker exec -it web-app /bin/bash

Production tip:
Avoid using this in production unless debugging — it breaks immutability assumptions.


docker stop <container>

Gracefully stop a container.

docker stop api

Tip:
Docker sends SIGTERM first — your app must handle it properly.


docker start <container>

Start a stopped container.

docker start api

docker restart <container>

Restart container.

docker restart api

Real-world use:
Temporary recovery, not a fix. If you restart often, you have a real issue.


docker rm <container>

Remove a container.

docker rm api

Tip:
Clean stopped containers regularly:

docker container prune

docker logs <container>

View container logs.

docker logs api

Production trick:

docker logs -f --tail 100 api

Follow logs without overwhelming output.


docker inspect <container>

Get detailed container info.

docker inspect api

Real-world use:
Check:

  • Environment variables
  • Mounts
  • Network details

Tip:
Pipe to jq for clarity:

docker inspect api | jq '.[0].Config.Env'

Docker Image Management Commands

Images define what runs, not containers.


docker images

List all images.

docker images

Tip:
Old images consume disk silently — clean regularly.


docker pull <image>

Download image.

docker pull nginx:1.25

Production tip:
Always pin versions.
nginx:latest
nginx:1.25


docker build -t <name> .

Build image from Dockerfile.

docker build -t myapp:1.0 .

Tip:
Keep Dockerfiles small and layered efficiently to speed up builds.


docker rmi <image>

Remove image.

docker rmi myapp:1.0

Tip:
Force remove dangling images:

docker image prune

docker inspect <image>

Inspect image metadata.

docker inspect nginx

Useful to verify:

  • Exposed ports
  • Entrypoint
  • Cmd

docker history <image>

Show image layers.

docker history myapp:1.0

Real-world insight:
Large layers = inefficient Dockerfile.


Docker Network & Volume Commands

This is where data and connectivity live.


docker network ls

List networks.

docker network ls

Tip:
Use user-defined networks for app-to-app communication.


docker network create <name>

Create a network.

docker network create app-net

Real-world use:
Connect backend and database containers securely.


docker volume ls

List volumes.

docker volume ls

docker volume create <name>

Create volume.

docker volume create db-data

Critical tip:
Volumes survive container restarts — but they are not backups.


Miscellaneous Docker Commands (Often Overlooked)


docker info

System-wide Docker information.

docker info

Use:
Verify storage driver, cgroup version, runtime.


docker version

Docker client & server version.

docker version

docker stats

Live resource usage.

docker stats

Production tip:
Use this for quick checks — rely on proper monitoring long-term.


docker login

Login to registry.

docker login

Security tip:
Never hardcode credentials in scripts.


docker logout

Logout safely.

docker logout

⚠️ Common Docker Mistakes in Real Systems

🚫 Treating containers as VMs
🚫 Using latest tags
🚫 Debugging production via docker exec
🚫 Assuming volumes = backups
🚫 Restarting instead of fixing root cause


🧠 Practical Docker Tips from Production

✔ Keep containers stateless
✔ Externalize config & secrets
✔ Log to stdout/stderr only
✔ Use multi-stage builds
✔ Clean unused images regularly


Final Thoughts

Docker commands are easy to learn.
Knowing when and why to use them is the real skill.

In production, Docker is not about containers —
it’s about operability, predictability, and discipline.

If you understand these commands in context, Docker becomes a powerful ally instead of a debugging nightmare.

🤞 Don’t miss the posts!

We don’t spam! Read more in our privacy policy

🤞 Don’t miss the posts!

We don’t spam! Read more in our privacy policy

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top