Docker Common Commands - CodeSwordsman

High-frequency commands for images, containers, Compose, networks and volumes, with run examples and cleanup warnings.

Docker 20.10+ / Compose V2Updated 2026-10-2931 commands

Images

List local images

docker images

Pull a pinned image tag; avoid latest in production

docker pull nginx:1.25

Build an image from the Dockerfile in the current directory

docker build -t app:1.0 .

Remove a local image

docker rmi app:1.0

Save an image to a file for offline environments

docker save -o app.tar app:1.0

Load an image from a file

docker load -i app.tar
Risky

Remove dangling images; -a deletes all unused images

docker image prune

Containers

List all containers, including stopped ones

docker ps -a

Run detached with port mapping, read-only volume and restart policy

docker run -d --name web -p 8080:80 -v /data/html:/usr/share/nginx/html:ro --restart unless-stopped nginx:1.25

Open a shell in a running container; use sh on slim images

docker exec -it web /bin/bash

Follow the last 100 log lines of a container

docker logs -f --tail 100 web

Stop and start a container

docker stop web && docker start web

Show full container config including IP, mounts and networks

docker inspect web

Show container CPU, memory and network usage

docker stats --no-stream

Copy files between host and container

docker cp ./app.conf web:/etc/nginx/conf.d/
Risky

Force-remove a container; unpersisted data inside is lost

docker rm -f web

Docker Compose

Start all services from compose.yaml in the background

docker compose up -d

Show container status for the project

docker compose ps

Follow logs of a specific service

docker compose logs -f api

Run a command inside a service container

docker compose exec api sh

Pull the latest images used by services

docker compose pull
Risky

Stop and remove containers and networks; -v also removes volumes

docker compose down

Networks & Volumes

List container networks

docker network ls

Create a user-defined network for service-name resolution

docker network create app-net

Inspect containers and IPs attached to a network

docker network inspect app-net

List volumes

docker volume ls

Show the host path of a volume

docker volume inspect app-data

Show disk usage of images, containers and volumes

docker system df

Cleanup (Careful)

Risky

Remove stopped containers, dangling images and unused networks

docker system prune
Risky

Also delete unused images and volumes; data loss is possible

docker system prune -a --volumes
Risky

Remove only stopped containers

docker container prune

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.