What You'll Learn
Beginner
- The 15 essential Docker commands for daily work
- The most useful flags for each
- Command patterns you'll use constantly
- How to clean up Docker resources
The Commands You'll Use Every Day
| Command | What it does |
|---|---|
docker run | Create and start a container |
docker ps | List running containers |
docker images | List local images |
docker pull | Download an image |
docker exec | Run a command in a running container |
docker logs | View container logs |
docker stop | Stop a running container |
docker rm | Remove a stopped container |
docker rmi | Remove an image |
docker build | Build an image from a Dockerfile |
docker inspect | Get detailed info about a container/image |
docker stats | Live resource usage of containers |
docker system prune | Clean up unused resources |
1. docker run — Create and Start a Container
Terminalbash
# Basic run
docker run nginx
# Run in background (detached)
docker run -d nginx
# Run with a name
docker run -d --name my-web nginx
# Run with port mapping
docker run -d -p 8080:80 nginx
# Run interactively
docker run -it ubuntu bash
# Run and auto-remove on exit
docker run --rm alpine echo "hello"
# Run with environment variables
docker run -e MY_VAR=value ubuntu env
# Run with a volume
docker run -v /host/path:/container/path nginx
Common docker run flags
| Flag | What it does |
|---|---|
-d | Detached — run in background |
-it | Interactive — attach a terminal |
-p host:container | Publish a port |
-e KEY=VALUE | Set environment variable |
-v host:container | Mount a volume |
--name NAME | Give the container a name |
--rm | Auto-remove on exit |
-m 512m | Memory limit |
--cpus 1.5 | CPU limit |
2. docker ps — List Containers
Terminalbash
# Running containers only
docker ps
# All containers (including stopped)
docker ps -a
# Only show IDs (useful for scripts)
docker ps -q
# Show last created container
docker ps -l
# Filter by name
docker ps -f name=my-web
Output format
Outputbash
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc123def456 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:8080->80/tcp my-web
3. docker images — List Local Images
Terminalbash
# List all images
docker images
# Same thing (alias)
docker image ls
# Show only IDs
docker images -q
# Filter by name
docker images nginx
4. docker pull — Download an Image
Terminalbash
# Pull latest
docker pull ubuntu
# Pull specific version
docker pull ubuntu:22.04
# Pull from a specific registry
docker pull ghcr.io/owner/repo:tag
# Pull all tags
docker pull -a alpine
5. docker exec — Run Commands in a Running Container
Very useful
If a container is running and you want to run a command inside it (without stopping it), use docker exec.Terminalbash
# Open a shell inside a running container
docker exec -it my-web bash
# Run a single command
docker exec my-web ls /etc
# Run as a specific user
docker exec -u root my-web whoami
6. docker logs — View Container Logs
Terminalbash
# View all logs
docker logs my-web
# Follow logs (like tail -f)
docker logs -f my-web
# Last 50 lines
docker logs --tail 50 my-web
# Logs since a timestamp
docker logs --since 2026-08-23T09:00:00 my-web
# With timestamps
docker logs -t my-web
7. docker stop / start / restart
Terminalbash
# Stop a container (graceful, 10s timeout)
docker stop my-web
# Force kill immediately
docker kill my-web
# Start a stopped container
docker start my-web
# Restart a running container
docker restart my-web
8. docker rm / rmi — Remove Containers and Images
Terminalbash
# Remove a stopped container
docker rm my-web
# Force remove a running container
docker rm -f my-web
# Remove an image
docker rmi nginx
# Remove all stopped containers
docker container prune
# Remove all unused images
docker image prune -a
9. docker build — Build an Image from Dockerfile
Terminalbash
# Build from Dockerfile in current directory
docker build -t myapp:1.0 .
# Build with a specific Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .
# Build without cache
docker build --no-cache -t myapp:1.0 .
10. docker inspect — Get Detailed Info
Terminalbash
# Full JSON output
docker inspect my-web
# Get just the IP address
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-web
# Get the entrypoint
docker inspect -f '{{.Config.Entrypoint}}' my-web
11. docker stats — Live Resource Usage
Terminalbash
# Live stats for all running containers
docker stats
# Stats for a specific container
docker stats my-web
# One snapshot (no live update)
docker stats --no-stream
Output:
Outputbash
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O
abc123def456 my-web 0.50% 25MiB / 512MiB 4.89% 5.2kB / 8.1kB 0B / 0B
12. docker system prune — Clean Up Everything
This removes data
These commands delete stopped containers, unused images, and unused networks. Use with care.Terminalbash
# Remove stopped containers, unused networks, dangling images
docker system prune
# Also remove all unused images (not referenced by containers)
docker system prune -a
# Also remove volumes (BE CAREFUL — deletes data!)
docker system prune -a --volumes
Command Patterns You'll Use Constantly
Pattern 1: "Run this one-off command"
Patternbash
docker run --rm alpine echo "hello"
Pattern 2: "Get a shell in that container"
Patternbash
docker exec -it <container-name> bash
Pattern 3: "What's using my disk?"
Patternbash
docker system df
Pattern 4: "Clean everything and start fresh"
Patternbash
docker stop $(docker ps -q) && docker system prune -a --volumes
Pattern 5: "Stop all running containers"
Patternbash
docker stop $(docker ps -q)
Cleaning Up — The Full Workflow
docker ps -a — see all containersdocker stop $(docker ps -q) — stop all running containersdocker container prune — remove stopped containersdocker image prune -a — remove unused imagesdocker volume prune — remove unused volumes (⚠️ deletes data!)docker network prune — remove unused networksdocker system df — see disk usage (confirm cleanup worked)Common Mistakes
Avoid these
- Using
docker rmon a running container. It fails. Stop first, or use-f. - Forgetting
-itwithdocker exec. Without-it, you get output but no interactive shell. - Not cleaning up. Stopped containers and unused images pile up. Run
docker system pruneregularly. - Using
docker system prune -a --volumescarelessly. This deletes all unused volumes — including database data. Only run this when you're sure. - Confusing
docker rm(containers) anddocker rmi(images).rm= remove container,rmi= remove image.
Practical Exercise (15 minutes)
Run
docker run -d -p 8080:80 --name web1 nginxRun
docker ps — see web1 runningRun
docker exec -it web1 bash — get a shellInside the container, run
apt-get update && apt-get install -y curlRun
curl http://localhost — see the nginx HTMLType
exitRun
docker logs web1 — see the access logsRun
docker stats --no-stream — see resource usageRun
docker stop web1 && docker rm web1 — clean upRun
docker system prune -f — remove unused resourcesMini Challenge
Create a "cleanup script" — a single command that stops all running containers, removes all stopped containers, and removes all unused images. Test it carefully. (Hint: use $(docker ps -q) for container IDs.)
Key Takeaways
docker runcreates and starts containers. Use-d,-it,-p,-v,-e,--name.docker psshows running containers;docker ps -ashows all.docker exec -it <name> bashgets a shell in a running container.docker logs -f <name>follows logs in real time.docker system prunecleans up; add-afor all images,--volumesfor data (careful!).docker statsshows live resource usage;docker inspectshows detailed JSON.
Previously: Lesson 06 ran your first containers.
Today: You learned the 15 essential Docker CLI commands and their flags.
Next: Lesson 08 goes deeper into the container lifecycle.
Today: You learned the 15 essential Docker CLI commands and their flags.
Next: Lesson 08 goes deeper into the container lifecycle.
FAQ
What's the difference between docker stop and docker kill?
docker stop sends SIGTERM, waits 10 seconds for graceful shutdown, then sends SIGKILL. docker kill sends SIGKILL immediately. Use stop for graceful shutdown; kill only when the container is stuck.
How do I see what's inside a container's filesystem?
Use docker exec -it <name> ls / for a quick look, or docker exec -it <name> bash for a full shell. For a stopped container, use docker cp <name>:/path ./local-path to copy files out.
Comments
Comments
Post a Comment