Galle, LK--:--5/6 sites live

Essential Docker CLI Commands & Lifecycle

Learn the daily command toolkit used by DevOps teams to manage, execute, inspect, and troubleshoot container instances.

5 Jun 20263 min readSelf Guide · Stage 3#Docker#Containers#W3DevOps

Lesson 5 of 26 · Module: Docker

18 min read · Source notes — Docker Main Commands

Learn the daily command toolkit used by DevOps teams to manage, execute, inspect, and troubleshoot container instances.

Screenshot: Docker Main Commands

Instructor running docker pull, run, and inspecting container status tables.

1. Running Containers (docker run)

The docker run command combines docker pull (if image not found locally), docker create, and docker start into one atomic operation.

Example: Running an Nginx Web Server

ExampleShell
# Run detached in background (-d) with name and port forwarding (-p host:container)
docker run -d \
  --name web-server \
  -p 8080:80 \
  --restart unless-stopped \
  nginx:alpine

Screenshot: Inspecting Running Containers

Demonstrating container port mapping and background daemon processes.

Core Command Matrix

CommandDevOps Use CaseKey Flags
docker psList running containers-a (show stopped), -q (quiet, only IDs)
docker logs <name>Fetch container stdout/stderr logs-f (follow/stream live), --tail 100
docker exec -it <name> shOpen interactive shell inside running container-i (interactive stdin), -t (allocate TTY)
docker stop <name>Sends SIGTERM, waits 10s grace period, then SIGKILL-t 30 (wait 30 seconds before kill)
docker rm <name>Remove stopped container-f (force remove running container)
docker imagesList local cached images with repository & tags-q (IDs only for piping)
docker system pruneReclaim disk space by purging unused containers, networks, dangling images-a --volumes (thorough cleanup)

Pro Tip: Debugging Crashed Containers

If a container crashes immediately with Exit Code 1 or Exit Code 137, do not delete it! Run:

ExampleShell
# View the final dying breath of the process
docker logs <container_id>

# Check the exact exit code and OOM status
docker inspect <container_id> --format='{{.State.ExitCode}} OOMKilled={{.State.OOMKilled}}'

Keep going: ← Lesson 4 · Lesson 6 → · Module overview · Track it in the Self Guide