Basic Container Commands
LEVEL 0
The Problem
You’ve run a container. Now you need to manage it. See what’s running. Stop things. Remove things. View logs.
Docker has dozens of commands. You don’t need them all yet. Let’s focus on the essential ones.
LEVEL 1
The Concept — The Control Panel
The Concept
Think of Docker CLI as a control panel for your container fleet.
You have sections:
- Monitoring: What’s running? What exists? What happened?
- Lifecycle: Start, stop, pause, restart
- Cleanup: Remove containers, remove images, reclaim space
- Inspection: Look inside, get details
Each button (command) does one specific thing.
LEVEL 2
The Mechanics — Essential Commands
The Mechanics
Monitoring Commands
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# List images
docker images
# View logs of a container
docker logs <container_id_or_name>
# Follow logs in real-time
docker logs -f <container_id_or_name>
Lifecycle Commands
# Create and start a container (what we've been doing)
docker run <image>
# Stop a running container (graceful)
docker stop <container_id_or_name>
# Start a stopped container
docker start <container_id_or_name>
# Restart a container
docker restart <container_id_or_name>
# Kill a container (forceful)
docker kill <container_id_or_name>
Cleanup Commands
# Remove a stopped container
docker rm <container_id_or_name>
# Remove an image
docker rmi <image_id_or_name>
# Remove all stopped containers
docker container prune
# Remove all unused images
docker image prune
# Remove everything unused (containers, images, networks, volumes)
docker system prune
Inspection Commands
# Get detailed info about a container
docker inspect <container_id_or_name>
# See resource usage (live)
docker stats
# See what files changed in a container
docker diff <container_id_or_name>
LEVEL 3
Naming Containers
By default, Docker gives containers random names like elegant_galileo or hungry_tesla. These are fun but not useful.
Name your containers:
docker run --name my-web-server nginx
Now you can use my-web-server instead of remembering container IDs:
docker logs my-web-server
docker stop my-web-server
docker rm my-web-server
Container names must be unique. If you try to create a container with a name that exists, you’ll get an error.
LEVEL 4
Container IDs and Short IDs
Every container gets a unique 64-character ID:
a4f2c8b3e9d1f6a5b4c3d2e1f0a9b8c7d6e5f4a3b2c1d0e9f8a7b6c5d4e3f2a1
You don’t need the full ID. The first few characters are usually enough:
docker stop a4f2 # Works if no other container starts with "a4f2"
docker logs a4f2c8 # More specific if needed
This applies to images too.