๐Ÿณ 5 Docker Commands Every DevOps Beginner Should Know (With Real Examples)

blog post

๐Ÿณ 5 Docker Commands Every DevOps Beginner Should Know (With Real Examples)

Welcome to DevOps Daily - your daily dose of practical DevOps learnings from my journey from beginner to cloud engineer.

I'm Ande, and six months ago, I didn't know what a Docker container was. Today, I'm deploying production apps with Blue-Green strategies.

This newsletter is where I share what actually worked - no fluff, no gatekeeping, just real lessons from the trenches.

Let's dive into today's topic.


๐ŸŽฏ Why Docker Commands Matter

When I started Project 1 (building a Dockerized Todo app), I spent hours stuck because I didn't know these 5 commands.

Knowing them would have saved me at least 10 hours of debugging.

Here they are - with the exact examples I use daily.


1๏ธโƒฃ docker ps -a โ€” See ALL Your Containers

The Problem: You run a container, it exits, and you think "Where did it go?!"

The Solution:

# See only RUNNING containers
docker ps

# See ALL containers (running + stopped)
docker ps -a

Real Example from My Project:
$ docker ps -a
CONTAINER ID   IMAGE          COMMAND                  STATUS                    
a1b2c3d4e5f6   todo-api:latest   "node server.js"         Up 2 hours                
f6e5d4c3b2a1   mongo:7        "docker-entrypoint.sโ€ฆ"   Exited (0) 5 minutes ago   

๐Ÿ‘‰ Pro Tip: Add --format for cleaner output:
docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

Why this matters: You can't debug what you can't see. This command shows you the full picture.
2๏ธโƒฃ docker logs <container> --tail 50 -f - Watch Logs in Real-Time

The Problem: Your app crashes, but you don't know why.
The Solution:
# See last 50 lines of logs
docker logs todo-api --tail 50

# Follow logs in real-time (like tail -f)
docker logs todo-api --tail 50 -f

Real Example:
$ docker logs todo-api --tail 20 -f
[INFO] Server running on port 3000
[ERROR] MongoDB connection failed: ECONNREFUSED
[INFO] Retrying connection in 5s...
[INFO] MongoDB connected successfully โœ…

๐Ÿ‘‰ Pro Tip: Filter logs by keyword:
docker logs todo-api 2>&1 | grep -i error

Why this matters: 90% of my debugging starts with docker logs. It's your first line of defense.

3๏ธโƒฃ docker exec -it <container> bash - Jump Inside a Running Container

The Problem: You need to check a file, run a command, or debug inside the container โ€” but you can't "SSH" into it.

The Solution:
# Open an interactive bash session inside the container
docker exec -it todo-api bash

Real Example:
$ docker exec -it todo-api bash
root@a1b2c3d4e5f6:/app# ls -la
total 48
drwxr-xr-x 1 root root 4096 Mar  5 10:30 .
drwxr-xr-x 1 root root 4096 Mar  5 10:30 ..
-rw-r--r-- 1 root root  321 Mar  5 10:30 package.json
-rw-r--r-- 1 root root 2145 Mar  5 10:30 server.js
# Now you can debug like you're inside!
root@a1b2c3d4e5f6:/app# exit  # Type exit to leave

๐Ÿ‘‰ Pro Tip: Use sh instead of bash for Alpine-based images:
docker exec -it todo-api sh  # For alpine images

4๏ธโƒฃ docker-compose up -d --build - Rebuild & Restart Everything

The Problem: You changed your code, but the container is still running the old version.

The Solution:
# Stop, rebuild, and restart all services
docker-compose up -d --build

Real Example from My CI/CD Pipeline:
# In my GitHub Actions workflow:
- name: Deploy to EC2
  run: |
    cd /home/ubuntu/todo-api-docker
    docker-compose down
    docker-compose build --no-cache  # Force fresh build
    docker-compose up -d

๐Ÿ‘‰ Pro Tip: Add --remove-orphans to clean up unused containers:
docker-compose up -d --build --remove-orphans

Why this matters: This is the command I use for every deployment. It ensures your changes actually take effect.

5๏ธโƒฃ docker system prune -a - Free Up Disk Space (Safely)

The Problem: Your server is running out of disk space because of old images and containers.

The Solution:
# Remove unused data (containers, networks, images, build cache)
docker system prune -a

Real Example:
$ docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          15        3         2.5GB     1.8GB (72%)
Containers      8         2         150MB     120MB (80%)

$ docker system prune -a
Deleted Images:
untagged: todo-api:latest
deleted: sha256:abc123...

$ docker system df  # After cleanup
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          3         3         700MB     0B (0%)

โš ๏ธ Warning: This deletes ALL unused images. Make sure you don't need them first!

๐Ÿ‘‰ Safer Alternative: Just remove dangling images:
docker image prune  # Only removes <none> tagged images

Why this matters: I learned this the hard way when my EC2 instance ran out of space during a deployment.

๐ŸŽ Bonus: My Docker Cheat Sheet
I keep this saved as ~/.docker-cheatsheet:
# Quick Reference
docker ps -a                          # List all containers
docker logs <name> -f --tail 50      # Follow logs
docker exec -it <name> bash          # Enter container
docker-compose up -d --build         # Rebuild & restart
docker system prune -a               # Clean up space

# Debugging
docker inspect <name>               # Detailed container info
docker stats                        # Live resource usage
docker-compose logs -f              # All services logs

# Cleanup
docker container prune -f           # Remove stopped containers
docker image prune -a -f            # Remove unused images
docker volume prune -f              # Remove unused volumes

๐Ÿ’ก Key Takeaway

You don't need to memorize 100 Docker commands.

Master these 5, and you'll handle 90% of daily DevOps tasks:

docker ps -a โ†’ See what's running
docker logs -f โ†’ Debug issues
docker exec -it โ†’ Investigate inside
docker-compose up --build โ†’ Deploy changes
docker system prune โ†’ Keep things clean

๐Ÿ”œ Coming Tomorrow

Topic: "Why My Terraform Plan Failed (And How I Fixed It in 3 Steps)"

We'll cover:
The #1 mistake beginners make with Terraform
How to read error messages like a pro
My debugging checklist for IaC

๐Ÿ™‹ Your Turn

Which of these 5 commands was new to you?
What's your most-used Docker command?
What DevOps topic should I cover next?

Comment on the blog - I read every response! ๐Ÿ‘‡