Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
CLI Docker Docker & Containers: From Zero to Production Docker Basics Module 1 — Container Fundamentals

Docker CLI Basics — Every Command You'll Use Daily

Reviewed & accurate
AI Summary

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

CommandWhat it does
docker runCreate and start a container
docker psList running containers
docker imagesList local images
docker pullDownload an image
docker execRun a command in a running container
docker logsView container logs
docker stopStop a running container
docker rmRemove a stopped container
docker rmiRemove an image
docker buildBuild an image from a Dockerfile
docker inspectGet detailed info about a container/image
docker statsLive resource usage of containers
docker system pruneClean 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

FlagWhat it does
-dDetached — run in background
-itInteractive — attach a terminal
-p host:containerPublish a port
-e KEY=VALUESet environment variable
-v host:containerMount a volume
--name NAMEGive the container a name
--rmAuto-remove on exit
-m 512mMemory limit
--cpus 1.5CPU 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 containers
docker stop $(docker ps -q) — stop all running containers
docker container prune — remove stopped containers
docker image prune -a — remove unused images
docker volume prune — remove unused volumes (⚠️ deletes data!)
docker network prune — remove unused networks
docker system df — see disk usage (confirm cleanup worked)

Common Mistakes

Avoid these
  • Using docker rm on a running container. It fails. Stop first, or use -f.
  • Forgetting -it with docker exec. Without -it, you get output but no interactive shell.
  • Not cleaning up. Stopped containers and unused images pile up. Run docker system prune regularly.
  • Using docker system prune -a --volumes carelessly. This deletes all unused volumes — including database data. Only run this when you're sure.
  • Confusing docker rm (containers) and docker rmi (images). rm = remove container, rmi = remove image.

Practical Exercise (15 minutes)

Run docker run -d -p 8080:80 --name web1 nginx
Run docker ps — see web1 running
Run docker exec -it web1 bash — get a shell
Inside the container, run apt-get update && apt-get install -y curl
Run curl http://localhost — see the nginx HTML
Type exit
Run docker logs web1 — see the access logs
Run docker stats --no-stream — see resource usage
Run docker stop web1 && docker rm web1 — clean up
Run docker system prune -f — remove unused resources

Mini 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 run creates and starts containers. Use -d, -it, -p, -v, -e, --name.
  • docker ps shows running containers; docker ps -a shows all.
  • docker exec -it <name> bash gets a shell in a running container.
  • docker logs -f <name> follows logs in real time.
  • docker system prune cleans up; add -a for all images, --volumes for data (careful!).
  • docker stats shows live resource usage; docker inspect shows 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.

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.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments