Cheat sheets /

Docker cheat sheet

Run it, get a shell in it, read its logs, build it, push it, and reclaim your disk afterwards. Docker and Docker Compose commands for every day.

Download all 35 for Snippet Keeper Get Snippet Keeper

In Snippet Keeper, click Import at the bottom of the sidebar and choose the file. They land in your docker app, and ones you already have are skipped. The highlighted parts become fill-in fields: type a value (or pick one), and the finished command is on your clipboard.

Containers

Run a throwaway container with a shell

docker run --rm -it image shell

Run in the background with a port

docker run -d --name name -p host_port:container_port image

Run with the current folder mounted

docker run --rm -it -v "$(pwd)":/app -w /app image shell

Run with an env file

docker run --rm --env-file env_file image

List running containers

docker ps

List all containers

docker ps -a

Shell into a running container

docker exec -it container shell

Follow a container’s logs

docker logs -f --tail lines container

Stop and remove a container

docker rm -f container

Copy a file out of a container

docker cp container:path dest

Live CPU and memory use

docker stats

Full details of a container

docker inspect container

Images

Build and tag an image

docker build -t name:tag .

Build for another platform

For example, building an x86 image on an Apple silicon Mac.

docker buildx build --platform platform -t name:tag .

Build without the cache

docker build --no-cache -t name:tag .

List images

docker images

Pull an image

docker pull image

Tag and push to a registry

docker tag image registry/image && docker push registry/image

Log in to a registry

docker login registry

See how an image’s layers were built

docker history image

Cleanup

How much space Docker is using

docker system df

Remove stopped containers

docker container prune -f

Remove dangling images

docker image prune -f

Remove everything unused

Deletes stopped containers, unused images, networks and volumes. Volume data is gone for good.

docker system prune -af --volumes

Volumes and networks

List volumes

docker volume ls

Create a network

docker network create network

Run a container on a network

Containers on the same network reach each other by name.

docker run -d --network network --name name image

Compose

Start everything in the background

docker compose up -d

Rebuild and start

docker compose up -d --build

Stop and remove, including volumes

docker compose down -v

List services

docker compose ps

Follow a service’s logs

docker compose logs -f service

Shell into a service

docker compose exec service shell

Run a one-off command

docker compose run --rm service command

Restart a service

docker compose restart service

Keep these one keystroke away.

Snippet Keeper is a Mac app for the commands you keep looking up. Press ⌥⇧Space in any app, type a few letters, fill in the blanks, and it’s on your clipboard.

Snippet Keeper asking for the values in a kubectl command before copying it