CheatSheetHub / Docker Commands

Docker Commands Cheat Sheet

Containers, images, volumes, networks, and Compose — the commands you use daily, grouped by what you're managing.

Containers Images Volumes & networks Docker Compose Cleanup

Containers

docker run -d -p 8080:80 nginxRun a container in the background, mapping host port 8080 to container port 80.
docker psList running containers.
docker ps -aList all containers, including stopped ones.
docker exec -it <name> bashOpen an interactive shell inside a running container.
docker logs -f <name>Stream a container's logs in real time.
docker stop <name>Gracefully stop a running container.
docker kill <name>Immediately force-stop a container.
docker restart <name>Stop and start a container again.
docker inspect <name>Show detailed configuration and state of a container as JSON.
docker cp file.txt <name>:/app/Copy a file from the host into a running container.
docker statsShow live CPU, memory, and network usage per container.
docker rename old newRename a container.
docker run --rm imageAutomatically remove the container once it exits.
docker run --env-file .env imageLoad environment variables from a file into the container.

Images

docker build -t myapp .Build an image from a Dockerfile in the current directory.
docker imagesList locally stored images.
docker pull nginx:latestDownload an image from a registry without running it.
docker push user/myappUpload a local image to a registry.
docker tag myapp user/myapp:v1Give an existing image a new name/tag.
docker history myappShow the layers that make up an image.
docker build --no-cache -t myapp .Rebuild an image ignoring any cached layers.
docker save myapp -o myapp.tarExport an image to a tar archive.
docker load -i myapp.tarImport an image from a tar archive.
docker inspect myappShow detailed metadata about an image.
[ ad space — 336×280 ]

Volumes & networks

docker volume create mydataCreate a named volume for persistent storage.
docker run -v mydata:/app/data ...Mount a named volume into a container.
docker run -v $(pwd):/app ...Bind-mount the current host directory into the container.
docker network lsList Docker networks.
docker network create mynetCreate a custom network so containers can reach each other by name.
docker network connect mynet <name>Attach a running container to a network.
docker volume lsList all volumes.
docker volume inspect mydataShow details about a specific volume, including its host path.
docker volume rm mydataDelete a named volume.

Docker Compose

docker compose up -dStart all services defined in docker-compose.yml in the background.
docker compose downStop and remove all services, networks created by up.
docker compose logs -fStream logs from all services at once.
docker compose buildRebuild images for services with a build section.
docker compose exec web bashOpen a shell in the running "web" service.
docker compose psList the status of services defined in the compose file.
docker compose restartRestart all services.
docker compose up -d --buildRebuild images and start services in one step.

Cleanup

docker system pruneRemove stopped containers, unused networks, and dangling images.
docker system prune -aAlso remove all unused images, not just dangling ones.
docker rm $(docker ps -aq)Remove all containers, running or not (stop them first).
docker rmi $(docker images -q)Remove all locally stored images.
docker volume pruneRemove all volumes not used by at least one container.
docker container pruneRemove all stopped containers.

Common questions

What's the difference between docker stop and docker kill?

docker stop sends a graceful shutdown signal (SIGTERM) and waits a grace period before force-killing the container. docker kill sends an immediate, unblockable termination signal (SIGKILL) with no grace period.

Why does my container exit immediately after starting?

A container only stays running while its main process (PID 1) is running. If that process finishes or crashes right away, the container exits. Check the exit reason with docker logs <container> and confirm the command isn't something that completes instantly.

How do I remove all stopped containers and unused images at once?

Run docker system prune to remove stopped containers, unused networks, and dangling images. Add -a to also remove all unused images, not just dangling ones.

Copied