Docker commands cheat sheet

Run, inspect and clean up Docker containers and images, work with Docker Compose, and reclaim disk space safely.

15 commands

List all containers

macOSWindowsLinux

Running and stopped containers with their status and ports.

docker ps -a

Run a container on a port

macOSWindowsLinux

Starts a container in the background and maps a port to your machine.

docker run -d -p : --name

Throwaway interactive container

macOSWindowsLinux

Opens a shell in a fresh container that is deleted when you exit.

docker run -it --rm bash

Open a shell inside a container

macOSWindowsLinux

Use bash instead of sh if the image has it.

docker exec -it sh

Follow a container’s logs

macOSWindowsLinux

Last 100 lines, then streams new ones.

docker logs -f --tail 100

Build an image

macOSWindowsLinux

Builds from the Dockerfile in the current folder.

docker build -t .

Copy a file out of a container

macOSWindowsLinux

Works the other way round too.

docker cp :

Start a Compose project

macOSWindowsLinux

Builds images if needed and starts all services in the background.

docker compose up -d --build

Follow Compose logs

macOSWindowsLinux

Leave out the service name to see all of them.

docker compose logs -f

Stop a Compose project

macOSWindowsLinux

Stops and removes the containers and network. Add -v to also delete volumes (data).

docker compose down

Stop all running containers

macOSLinuxCareful

Gracefully stops every running container.

docker stop $(docker ps -q)

How much space Docker uses

macOSWindowsLinux

Space used by images, containers, volumes and build cache.

docker system df

Remove dangling images

macOSWindowsLinux

Deletes untagged leftover layers from old builds. Asks before deleting.

docker image prune

Remove stopped containers

macOSWindowsLinuxCareful

Deletes all stopped containers. Asks before deleting.

docker container prune

Reclaim all unused Docker space

macOSWindowsLinuxDestructive

Removes stopped containers, unused networks, every image not used by a container, and the build cache.

docker system prune -a

You’ll need to re-download or rebuild images. Volumes are kept unless you add --volumes.

Every command copies as plain, clean text: no curly quotes, no long dashes, no invisible characters.

Related guides