Free developer tool

Free Docker Commands Cheat Sheet

A Docker commands cheat sheet is a quick reference for the CLI commands you use to run and debug containers, build and share images, manage volumes and networks, drive Docker Compose and clean up disk space. Search 74 commands by name or task, copy a working example and see which ones delete data before you run them. Nothing here runs on your machine.

Showing 74 of 74 commands

Run & manage containers

docker run

Create and start a new container from an image. Pulls the image first if it is missing.

Syntax: docker run [options] image [command]

-d
run in the background (detached)
-p 8080:80
publish host port 8080 to container port 80
--name web
give the container a name
-e KEY=value
set an environment variable
-v ./data:/data
mount a host folder or volume
--rm
delete the container when it exits
$ docker run -d --name web -p 8080:80 nginx

docker run -it

Start a container with an interactive terminal, for example a throwaway shell.

Syntax: docker run -it [--rm] image [shell]

-i
keep stdin open
-t
allocate a terminal
--rm
remove it when you exit
--entrypoint sh
override the image entrypoint
$ docker run -it --rm ubuntu bash

docker ps

List running containers with their IDs, images, ports and status.

Syntax: docker ps [options]

-a
include stopped containers
-q
print only container IDs
--filter status=exited
filter by status, name, label and more
--format
custom output with a Go template
$ docker ps -a

docker stop

Stop a running container gracefully: sends SIGTERM, then SIGKILL after a timeout.

Syntax: docker stop [-t seconds] container...

-t 30
wait 30 seconds before killing (default 10)
$ docker stop web

docker start

Start one or more stopped containers.

Syntax: docker start [options] container...

-a
attach to output
-i
attach stdin
$ docker start web

docker restart

Stop and start a container again.

Syntax: docker restart [-t seconds] container...

-t
seconds to wait before killing
$ docker restart web

docker kill

Stop a container immediately with SIGKILL, or send another signal.

Syntax: docker kill [-s signal] container...

-s HUP
send a different signal, such as HUP to reload
$ docker kill web

Careful: SIGKILL gives the process no chance to flush data or close connections. Try docker stop first.

docker rm

Delete one or more stopped containers.

Syntax: docker rm [options] container...

-f
force-remove a running container
-v
also remove its anonymous volumes
$ docker rm web

Careful: Anything written inside the container and not in a volume is lost. With -f a running container is killed first; with -v its anonymous volumes are deleted.

docker pause / unpause

Freeze or resume every process in a container.

Syntax: docker pause container · docker unpause container

$ docker pause web

docker rename

Rename a container.

Syntax: docker rename old new

$ docker rename web web-old

docker update

Change resource limits or the restart policy of a container without recreating it.

Syntax: docker update [options] container...

--restart unless-stopped
restart automatically unless you stopped it
--memory 512m
memory limit
--cpus 1.5
CPU limit
$ docker update --restart unless-stopped web

docker wait

Block until a container stops, then print its exit code.

Syntax: docker wait container...

$ docker wait migrate

docker create

Create a container without starting it.

Syntax: docker create [options] image [command]

--name
give the container a name
$ docker create --name job alpine echo hi

Logs, exec & inspect

docker logs

Show what a container wrote to stdout and stderr.

Syntax: docker logs [options] container

-f
follow new output
--tail 100
only the last 100 lines
--since 10m
only the last 10 minutes
-t
add timestamps
$ docker logs -f --tail 100 web

docker exec

Run a command inside a running container, such as a shell.

Syntax: docker exec [options] container command

-it
interactive terminal
-u root
run as a specific user
-w /app
working directory
-e KEY=value
set an environment variable
$ docker exec -it web sh

docker inspect

Print low-level JSON details of a container, image, volume or network.

Syntax: docker inspect [options] name|id...

-f
pick one field with a Go template
--type
container, image, volume or network
$ docker inspect -f '{{.NetworkSettings.IPAddress}}' web

docker stats

Live CPU, memory, network and disk usage per container.

Syntax: docker stats [options] [container...]

--no-stream
print one snapshot and exit
-a
include stopped containers
$ docker stats --no-stream

docker top

List the processes running inside a container.

Syntax: docker top container [ps options]

$ docker top web

docker attach

Connect your terminal to a running container's main process.

Syntax: docker attach container

Ctrl+P Ctrl+Q
detach without stopping it
$ docker attach web

Careful: Pressing Ctrl+C while attached can stop the container. Detach with Ctrl+P then Ctrl+Q, or use docker exec instead.

docker port

Show which host ports map to a container's ports.

Syntax: docker port container [port]

$ docker port web

docker diff

List files added (A), changed (C) or deleted (D) in a container since it started.

Syntax: docker diff container

$ docker diff web

docker events

Stream real-time events from the Docker daemon: starts, stops, pulls and more.

Syntax: docker events [options]

--filter container=web
only events for one container
--since 1h
events from the last hour
$ docker events --filter container=web

Images

docker images

List images stored locally. Same as docker image ls.

Syntax: docker images [options] [repository[:tag]]

-a
include intermediate images
-q
print only image IDs
--filter dangling=true
untagged images only
$ docker images

docker pull

Download an image from a registry such as Docker Hub.

Syntax: docker pull image[:tag|@digest]

--platform linux/amd64
pull a specific architecture
-a
pull every tag
$ docker pull postgres:17

docker rmi

Delete one or more local images. Same as docker image rm.

Syntax: docker rmi [options] image...

-f
force removal even if tagged in several repositories
$ docker rmi node:18

Careful: You will have to pull or rebuild the image again. An image used by a container cannot be removed without -f.

docker tag

Give an image another name or tag, for example before pushing it.

Syntax: docker tag source[:tag] target[:tag]

$ docker tag myapp:latest ghcr.io/acme/myapp:1.4.0

docker history

Show the layers of an image and the instruction that created each.

Syntax: docker history [options] image

--no-trunc
show full commands
-H
human-readable sizes (default)
$ docker history myapp:latest

docker image prune

Delete dangling (untagged) images to free disk space.

Syntax: docker image prune [options]

-a
delete every image not used by a container
-f
do not ask for confirmation
--filter until=24h
only images older than 24 hours
$ docker image prune

Careful: With -a, every image not used by a container is deleted, including base images you will have to pull again.

docker save / load

Export images to a tar file and import them on another machine without a registry.

Syntax: docker save -o file.tar image... · docker load -i file.tar

-o
output file for save
-i
input file for load
$ docker save -o myapp.tar myapp:latest

docker search

Search Docker Hub for images.

Syntax: docker search [options] term

--filter is-official=true
official images only
--limit 10
number of results
$ docker search --filter is-official=true redis

Build

docker build

Build an image from a Dockerfile and a build context folder.

Syntax: docker build [options] path

-t name:tag
name and tag the image
-f path
use a different Dockerfile
--no-cache
ignore the layer cache
--build-arg KEY=value
set a build-time variable
--target stage
stop at a multi-stage build stage
$ docker build -t myapp:latest .

docker buildx build

Build with BuildKit features such as multi-platform images and remote cache.

Syntax: docker buildx build [options] path

--platform linux/amd64,linux/arm64
build for several architectures
--push
push the result to a registry
--load
load the result into local images
$ docker buildx build --platform linux/amd64,linux/arm64 -t acme/myapp:1.0 --push .

docker buildx ls

List builder instances and the platforms they support.

Syntax: docker buildx ls

create --use
create a new builder and make it the default
$ docker buildx ls

docker builder prune

Delete the build cache, which often uses the most disk space.

Syntax: docker builder prune [options]

-a
delete all cache, not just unused
-f
do not ask for confirmation
--filter until=24h
only cache unused for 24 hours
$ docker builder prune

Careful: Your next builds start from scratch and will be slower until the cache is rebuilt.

docker commit

Save a container's current filesystem as a new image. Prefer a Dockerfile for anything you keep.

Syntax: docker commit [options] container image[:tag]

-m
commit message
-c
apply a Dockerfile instruction such as CMD
$ docker commit web web-debug:snapshot

Volumes & files

docker volume create

Create a named volume for data that must outlive containers.

Syntax: docker volume create [name]

--driver
volume driver, default local
$ docker volume create pgdata

docker volume ls

List volumes.

Syntax: docker volume ls [options]

--filter dangling=true
volumes no container uses
-q
names only
$ docker volume ls

docker volume inspect

Show a volume's details, including where its data lives on the host.

Syntax: docker volume inspect volume...

$ docker volume inspect pgdata

docker volume rm

Delete one or more volumes and all the data in them.

Syntax: docker volume rm [-f] volume...

-f
force removal
$ docker volume rm pgdata

Careful: The data in the volume, such as a database, is deleted permanently. Back it up first.

docker volume prune

Delete volumes that no container uses.

Syntax: docker volume prune [options]

-a
include named volumes, not only anonymous ones
-f
do not ask for confirmation
$ docker volume prune

Careful: A stopped-and-removed database container leaves its volume unused, so prune deletes that data. With -a, named volumes are deleted too.

docker run -v

Mount a named volume or a host folder into a container.

Syntax: docker run -v source:/path[:ro] image

pgdata:/var/lib/postgresql/data
named volume
./src:/app
bind-mount a host folder
:ro
mount read-only
--mount
longer, explicit syntax
$ docker run -d -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=secret postgres:17

docker cp

Copy files between a container and the host.

Syntax: docker cp container:path hostpath · docker cp hostpath container:path

-a
keep owner and group
$ docker cp web:/etc/nginx/nginx.conf ./nginx.conf

Careful: Copying into a container overwrites files with the same name without asking.

volume backup

Back up a volume to a tar file using a throwaway container.

Syntax: docker run --rm -v volume:/data -v "$PWD":/backup alpine tar ...

--rm
remove the helper container afterwards
$ docker run --rm -v pgdata:/data -v "$PWD":/backup alpine tar czf /backup/pgdata.tgz -C /data .

Networks & ports

docker network ls

List networks. Default ones are bridge, host and none.

Syntax: docker network ls [options]

-q
IDs only
$ docker network ls

docker network create

Create a user-defined network so containers can reach each other by name.

Syntax: docker network create [options] name

--driver bridge
network driver (default bridge)
--subnet 172.28.0.0/16
choose the subnet
$ docker network create appnet

docker network connect

Attach a running container to a network. disconnect does the opposite.

Syntax: docker network connect network container

--alias db
extra DNS name on that network
$ docker network connect appnet web

docker network inspect

Show a network's subnet, gateway and connected containers.

Syntax: docker network inspect network...

$ docker network inspect appnet

docker network rm

Delete one or more networks.

Syntax: docker network rm network...

prune
docker network prune deletes all unused networks
$ docker network rm appnet

docker run -p

Publish container ports on the host so you can reach them from outside.

Syntax: docker run -p [ip:]host:container[/udp] image

-p 8080:80
host 8080 to container 80
-p 127.0.0.1:5432:5432
only on localhost
-P
publish every exposed port to random host ports
--network host
share the host network (Linux)
$ docker run -d -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD=secret postgres:17

Careful: -p 5432:5432 listens on every host interface and Docker's firewall rules can bypass ufw. Bind to 127.0.0.1 for services that should stay private.

Docker Compose

docker compose up

Create and start every service in compose.yaml.

Syntax: docker compose up [options] [service...]

-d
run in the background
--build
rebuild images first
--force-recreate
recreate containers even if unchanged
--remove-orphans
remove containers for services no longer in the file
$ docker compose up -d --build

docker compose down

Stop and remove the project's containers and networks.

Syntax: docker compose down [options]

-v
also delete named and anonymous volumes
--rmi all
also delete images
--remove-orphans
remove containers not in the file
$ docker compose down

Careful: down -v deletes the project's volumes, including database data. Plain down keeps volumes.

docker compose ps

List the project's containers and their state.

Syntax: docker compose ps [options]

-a
include stopped containers
$ docker compose ps

docker compose logs

Show logs from every service, or one.

Syntax: docker compose logs [options] [service...]

-f
follow
--tail 50
last 50 lines per service
$ docker compose logs -f --tail 50 api

docker compose exec

Run a command in a running service container.

Syntax: docker compose exec [options] service command

-T
no terminal, for scripts and CI
-u
run as a user
$ docker compose exec db psql -U postgres

docker compose run

Run a one-off command in a new container for a service, such as a migration.

Syntax: docker compose run [options] service command

--rm
remove the container afterwards
--no-deps
do not start linked services
$ docker compose run --rm api npm run migrate

docker compose build

Build or rebuild service images.

Syntax: docker compose build [options] [service...]

--no-cache
ignore the layer cache
--pull
pull newer base images
$ docker compose build --pull api

docker compose pull

Pull the latest images for the project's services.

Syntax: docker compose pull [service...]

-q
quiet
$ docker compose pull && docker compose up -d

docker compose restart

Restart services. It does not pick up changes to compose.yaml; use up -d for that.

Syntax: docker compose restart [service...]

-t
shutdown timeout in seconds
$ docker compose restart api

docker compose stop / start

Stop services without removing them, then start them again.

Syntax: docker compose stop [service...] · docker compose start [service...]

$ docker compose stop worker

docker compose config

Validate compose.yaml and print it with variables and overrides merged.

Syntax: docker compose config [options]

--services
list service names
-q
only validate
$ docker compose config

docker compose -f

Choose which compose files to use, for example a base file plus a production override.

Syntax: docker compose -f file [-f file] command

-p name
set the project name
--profile
enable services in a profile
--env-file
use a different .env file
$ docker compose -f compose.yaml -f compose.prod.yaml up -d

docker compose watch

Sync or rebuild services automatically when files change (needs a develop.watch section).

Syntax: docker compose watch [service...]

$ docker compose watch

docker compose up --scale

Run several containers for one service.

Syntax: docker compose up -d --scale service=N

$ docker compose up -d --scale worker=3

Registry & sharing

docker login

Sign in to Docker Hub or another registry.

Syntax: docker login [options] [registry]

-u
username
--password-stdin
read the password or token from stdin
$ echo "$GHCR_TOKEN" | docker login ghcr.io -u acme --password-stdin

Careful: Never pass a password with -p on the command line; it lands in shell history. Use --password-stdin with a token.

docker logout

Remove stored credentials for a registry.

Syntax: docker logout [registry]

$ docker logout ghcr.io

docker push

Upload a tagged image to a registry.

Syntax: docker push image[:tag]

-a
push every tag of the repository
$ docker push ghcr.io/acme/myapp:1.4.0

docker manifest inspect

Show which platforms a remote image supports without pulling it.

Syntax: docker manifest inspect image[:tag]

-v
verbose, with digests per platform
$ docker manifest inspect node:22

Cleanup & system

docker system df

Show how much disk space images, containers, volumes and build cache use.

Syntax: docker system df [-v]

-v
details per image, container and volume
$ docker system df

docker system prune

Free disk space by deleting stopped containers, unused networks, dangling images and build cache in one go.

Syntax: docker system prune [options]

-a
also delete every image not used by a container
--volumes
also delete unused volumes
-f
do not ask for confirmation
$ docker system prune

Careful: Stopped containers are deleted with their data. Adding --volumes deletes unused volumes such as a database you stopped; -a removes images you will have to pull again. Run docker system df first.

docker container prune

Delete every stopped container.

Syntax: docker container prune [options]

-f
do not ask
--filter until=24h
only containers created over 24 hours ago
$ docker container prune --filter until=24h

Careful: Files written inside those containers outside volumes are lost.

remove all containers

Stop and delete every container on this machine.

Syntax: docker rm -f $(docker ps -aq)

-aq
IDs of all containers, running or not
$ docker rm -f $(docker ps -aq)

Careful: This kills and deletes every container, including ones from other projects. List them with docker ps -a first.

docker info

Show daemon details: storage driver, container counts, CPUs, memory and warnings.

Syntax: docker info [--format]

--format
pick one field
$ docker info

docker version

Show the client and server (engine) versions.

Syntax: docker version

--format
pick one field
$ docker version

docker context

Switch which Docker engine the CLI talks to, such as a remote host over SSH.

Syntax: docker context ls | create | use name

create --docker host=ssh://user@host
add a remote engine
use default
switch back to local
$ docker context use default

How to use this Docker command list

  1. 1. Search by command or task

    Type a command such as logs, or a task such as shell, disk space or port.

  2. 2. Filter by category

    Pick a category such as Volumes or Docker Compose to narrow the list.

  3. 3. Read the syntax and flags

    Check the syntax line, the common flags and any Careful note before you run it.

  4. 4. Copy the example

    Copy the example, change the container, image or volume names to yours and run it.

Docker commands FAQ

What are the most used Docker commands?

docker run starts a container, docker ps lists them, docker logs and docker exec help you debug, docker stop and docker rm stop and delete them, docker build and docker pull get images, and docker compose up -d starts a whole multi-container app. Learn these first.

How do I get a shell inside a running container?

Run docker exec -it <container> sh, or bash if the image has it. With Compose, use docker compose exec <service> sh. Leaving the shell does not stop the container.

How do I free disk space used by Docker?

Run docker system df to see what uses space, then docker system prune to delete stopped containers, unused networks, dangling images and build cache. docker builder prune clears only the build cache. Add --volumes only if you are sure no data in unused volumes is needed.

What is the difference between docker compose down and down -v?

docker compose down stops and removes the project's containers and networks but keeps volumes, so database data survives. docker compose down -v also deletes the volumes and their data.

Is it docker-compose or docker compose?

Use docker compose (with a space). It is Compose v2, built into the Docker CLI. The old docker-compose v1 Python tool is no longer supported, though most commands and compose files work the same.

Which Docker commands delete data?

docker rm, docker volume rm, docker volume prune, docker compose down -v, docker system prune --volumes and docker rm -f $(docker ps -aq) can all delete data with no undo. This cheat sheet marks each one with a Careful note.

Automate the container chores you repeat

Rebuilding, migrating, checking logs and pruning the same way every week? Tornic turns those steps into deterministic multi-step workflows that run on the Claude Code, Codex or Cursor CLI you already pay for.

Explore Tornic

Related free tools