Docker Lesson 6 – Docker CLI Basics | Dataplexa
Docker · Lesson 6

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

The docker command is your control center. Every container you'll ever create, stop, or debug starts here.
Docker CLI — Daily Commands
$ docker run nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
$ docker ps
CONTAINER ID IMAGE COMMAND STATUS
$ docker stop 7f8a9b2c
7f8a9b2c
Every Docker command follows the same pattern: docker [command] [options] [image/container]. The CLI is consistent — learn one command structure, and you know them all.

Docker Command Anatomy

docker
run
-p 80:80
nginx
Base command
Action
Flags
Target

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

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

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

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

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