Docker Course
Docker CLI Basics
Master the essential Docker commands that every developer uses daily to manage containers and debug production issues.
This lesson covers
Essential Docker commands · Container lifecycle management · Debugging running containers · Command structure and flags
Your Docker CLI Toolbox
Thedocker command is your control center. Every container you'll ever create, stop, or debug starts here.
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
$ docker ps
CONTAINER ID IMAGE COMMAND STATUS
$ docker stop 7f8a9b2c
7f8a9b2c
Docker Command Anatomy
Container Lifecycle Commands
The scenario: You're a backend developer at a fintech startup. Your team lead asks you to spin up a PostgreSQL database for testing a new payment feature. You need it running in 5 minutes.Tools used:
- docker run — Creates and starts a new container from an image
- docker ps — Lists running containers (like ps for processes)
- docker stop — Gracefully shuts down a running container
# Start a PostgreSQL container for development
docker run --name dev-postgres -e POSTGRES_PASSWORD=mypassword -p 5432:5432 postgres:13
# Check if the container is running
docker ps
# Stop the database when done testing
docker stop dev-postgres
Unable to find image 'postgres:13' locally 13: Pulling from library/postgres 7b1a6ab2e44d: Pull complete 90eb44ebc60b: Pull complete 5ba7d5c29319: Pull complete c655bc89a2a8: Pull complete Digest: sha256:f8f8c89d3b1d Status: Downloaded newer image for postgres:13 2023-10-15 14:23:10.123 UTC [1] LOG: database system is ready to accept connections CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f8a9b2c1d3e postgres:13 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:5432->5432/tcp dev-postgres dev-postgres
What just happened?
0.0.0.0:5432->5432/tcp — Docker mapped your computer's port 5432 to the container's port 5432. Your apps can now connect to localhost:5432 and reach the PostgreSQL database running inside the container.
--name flag
Gives your container a memorable name. Without it, Docker generates random names like angry_tesla
-e flag
Sets environment variables inside the container. PostgreSQL requires POSTGRES_PASSWORD
-p flag
Port mapping. Format: host:container. Makes the container accessible from your machine
postgres:13
Image name and tag. Always specify versions in production — latest changes unpredictably
Common Mistake: Forgetting -d flag
Without -d (detached), the container runs in foreground and locks your terminal. Add -d to run in background: docker run -d --name dev-postgres ...
Inspecting and Debugging Containers
The scenario: It's 2 AM. Your company's API is returning 500 errors. Your team suspects the Redis cache container is having issues. You need to check what's happening inside the container — fast.# Show all containers (running and stopped)
docker ps -a
# View container logs to see what went wrong
docker logs redis-cache
# Get inside a running container to debug
docker exec -it redis-cache /bin/bash
CONTAINER ID IMAGE COMMAND STATUS NAMES 7f8a9b2c1d3e postgres "docker-entrypoint.s…" Up 2 hours dev-postgres a1b2c3d4e5f6 redis "docker-entrypoint.s…" Exited (1) 10 minutes ago redis-cache 1:C 15 Oct 2023 14:45:12.345 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 15 Oct 2023 14:45:12.345 # Redis version=7.0.5 1:M 15 Oct 2023 14:55:30.123 # Out of memory: Kill process or sacrifice child 1:M 15 Oct 2023 14:55:30.124 # Redis is now ready to exit, bye bye... root@a1b2c3d4e5f6:/data#
What just happened?
Exited (1) shows the container crashed with error code 1. The logs reveal Out of memory — Redis ran out of RAM. The root@a1b2c3d4e5f6 prompt means you're now inside the container's shell, ready to investigate further.
🔍 For Investigation
docker logs
Shows container output. Add -f to follow logs in real-time, --tail 50 for recent lines only.
🛠️ For Debugging
docker exec
Runs commands inside running containers. -it gives you an interactive terminal session.
Essential Debugging Commands
# Check container resource usage
docker stats redis-cache
# See detailed container information
docker inspect redis-cache
# Restart a crashed container
docker restart redis-cache
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O
a1b2c3d4e5f6 redis-cache 0.50% 245.7MiB / 256.0MiB 95.98% 1.2kB / 648B
[
{
"Id": "a1b2c3d4e5f6c7d8e9f0a1b2c3d4e5f6",
"Created": "2023-10-15T14:45:10.123456789Z",
"State": {
"Status": "exited",
"ExitCode": 1,
"Error": "",
"StartedAt": "2023-10-15T14:45:12.345Z",
"FinishedAt": "2023-10-15T14:55:30.124Z"
},
"Config": {
"Image": "redis:7.0",
"Env": [
"REDIS_MAXMEMORY=256mb"
]
}
}
]
redis-cache
What just happened?
MEM %: 95.98% confirms the memory issue — Redis was using almost all available RAM. "ExitCode": 1 from inspect shows it crashed with an error. The restart command will attempt to start it again with the same configuration.
Managing Container Data and Files
The scenario: You're deploying a Node.js application. The container works locally, but you need to copy the latest configuration file into the production container before the evening launch.# Copy a file from host into running container
docker cp ./config.json web-app:/app/config.json
# Copy a file from container to host (for backup/debugging)
docker cp web-app:/app/logs/error.log ./error-backup.log
# Mount a directory for real-time development
docker run -d --name web-app -v $(pwd):/app -p 3000:3000 node:16
$ docker cp ./config.json web-app:/app/config.json (no output - success) $ docker cp web-app:/app/logs/error.log ./error-backup.log (no output - success) $ docker run -d --name web-app -v $(pwd):/app -p 3000:3000 node:16 7a8b9c0d1e2f3g4h5i6j7k8l9m0n1o2p3q4r5s6t7u8v9w0x1y2z3a4b5c6d7e8f
What just happened?
docker cp works even with running containers — no need to stop them. The -v $(pwd):/app mount creates a live connection: changes to files on your computer instantly appear inside the container at /app.
Path Format Matters
Docker cp syntax is container:path for container files, just path for host files. Wrong: /container/file. Right: container:/path/file.
Cleanup and Management
Docker containers accumulate quickly during development. Your disk fills up. Your teammates complain about slow Docker builds. Time to clean house.# Remove a stopped container
docker rm web-app
# Remove a running container (force)
docker rm -f redis-cache
# Remove all stopped containers at once
docker container prune
# Nuclear option: remove everything unused
docker system prune -a