Skip to content
xt-tools

Docker cheatsheet

Searchable reference of Docker and Docker Compose commands for images, containers, volumes and networks.

149 of 149 commands

Docker commands
SectionCommandDescription
Rundocker run <image>Create and start a container from an image, pulling it if needed.
Rundocker run -it --rm <image> shInteractive shell in a throwaway container, removed on exit.
Rundocker run -d --name <name> <image>Run in the background with a fixed name.
Rundocker run -p 8080:80 <image>Publish container port 80 on host port 8080 (all interfaces).
Rundocker run -p 127.0.0.1:8080:80 <image>Publish a port on localhost only.
Rundocker run -P <image>Publish every EXPOSEd port on a random host port.
Rundocker run -e KEY=value <image>Set an environment variable.
Rundocker run --env-file .env <image>Load environment variables from a file.
Rundocker run -v <volume>:/data <image>Mount a named volume at /data (created if missing).
Rundocker run -v "$PWD":/app -w /app <image>Bind-mount the current directory and make it the working directory.
Rundocker run --mount type=bind,src=<host-path>,dst=<path>,readonly <image>Explicit bind mount; fails instead of creating the host path if it is missing.
Rundocker run --tmpfs /tmp <image>Mount an in-memory filesystem.
Rundocker run --network <network> <image>Attach the container to a network.
Rundocker run --restart unless-stopped <image>Restart after crashes and daemon restarts unless stopped by hand.
Rundocker run --user 1000:1000 <image>Run as a specific UID:GID instead of the image default.
Rundocker run --read-only --cap-drop ALL --security-opt no-new-privileges <image>Hardened run: read-only root filesystem, no capabilities, no privilege escalation.
Rundocker run --memory 512m --cpus 1.5 <image>Limit memory and CPU.
Rundocker run --entrypoint sh <image>Override the image ENTRYPOINT.
Rundocker run --platform linux/arm64 <image>Run an image built for another platform (needs emulation).
Rundocker run --init <image>Run a tiny init as PID 1 to forward signals and reap zombies.
Rundocker run --gpus all <image>Give the container access to all GPUs (NVIDIA Container Toolkit).
Rundocker run --add-host host.docker.internal:host-gateway <image>Let a container on Linux reach the host as host.docker.internal.
Containersdocker psList running containers.
Containersdocker ps -aList all containers, including stopped ones.
Containersdocker ps -qPrint only container IDs, handy for scripting.
Containersdocker ps --filter status=exitedList containers that have exited.
Containersdocker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"Custom columns with a Go template.
Containersdocker start <container>Start a stopped container.
Containersdocker stop <container>Stop gracefully: SIGTERM, then SIGKILL after 10 s.
Containersdocker restart <container>Stop and start a container.
Containersdocker kill <container>Stop immediately with SIGKILL (or --signal).
Containersdocker pause <container>Freeze all processes in a container; resume with docker unpause.
Containersdocker rm <container>Remove a stopped container.
Containersdocker rm -f <container>Force-remove a container, stopping it first.
Containersdocker rename <old> <new>Rename a container.
Containersdocker inspect <container>Full JSON configuration and state.
Containersdocker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" <container>Print the container IP addresses.
Containersdocker port <container>Show port mappings.
Containersdocker statsLive CPU, memory, network and disk usage per container.
Containersdocker top <container>List the processes running in a container.
Containersdocker cp <container>:<path> <host-path>Copy files out of a container.
Containersdocker cp <host-path> <container>:<path>Copy files into a container.
Containersdocker diff <container>Files added (A), changed (C) or deleted (D) since the container started.
Containersdocker update --restart no <container>Change restart policy or resource limits of a container.
Containersdocker wait <container>Block until a container stops, then print its exit code.
Containersdocker export -o <file>.tar <container>Export the container filesystem as a tar archive.
Exec & logsdocker exec -it <container> shOpen a shell in a running container.
Exec & logsdocker exec -it -u root <container> shOpen a shell as root.
Exec & logsdocker exec -e KEY=value <container> <command>Run a command with an extra environment variable.
Exec & logsdocker exec -w /app <container> <command>Run a command in a given working directory.
Exec & logsdocker logs <container>Print the container output (stdout and stderr).
Exec & logsdocker logs -f <container>Follow the log output.
Exec & logsdocker logs -f --tail 100 <container>Follow, starting from the last 100 lines.
Exec & logsdocker logs --since 10m <container>Logs from the last 10 minutes (or a timestamp).
Exec & logsdocker logs -t <container>Prefix each line with a timestamp.
Exec & logsdocker attach <container>Attach to the main process; detach with Ctrl-p Ctrl-q.
Imagesdocker imagesList local images (same as docker image ls).
Imagesdocker image ls --filter dangling=trueList untagged (dangling) images.
Imagesdocker pull <image>:<tag>Download an image from a registry.
Imagesdocker pull --platform linux/amd64 <image>Pull the variant for a specific platform.
Imagesdocker tag <image> <registry>/<repo>:<tag>Add a new name to an image, e.g. before pushing.
Imagesdocker rmi <image>Remove an image (same as docker image rm).
Imagesdocker image inspect <image>JSON details: layers, env, entrypoint, labels.
Imagesdocker history <image>Show the layers of an image and the instruction that made each one.
Imagesdocker save -o <file>.tar <image>Save one or more images to a tar archive.
Imagesdocker load -i <file>.tarLoad images from a tar archive.
Imagesdocker commit <container> <image>Create an image from a container’s changes (prefer a Dockerfile).
Imagesdocker search <term>Search Docker Hub for images.
Builddocker build -t <name>:<tag> .Build an image from the Dockerfile in the current directory.
Builddocker build -f <path>/Dockerfile .Use a Dockerfile in another location.
Builddocker build --build-arg KEY=value .Set a build-time ARG.
Builddocker build --target <stage> .Build only up to a stage of a multi-stage Dockerfile.
Builddocker build --no-cache .Rebuild every layer, ignoring the cache.
Builddocker build --pull .Always pull a newer version of the base images.
Builddocker build --progress=plain .Plain build output, showing the full output of every step.
Builddocker build --secret id=<id>,src=<file> .Pass a secret file to RUN --mount=type=secret,id=<id> without storing it in a layer.
Builddocker build --ssh default .Forward your SSH agent to RUN --mount=type=ssh steps.
Builddocker build -o <dir> .Export the build result as files to a directory instead of an image.
Builddocker build --check .Run the Dockerfile build checks without building.
Builddocker buildx build --platform linux/amd64,linux/arm64 -t <image> --push .Build a multi-platform image and push it.
Builddocker buildx create --name <builder> --useCreate a docker-container builder and make it the default.
Builddocker buildx lsList builders and the platforms they support.
Builddocker buildx bakeBuild several targets defined in docker-bake.hcl or a Compose file.
Builddocker buildx duShow build cache disk usage.
Builddocker builder pruneRemove the build cache.
Registrydocker loginLog in to Docker Hub.
Registrydocker login <registry>Log in to another registry, e.g. ghcr.io.
Registryecho "$TOKEN" | docker login -u <user> --password-stdin <registry>Log in without the token in shell history.
Registrydocker logout <registry>Remove stored credentials.
Registrydocker push <registry>/<repo>:<tag>Upload an image to a registry.
Registrydocker buildx imagetools inspect <image>Show the manifest list and platforms of a remote image.
Registrydocker scout cves <image>List known vulnerabilities in an image (Docker Scout plugin).
Volumesdocker volume create <name>Create a named volume.
Volumesdocker volume lsList volumes.
Volumesdocker volume inspect <name>Show a volume’s driver, mount point and labels.
Volumesdocker volume rm <name>Remove a volume that no container uses.
Volumesdocker volume pruneRemove unused anonymous volumes.
Volumesdocker volume prune -aRemove all unused volumes, named ones included.
Volumesdocker run --rm -v <volume>:/data -v "$PWD":/backup alpine tar czf /backup/backup.tgz -C /data .Back up a volume to a tarball in the current directory.
Networksdocker network lsList networks.
Networksdocker network create <name>Create a user-defined bridge network; containers on it resolve each other by name.
Networksdocker network create --subnet 172.28.0.0/16 <name>Create a network with a fixed subnet.
Networksdocker network connect <network> <container>Attach a running container to a network.
Networksdocker network connect --alias <alias> <network> <container>Attach with an extra DNS name.
Networksdocker network disconnect <network> <container>Detach a container from a network.
Networksdocker network inspect <network>Show the subnet and connected containers.
Networksdocker network rm <network>Remove a network.
Networksdocker network pruneRemove all unused networks.
Networksdocker run --network host <image>Share the host network stack (no port publishing needed).
Networksdocker run --network none <image>Run with networking disabled.
System & cleanupdocker versionClient and server versions.
System & cleanupdocker infoDaemon configuration: storage driver, cgroups, number of containers.
System & cleanupdocker system dfDisk used by images, containers, volumes and build cache.
System & cleanupdocker system df -vDisk usage per image, container and volume.
System & cleanupdocker system pruneRemove stopped containers, unused networks, dangling images and build cache.
System & cleanupdocker system prune -a --volumesAlso remove every unused image and anonymous volume. Destructive.
System & cleanupdocker container pruneRemove all stopped containers.
System & cleanupdocker image pruneRemove dangling images.
System & cleanupdocker image prune -aRemove every image not used by a container.
System & cleanupdocker image prune -a --filter "until=168h"Remove unused images older than a week.
System & cleanupdocker eventsStream real-time events from the daemon.
System & cleanupdocker context lsList contexts (local and remote Docker endpoints).
System & cleanupdocker context create <name> --docker host=ssh://<user>@<host>Add a remote Docker host reached over SSH.
System & cleanupdocker context use <name>Send subsequent commands to another Docker endpoint.
Composedocker compose up -dCreate and start every service in the background.
Composedocker compose up -d --buildRebuild images before starting.
Composedocker compose up -d <service>Start one service and its dependencies.
Composedocker compose up -d --force-recreateRecreate containers even if their configuration is unchanged.
Composedocker compose up --waitStart and wait until services are running or healthy.
Composedocker compose up -d --scale <service>=3Run three replicas of a service.
Composedocker compose downStop and remove containers and networks.
Composedocker compose down -vAlso remove named and anonymous volumes. Deletes data.
Composedocker compose psList the project’s containers.
Composedocker compose logs -f <service>Follow the logs of a service.
Composedocker compose exec <service> shOpen a shell in a running service container.
Composedocker compose run --rm <service> <command>Run a one-off command in a new container.
Composedocker compose buildBuild or rebuild service images.
Composedocker compose pullPull the latest images for every service.
Composedocker compose restart <service>Restart a service (does not apply config changes).
Composedocker compose stopStop services without removing them.
Composedocker compose configPrint the merged, interpolated configuration.
Composedocker compose -f compose.yaml -f compose.prod.yaml up -dMerge several Compose files, later ones win.
Composedocker compose -p <project> up -dUse a specific project name.
Composedocker compose --profile <profile> up -dAlso start services assigned to a profile.
Composedocker compose --env-file <file> up -dUse another file for variable interpolation.
Composedocker compose watchSync or rebuild services when files change (develop.watch).
Composedocker compose lsList running Compose projects.
Composedocker compose topShow the processes of every service.
Composedocker compose cp <service>:<path> <host-path>Copy files out of a service container.