Luke Anderson Docker

Useful Docker Commands — Complete Cheat Sheet with Examples

Docker is the standard for containerization in 2026 — used by every major tech company and most modern development teams. But Docker has dozens of commands with confusing aliases (docker ps vs docker container ls, docker rmi vs docker image rm), and remembering which flags to use is half the battle.

This guide is the complete, up-to-date Docker commands cheat sheet you can bookmark and come back to. Every command has a real example, explanation of what it actually does, and notes on common variants. Whether you’re spinning up your first Nginx container or managing production deployments, this covers what you need to know.

💡 If you spot a command that should be added or notice something outdated, leave a comment below — this page is actively maintained.

Docker Basics: Information and Setup Commands

Show all available Docker commands

bash
docker

Lists every top-level command Docker supports. Useful when you forget what’s possible — it’s a self-documenting CLI.

Check Docker version

bash
docker version

Shows both the Docker client and server (daemon) versions. If the daemon isn’t running, only the client info will appear — that’s how you spot a stopped Docker service.

View Docker system information

bash
docker info

Displays detailed runtime info: number of containers, images, storage driver, kernel version, total memory, and more. Great for troubleshooting environment issues.

Check disk usage

bash
docker system df

Shows how much disk space images, containers, and volumes are using. Critical when your machine starts running out of space (which happens often with Docker).

Free up disk space

bash
# Remove stopped containers, unused networks, dangling images, and build cache
docker system prune

# Remove EVERYTHING unused (including unused images, not just dangling ones)
docker system prune -a

# Add volumes too (CAREFUL — destroys data)
docker system prune -a --volumes

docker system prune -a regularly reclaims 10–50GB of disk space on active dev machines.

Working With Docker Containers

List all running containers

bash
docker ps

# Same thing, modern syntax:
docker container ls

Shows only running containers by default. Use this constantly to see what’s active.

List ALL containers (running + stopped)

bash
docker ps -a

# Modern syntax:
docker container ls -a

The -a flag includes stopped/exited containers. Useful for seeing what’s been running historically.

Create and run a container in the foreground (interactive)

bash
docker container run -it -p 80:80 imageName

# Example using nginx:
docker container run -it -p 80:80 nginx

Flag breakdown:

  • -i → keep STDIN open (interactive)
  • -t → allocate a pseudo-TTY (terminal)
  • -p 80:80 → map host port 80 to container port 80
  • -it is a common combined shortcut

The container takes over your terminal — Ctrl+C stops it.

Create and run a container in the background (detached)

bash
docker container run -d -p 80:80 imageName

# Example with nginx:
docker container run -d -p 80:80 nginx

The -d flag means detached mode — the container runs in the background and your terminal returns immediately. This is how 99% of production containers are started.

Give a container a custom name

bash
docker container run -d -p 80:80 --name myAppName nginx

# Example:
docker container run -d -p 80:80 --name nginx_local nginx

Without --name, Docker auto-generates a random name like nostalgic_einstein. Naming containers makes them much easier to manage — you can use the name instead of the container ID in any command.

Stop a running container

bash
docker container stop containerIdOrName

# Examples:
docker container stop nginx_local
docker container stop abc123def456

Sends SIGTERM first (giving the container 10 seconds to shut down gracefully), then SIGKILL if it doesn’t comply.

Stop ALL running containers in one command

bash
docker stop $(docker ps -aq)

The inner command docker ps -aq returns just the IDs of all containers, which the outer docker stop then stops. Lifesaver when you want a clean slate.

Start a stopped container

bash
docker container start containerIdOrName

Brings a previously stopped container back to life with all its data intact.

Restart a container

bash
docker container restart containerIdOrName

Stops and starts in one command. Useful after config changes.

Remove a container

bash
# Remove a single container (must be stopped first)
docker container rm containerIdHere

# Force remove (even if running — Docker stops then removes)
docker container rm containerIdHere -f

# Remove multiple containers at once
docker container rm containerId1 containerId2 containerId3

Remove ALL containers

bash
docker rm $(docker ps -aq)

# Force version (also stops running ones):
docker rm $(docker ps -aq) -f

View container logs

bash
# All logs from a container
docker container logs containerName

# Follow logs in real time (like tail -f)
docker container logs -f containerName

# Show last 100 lines
docker container logs --tail 100 containerName

# Logs with timestamps
docker container logs -t containerName

The -f flag (follow) is what you’ll use most — it shows new log lines as they appear.

Inspect a container (full details)

bash
docker container inspect containerName

Returns a massive JSON object with everything about the container — network settings, volumes, environment variables, mount points, IP address, you name it. Great for debugging.

Get a container’s IP address quickly

bash
docker inspect -f '{{.NetworkSettings.IPAddress}}' containerName

A more focused version of inspect using Go template formatting.

See container resource usage (live)

bash
docker stats

Shows real-time CPU, memory, network, and disk I/O for every running container. Like top but for Docker.

Execute a command inside a running container

bash
# Open a shell inside a running container
docker exec -it containerId sh

# Or use bash if the image has it
docker exec -it containerId bash

# Run a one-off command without entering the container
docker exec containerId ls /etc

Run a new container and immediately enter it

bash
docker container run -it imageName bash

Combines run and exec — creates a fresh container and drops you into a shell. The container exits when you leave the shell.

Working With Docker Images

List all local images

bash
docker images

# Modern syntax:
docker image ls

Shows every image cached on your machine with size and tag info.

Pull an image from Docker Hub

bash
docker pull imageNameHere

# Examples:
docker pull nginx
docker pull mysql:8.0           # specific version
docker pull node:20-alpine      # specific tag (smaller alpine variant)

Downloads an image without running it. Useful for pre-fetching images before going offline.

Search Docker Hub for images

bash
docker search keyword

# Example:
docker search redis

Returns matching images from the Docker Hub registry, sorted by popularity (stars).

Remove an image

bash
docker image rm imageIdHere

# Older syntax (still works):
docker rmi imageIdHere

You can’t remove an image that’s being used by a container — stop and remove the container first, or use -f to force.

Remove ALL images

bash
docker rmi $(docker images -a -q)

# Force version:
docker rmi $(docker images -a -q) -f

Useful for completely resetting your Docker environment.

Remove only dangling images

bash
docker image prune

“Dangling” images are unnamed image layers left over from rebuilds. This frees up space without touching images you actively use.

Tag an image

bash
docker tag sourceImage:tag targetImage:tag

# Example: tag a local image for pushing to your registry
docker tag myapp:latest myregistry.com/myapp:v1.0

Tags don’t copy the image — they just create an additional name pointing to the same image data.

Push an image to a registry

bash
docker push myregistry.com/myapp:v1.0

Uploads your image to Docker Hub or a private registry. Requires docker login first.

Docker Networks

Docker networks let containers talk to each other (or stay isolated). Every container is on a network — by default it’s the bridge network.

List all available networks

bash
docker network ls

You’ll always see at least three default networks: bridge, host, and none.

Inspect a network

bash
docker network inspect networkNameHere

Shows the network’s configuration plus all containers connected to it (with their IPs).

Create a new network

bash
docker network create networkName

# With a specific driver:
docker network create --driver bridge my_bridge

Custom networks are usually bridge type (default). Use them to group related containers — they can find each other by container name instead of IP.

Run a container on a specific network

bash
docker container run -d --network networkNameHere nginx

Connect an existing container to a network

bash
docker network connect networkNameHere containerNameHere

A container can be on multiple networks simultaneously.

Disconnect a container from a network

bash
docker network disconnect networkNameHere containerNameHere

Remove a network

bash
docker network rm networkName

You can’t remove a network if any containers are still connected to it.

Working With Dockerfiles

A Dockerfile is a text file with instructions for building a custom image. Most real projects use one.

Build an image from a Dockerfile

bash
# Build with a custom name, from current directory
docker image build -t customImageName .

# Older syntax (still works):
docker build -t customImageName .

# Build with a specific Dockerfile path
docker build -t myimage -f path/to/Dockerfile .

# Build with no cache (force fresh build)
docker build -t myimage --no-cache .

The . at the end is the build context — the folder Docker uses for the build. Don’t forget it.

Run your custom image

bash
docker container run -p 80:80 --rm customImageName

The --rm flag automatically removes the container when it stops, so you don’t accumulate dead containers during development.

View build history of an image

bash
docker history imageName

Shows each layer of the image and what command created it. Useful for understanding image size and optimization.

Volumes and Bind Mounts (Persistent Data)

Containers are ephemeral — when you delete them, all data inside is gone. To persist data, you need volumes or bind mounts.

Setting up a bind mount

bash
docker container run -p 80:80 -v $(pwd):/usr/share/nginx/html nginx

This mounts your current directory ($(pwd)) into the container at /usr/share/nginx/html. Any file changes on your host are instantly visible inside the container. Perfect for development.

List all named volumes

bash
docker volume ls

Create a named volume

bash
docker volume create myvolume

Use a named volume with a container

bash
docker container run -d -v myvolume:/var/lib/mysql mysql

Named volumes are managed by Docker and stored in a special directory (/var/lib/docker/volumes/ on Linux). They survive container deletion.

Inspect a volume

bash
docker volume inspect myvolume

Shows the volume’s actual location on disk (the Mountpoint).

Remove a volume

bash
docker volume rm myvolume

# Remove all unused volumes:
docker volume prune

Bind mount vs named volume — which to use?

Bind Mount Named Volume
Best for Development (live code changes) Production (databases, persistent data)
Location Anywhere on host Docker-managed location
Performance Slower on macOS/Windows Faster on all platforms
Backup Manual docker volume commands

Docker Compose Bonus

For multi-container apps (e.g. app + database + cache), use Docker Compose:

bash
# Start all services defined in docker-compose.yml
docker compose up -d

# Stop all services
docker compose down

# View logs for all services
docker compose logs -f

# Restart a specific service
docker compose restart app

# Run a one-off command in a service
docker compose exec app bash

# Rebuild images and restart
docker compose up -d --build

Note: Modern Docker uses docker compose (with a space) instead of the older docker-compose (with a hyphen). Both work, but docker compose is the current standard built into Docker Desktop.

Common Errors and Fixes

Error Cause Fix
Cannot connect to the Docker daemon Docker isn’t running Start Docker Desktop, or sudo systemctl start docker on Linux
port is already allocated Another container/process is using that port Stop the conflicting container, or use a different host port
no space left on device Docker filled your disk docker system prune -a --volumes
permission denied while trying to connect User not in docker group (Linux) sudo usermod -aG docker $USER, then log out/in
image not found Image doesn’t exist or typo in name Check the name; docker pull it first
Conflict: unable to remove image Image is in use by a container Stop and remove the container first
OCI runtime exec failed: exec: "bash": executable file not found Image doesn’t have bash (e.g., alpine) Use sh instead of bash

Docker Commands Quick Reference (Cheat Sheet)

Containers

Command What it does
docker ps List running containers
docker ps -a List all containers
docker run -d -p 80:80 --name web nginx Run container in background
docker stop name Stop container
docker start name Start a stopped container
docker restart name Restart container
docker rm name Remove container
docker rm -f $(docker ps -aq) Remove ALL containers
docker logs -f name Follow container logs
docker exec -it name sh Open shell inside container
docker inspect name Show full container details
docker stats Live resource usage

Images

Command What it does
docker images List local images
docker pull nginx Download an image
docker push myimage Upload to registry
docker rmi imageId Remove image
docker rmi $(docker images -aq) Remove ALL images
docker build -t myimage . Build from Dockerfile
docker tag src:tag dest:tag Tag image with new name

Networks & Volumes

Command What it does
docker network ls List networks
docker network create name Create network
docker network connect net container Connect container to network
docker volume ls List volumes
docker volume create name Create volume
docker volume prune Remove unused volumes

System

Command What it does
docker info System info
docker version Version info
docker system df Disk usage
docker system prune -a Clean up everything unused

Docker Compose

Command What it does
docker compose up -d Start all services
docker compose down Stop all services
docker compose logs -f Follow logs
docker compose exec app bash Shell into a service
docker compose up -d --build Rebuild and restart

Wrapping Up

These are the Docker commands you’ll actually use day-to-day — everything from basic container management to network configuration and cleanup. Bookmark this page; the quick reference at the bottom is everything you need for 95% of Docker work.

The two commands every Docker user should run regularly:

  1. docker ps -a — see exactly what containers exist (running or not)
  2. docker system prune -a — reclaim disk space (Docker quietly eats gigabytes)

For multi-container projects, learn Docker Compose — it replaces dozens of long docker run commands with a single docker-compose.yml file. And for production deployments, look into Docker Swarm or Kubernetes once your needs grow beyond a single machine.

Discussion

Be the first to comment

Leave a comment

Get a quote