Docker Cheat Sheet

Build, run, ship, and debug container images — the day-to-day lifecycle every image spends in CI and production.

Containers7 sections · 61 commands
⌘K

Images

13
docker build -t NAME:TAG .

Build an image from the Dockerfile

docker build --no-cache -t NAME .

Build bypassing the cache

docker pull IMAGE

Pull an image from a registry

docker push USER/IMAGE:TAG

Push to a registry

docker images

List local images

docker image inspect IMAGE

Full metadata as JSON

docker image prune -a

Remove all unused images

docker rmi IMAGE

Tag or remove an image

docker history IMAGE

Layers a build produced

docker tag IMAGE NEWNAME:TAG

Re-tag an image

docker save IMAGE | gzip > image.tar.gz

Export an image to a tarball

docker load < image.tar.gz

Import images from a tarball

docker build --target BUILDER -t NAME .

Build up to a specific stage (multi-stage)

Containers

16
docker run IMAGE

Run a container

docker run --name web -d -p 8080:80 IMAGE

Detached, named, port-mapped

docker run --rm IMAGE

Remove the container on exit

docker run -it IMAGE /bin/sh

Interactive shell

docker run -v /host:/container IMAGE

Mount a host directory

docker run --env VAR=value IMAGE

Set an environment variable

docker run --env-file .env IMAGE

Load env from a file

docker run --network host IMAGE

Use the host network

docker run --restart unless-stopped IMAGE

Restart policy

docker ps

Running containers

docker ps -a

All containers

docker start/stop/restart CONTAINER

Control a container

docker rm CONTAINER

Remove a stopped container

docker rm -f CONTAINER

Force-remove a running container

docker container prune

Remove all stopped containers

docker rename OLD NEW

Rename a container

Shell, Logs & Debug

10
docker exec -it CONTAINER /bin/sh

Open a shell inside a container

docker exec CONTAINER CMD

Run one command inside

docker logs CONTAINER

STDOUT/STDERR of the container

docker logs -f --tail 100 CONTAINER

Follow the last 100 lines

docker logs CONTAINER 2>&1 | grep error

Scan logs for errors

docker top CONTAINER

Processes inside the container

docker diff CONTAINER

Which files the container changed

docker cp CONTAINER:/path/to/file .

Copy a file out of a container

docker stat CONTAINER

Live CPU/memory/network usage

docker inspect CONTAINER | grep IPAddress

Read the container IP

Networks

6
docker network ls

List networks

docker network create NAME

Create a network

docker network connect NET CONTAINER

Attach a running container

docker network disconnect NET CONTAINER

Detach a container

docker run --network NET IMAGE

Run on a custom network

docker port CONTAINER

Show published ports

Volumes & Storage

6
docker volume ls

List named volumes

docker volume create NAME

Create a named volume

docker run -v NAME:/data IMAGE

Use a named volume

docker run --mount type=bind,src=/host,dst=/container IMAGE

Mount with build-args syntax

docker volume prune

Remove unused volumes

docker volume inspect NAME

Where the volume lives

Cleanup & Housekeeping

5
docker system df

Disk usage by images/containers/volumes

docker system prune

Remove dangling images and stopped containers

docker system prune -a --volumes

Full cleanup (careful)

docker image prune -f

Drop dangling images only

docker events

Stream live engine events

Registry & Auth

5
docker login

Authenticate to a registry

docker logout

Clear registry credentials

docker search TERM

Search the registry

docker manifest inspect IMAGE

Inspect tags and platforms

docker run --platform linux/amd64 IMAGE

Force an architecture