Names such as my-container, my-app, app-data, my-network and web are examples, so swap in your own. Commands that use $(...) are for Linux and macOS shells.

Tools for this guide: Docker run to Compose.

Setup and help #

  • Show the Docker client and server versionsLinux, macOS, Windows
    docker version
  • Show system-wide information, such as storage and container countsLinux, macOS, Windows
    docker info
  • Get help for any commandLinux, macOS, Windows
    docker run --help
  • Sign in to Docker HubLinux, macOS, Windows
    docker login
  • Sign in to another registry, such as GitHubLinux, macOS, Windows
    docker login ghcr.io
  • Sign in with a token without it showing in your historyLinux, macOS
    echo "$TOKEN" | docker login ghcr.io -u myuser --password-stdin
  • Sign out of a registryLinux, macOS, Windows
    docker logout

Run containers #

  • Run a container from an imageLinux, macOS, Windows
    docker run nginx
  • Run a container in the backgroundLinux, macOS, Windows
    docker run -d nginx
  • Run a container with a nameLinux, macOS, Windows
    docker run -d --name my-container nginx
  • Publish a container port on the hostLinux, macOS, Windows
    docker run -d -p 8080:80 nginx
  • Publish a port on localhost onlyLinux, macOS, Windows
    docker run -d -p 127.0.0.1:8080:80 nginx
  • Start an interactive shell in a new containerLinux, macOS, Windows
    docker run -it ubuntu bash
  • Run a container and remove it when it stopsLinux, macOS, Windows
    docker run --rm -it alpine sh
  • Set an environment variableLinux, macOS, Windows
    docker run -d -e TZ=Europe/Dublin nginx
  • Load environment variables from a fileLinux, macOS, Windows
    docker run -d --env-file .env my-app
  • Mount a folder from the host, read-onlyLinux, macOS
    docker run -d -v "$(pwd)/site":/usr/share/nginx/html:ro nginx
  • Mount a named volumeLinux, macOS, Windows
    docker run -d -v app-data:/data my-app
  • Restart the container automatically unless you stop itLinux, macOS, Windows
    docker run -d --restart unless-stopped nginx
  • Limit a container's memory and CPULinux, macOS, Windows
    docker run -d --memory 512m --cpus 1.5 nginx
  • Run a container on a specific networkLinux, macOS, Windows
    docker run -d --network my-network nginx
  • Run a container as a specific userLinux, macOS, Windows
    docker run --rm -u 1000:1000 alpine id
  • Override the image's entrypointLinux, macOS, Windows
    docker run --rm -it --entrypoint sh my-app
  • Run an image built for another platformLinux, macOS, Windows
    docker run --rm --platform linux/amd64 alpine uname -m
  • Give a container access to NVIDIA GPUsLinux, Windows
    docker run --rm --gpus all ubuntu nvidia-smi
  • Create a container without starting itLinux, macOS, Windows
    docker create --name my-container nginx

Manage containers #

  • List running containersLinux, macOS, Windows
    docker ps
  • List all containers, including stopped onesLinux, macOS, Windows
    docker ps -a
  • List only container IDsLinux, macOS, Windows
    docker ps -q
  • List containers whose name matchesLinux, macOS, Windows
    docker ps --filter "name=web"
  • List containers with their disk sizeLinux, macOS, Windows
    docker ps -s
  • Stop a containerLinux, macOS, Windows
    docker stop my-container
  • Start a stopped containerLinux, macOS, Windows
    docker start my-container
  • Restart a containerLinux, macOS, Windows
    docker restart my-container
  • Kill a container immediatelyLinux, macOS, Windows
    docker kill my-container

    Warning: The container can't shut down cleanly, so unsaved data may be lost.

  • Pause a containerLinux, macOS, Windows
    docker pause my-container
  • Resume a paused containerLinux, macOS, Windows
    docker unpause my-container
  • Rename a containerLinux, macOS, Windows
    docker rename my-container my-container-old
  • Change a running container's restart policyLinux, macOS, Windows
    docker update --restart unless-stopped my-container
  • Wait for a container to stop and print its exit codeLinux, macOS, Windows
    docker wait my-container
  • Remove a stopped containerLinux, macOS, Windows
    docker rm my-container
  • Remove a container even if it's runningLinux, macOS, Windows
    docker rm -f my-container

    Warning: Stops the container without a clean shutdown and deletes it.

  • Remove all stopped containersLinux, macOS, Windows
    docker container prune

    Warning: Deletes every stopped container and anything not saved to a volume.

  • Stop all running containersLinux, macOS
    docker stop $(docker ps -q)
  • Remove all containersLinux, macOS
    docker rm -f $(docker ps -aq)

    Warning: Deletes every container, running or not. Data not in a volume is lost.

Logs and inspection #

  • Show a container's logsLinux, macOS, Windows
    docker logs my-container
  • Follow a container's logsLinux, macOS, Windows
    docker logs -f my-container
  • Show the last 100 log linesLinux, macOS, Windows
    docker logs --tail 100 my-container
  • Show logs from the last hourLinux, macOS, Windows
    docker logs --since 1h my-container
  • Show logs with timestampsLinux, macOS, Windows
    docker logs -t my-container
  • Watch live CPU, memory and network useLinux, macOS, Windows
    docker stats
  • Show resource use once, without updatingLinux, macOS, Windows
    docker stats --no-stream
  • Show the processes running in a containerLinux, macOS, Windows
    docker top my-container
  • Show all of a container's settingsLinux, macOS, Windows
    docker inspect my-container
  • Get a container's IP addressLinux, macOS, Windows
    docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" my-container
  • Show a container's health check statusLinux, macOS, Windows
    docker inspect -f "{{.State.Health.Status}}" my-container
  • Show a container's exit codeLinux, macOS, Windows
    docker inspect -f "{{.State.ExitCode}}" my-container
  • Show a container's published portsLinux, macOS, Windows
    docker port my-container
  • Show files changed inside a containerLinux, macOS, Windows
    docker diff my-container
  • Watch Docker events as they happenLinux, macOS, Windows
    docker events

Shells and files #

  • Open a shell inside a containerLinux, macOS, Windows
    docker exec -it my-container sh
  • Open a Bash shell inside a containerLinux, macOS, Windows
    docker exec -it my-container bash
  • Open a shell inside a container as rootLinux, macOS, Windows
    docker exec -it -u root my-container sh
  • Run a single command inside a containerLinux, macOS, Windows
    docker exec my-container ls /app
  • Run a command with an extra environment variableLinux, macOS, Windows
    docker exec -e DEBUG=1 my-container env
  • Copy a file out of a containerLinux, macOS, Windows
    docker cp my-container:/app/config.yml ./config.yml
  • Copy a file into a containerLinux, macOS, Windows
    docker cp ./config.yml my-container:/app/config.yml
  • Attach to a container's main process (detach with Ctrl+P, Ctrl+Q)Linux, macOS, Windows
    docker attach my-container

Images #

  • List imagesLinux, macOS, Windows
    docker images
  • Download an imageLinux, macOS, Windows
    docker pull nginx
  • Download a specific image versionLinux, macOS, Windows
    docker pull nginx:1.27-alpine
  • Download an image for another platformLinux, macOS, Windows
    docker pull --platform linux/arm64 nginx
  • Tag an image with a new nameLinux, macOS, Windows
    docker tag my-app:latest myuser/my-app:1.0
  • Upload an image to a registryLinux, macOS, Windows
    docker push myuser/my-app:1.0
  • Search Docker Hub for imagesLinux, macOS, Windows
    docker search nginx
  • Show an image's layers and how it was builtLinux, macOS, Windows
    docker history nginx
  • Show all of an image's settingsLinux, macOS, Windows
    docker image inspect nginx
  • Scan an image for known vulnerabilitiesLinux, macOS, Windows
    docker scout cves nginx
  • Save an image to a fileLinux, macOS, Windows
    docker save -o my-app.tar my-app:latest
  • Load an image from a fileLinux, macOS, Windows
    docker load -i my-app.tar
  • Save a container's changes as a new imageLinux, macOS, Windows
    docker commit my-container my-app:snapshot
  • Export a container's filesystem to a fileLinux, macOS, Windows
    docker export -o my-container.tar my-container
  • Create an image from an exported filesystemLinux, macOS, Windows
    docker import my-container.tar my-app:imported
  • Remove an imageLinux, macOS, Windows
    docker rmi nginx
  • Remove untagged (dangling) imagesLinux, macOS, Windows
    docker image prune
  • Remove all images no container is usingLinux, macOS, Windows
    docker image prune -a

    Warning: Deletes every image not used by a container. You'll need to download or build them again.

Build images #

  • Create a Dockerfile and Compose file for your projectLinux, macOS, Windows
    docker init
  • Build an image from the Dockerfile in this folderLinux, macOS, Windows
    docker build -t my-app .
  • Build with a different DockerfileLinux, macOS, Windows
    docker build -f Dockerfile.prod -t my-app:prod .
  • Build without using the cacheLinux, macOS, Windows
    docker build --no-cache -t my-app .
  • Build with the newest version of the base imageLinux, macOS, Windows
    docker build --pull -t my-app .
  • Pass a build argumentLinux, macOS, Windows
    docker build --build-arg NODE_VERSION=22 -t my-app .
  • Build only up to one stage of a multi-stage DockerfileLinux, macOS, Windows
    docker build --target build -t my-app:build .
  • Show the full build outputLinux, macOS, Windows
    docker build --progress=plain -t my-app .
  • Create a builder for multi-platform imagesLinux, macOS, Windows
    docker buildx create --name multi --use
  • List buildersLinux, macOS, Windows
    docker buildx ls
  • Build for Intel and ARM and push to a registryLinux, macOS, Windows
    docker buildx build --platform linux/amd64,linux/arm64 -t myuser/my-app:1.0 --push .
  • Clear the build cacheLinux, macOS, Windows
    docker builder prune

Volumes #

  • List volumesLinux, macOS, Windows
    docker volume ls
  • Create a volumeLinux, macOS, Windows
    docker volume create app-data
  • Show a volume's details, including where it's storedLinux, macOS, Windows
    docker volume inspect app-data
  • Back up a volume to a fileLinux, macOS
    docker run --rm -v app-data:/data -v "$(pwd)":/backup alpine tar czf /backup/app-data.tar.gz -C /data .
  • Restore a volume from a backupLinux, macOS
    docker run --rm -v app-data:/data -v "$(pwd)":/backup alpine tar xzf /backup/app-data.tar.gz -C /data

    Warning: Overwrites files in the volume with the ones in the backup.

  • Remove a volumeLinux, macOS, Windows
    docker volume rm app-data

    Warning: Deletes the data in the volume and can't be undone.

  • Remove unused anonymous volumesLinux, macOS, Windows
    docker volume prune

    Warning: Deletes the data in those volumes and can't be undone.

  • Remove all unused volumes, including named onesLinux, macOS, Windows
    docker volume prune -a

    Warning: Deletes the data in every volume not used by a container, and can't be undone.

Networks #

  • List networksLinux, macOS, Windows
    docker network ls
  • Create a networkLinux, macOS, Windows
    docker network create my-network
  • Show a network's details and connected containersLinux, macOS, Windows
    docker network inspect my-network
  • Connect a container to a networkLinux, macOS, Windows
    docker network connect my-network my-container
  • Disconnect a container from a networkLinux, macOS, Windows
    docker network disconnect my-network my-container
  • Use the host's network directlyLinux
    docker run -d --network host nginx
  • Give containers their own IP addresses on your LAN (macvlan)Linux
    docker network create -d macvlan --subnet 192.168.1.0/24 --gateway 192.168.1.1 -o parent=eth0 lan
  • Remove a networkLinux, macOS, Windows
    docker network rm my-network
  • Remove unused networksLinux, macOS, Windows
    docker network prune

Docker Compose #

  • Start a Compose stack in the backgroundLinux, macOS, Windows
    docker compose up -d
  • Start a Compose stack and show its outputLinux, macOS, Windows
    docker compose up
  • Rebuild images and start a Compose stackLinux, macOS, Windows
    docker compose up -d --build
  • Use a different Compose fileLinux, macOS, Windows
    docker compose -f compose.prod.yaml up -d
  • Run more copies of a serviceLinux, macOS, Windows
    docker compose up -d --scale worker=3
  • Remove containers for services no longer in the fileLinux, macOS, Windows
    docker compose up -d --remove-orphans
  • Stop and remove a Compose stackLinux, macOS, Windows
    docker compose down
  • Stop and remove a Compose stack and its volumesLinux, macOS, Windows
    docker compose down -v

    Warning: Deletes the stack's volumes and the data in them.

  • Stop a Compose stack without removing itLinux, macOS, Windows
    docker compose stop
  • Start a stopped Compose stackLinux, macOS, Windows
    docker compose start
  • Restart one serviceLinux, macOS, Windows
    docker compose restart web
  • List a stack's services and their statusLinux, macOS, Windows
    docker compose ps
  • List all running Compose projectsLinux, macOS, Windows
    docker compose ls
  • Follow logs for every serviceLinux, macOS, Windows
    docker compose logs -f
  • Follow logs for one serviceLinux, macOS, Windows
    docker compose logs -f web
  • Open a shell in a running serviceLinux, macOS, Windows
    docker compose exec web sh
  • Run a one-off command in a new service containerLinux, macOS, Windows
    docker compose run --rm web npm test
  • Show the processes in each serviceLinux, macOS, Windows
    docker compose top
  • Download the latest images for a stackLinux, macOS, Windows
    docker compose pull
  • Update a stack to its latest imagesLinux, macOS
    docker compose pull && docker compose up -d
  • Build a stack's imagesLinux, macOS, Windows
    docker compose build
  • Check the Compose file and show the final settingsLinux, macOS, Windows
    docker compose config
  • Rebuild and restart services when files changeLinux, macOS, Windows
    docker compose watch

Disk space and clean-up #

  • Show how much disk space Docker is usingLinux, macOS, Windows
    docker system df
  • Show disk use for each image, container and volumeLinux, macOS, Windows
    docker system df -v
  • Remove stopped containers, unused networks, dangling images and build cacheLinux, macOS, Windows
    docker system prune

    Warning: Deletes what it lists and can't be undone.

  • Remove everything Docker isn't using, including images and volumesLinux, macOS, Windows
    docker system prune -a --volumes

    Warning: Deletes all unused images and volume data, and can't be undone.

Remote hosts and contexts #

  • List Docker contextsLinux, macOS, Windows
    docker context ls
  • Add a remote Docker host over SSHLinux, macOS, Windows
    docker context create homelab --docker "host=ssh://[email protected]"
  • Switch to another Docker hostLinux, macOS, Windows
    docker context use homelab
  • Switch back to your local DockerLinux, macOS, Windows
    docker context use default
  • Run one command on a remote Docker hostLinux, macOS, Windows
    docker -H ssh://[email protected] ps

Swarm #

  • Turn this machine into a Swarm managerLinux, macOS, Windows
    docker swarm init
  • Show the command for joining a worker to the SwarmLinux, macOS, Windows
    docker swarm join-token worker
  • List the nodes in the SwarmLinux, macOS, Windows
    docker node ls
  • Create a service with 3 replicasLinux, macOS, Windows
    docker service create --name web --replicas 3 -p 8080:80 nginx
  • List servicesLinux, macOS, Windows
    docker service ls
  • Show where a service's tasks are runningLinux, macOS, Windows
    docker service ps web
  • Change how many replicas a service runsLinux, macOS, Windows
    docker service scale web=5
  • Update a service to a new imageLinux, macOS, Windows
    docker service update --image nginx:1.27 web
  • Follow a service's logsLinux, macOS, Windows
    docker service logs -f web
  • Deploy a Compose file as a stackLinux, macOS, Windows
    docker stack deploy -c compose.yaml my-stack
  • Remove a stackLinux, macOS, Windows
    docker stack rm my-stack
  • Leave the SwarmLinux, macOS, Windows
    docker swarm leave --force

    Warning: On a manager, this can break the Swarm if it's the last one.