Docker Cheatsheet
135+ Docker commands and Dockerfile instructions — searchable by keyword or category. Click Copy to grab any command.
docker run <image>ContainersCreate and start a container from an image.
docker run -d <image>ContainersRun container in detached (background) mode.
docker run -it <image> bashContainersRun container interactively with a bash shell.
docker run --name <name> <image>ContainersAssign a custom name to the container.
docker run -p 8080:80 <image>ContainersMap host port 8080 to container port 80.
docker run -e KEY=value <image>ContainersSet an environment variable inside the container.
docker run -v /host:/container <image>ContainersBind-mount a host directory into the container.
docker run --rm <image>ContainersAutomatically remove container when it exits.
docker run --network <net> <image>ContainersConnect the container to a specific network.
docker run --restart always <image>ContainersAlways restart the container if it stops.
docker run --memory 512m <image>ContainersLimit the container memory to 512 MB.
docker run --cpus 1.5 <image>ContainersLimit the container to 1.5 CPU cores.
docker psContainersList all running containers.
docker ps -aContainersList all containers including stopped ones.
docker ps -qContainersList only container IDs (running).
docker start <container>ContainersStart a stopped container.
docker stop <container>ContainersGracefully stop a running container (SIGTERM).
docker kill <container>ContainersForce-stop a container immediately (SIGKILL).
docker restart <container>ContainersStop then start a container.
docker rm <container>ContainersRemove a stopped container.
docker rm -f <container>ContainersForce-remove a running container.
docker rm $(docker ps -aq)ContainersRemove all stopped containers.
docker exec -it <container> bashContainersOpen an interactive shell in a running container.
docker exec <container> <cmd>ContainersRun a command inside a running container.
docker logs <container>ContainersFetch logs from a container.
docker logs -f <container>ContainersFollow (tail) container logs in real time.
docker logs --tail 100 <container>ContainersShow the last 100 lines of container logs.
docker inspect <container>ContainersReturn low-level JSON info about a container.
docker statsContainersLive resource usage stats for all running containers.
docker top <container>ContainersShow processes running inside a container.
docker cp <container>:/path ./localContainersCopy a file from a container to the host.
docker cp ./local <container>:/pathContainersCopy a file from the host into a container.
docker pause <container>ContainersPause all processes in a container (SIGSTOP).
docker unpause <container>ContainersUnpause a paused container.
docker rename <old> <new>ContainersRename a container.
docker update --memory 1g <container>ContainersUpdate resource limits of a running container.
docker wait <container>ContainersBlock until a container stops, then print exit code.
docker diff <container>ContainersInspect file changes in a container's filesystem.
docker commit <container> <image:tag>ContainersCreate a new image from a container's state.
docker port <container>ContainersList port mappings for a container.
docker imagesImagesList all locally available images.
docker images -aImagesList all images including intermediate layers.
docker pull <image>ImagesDownload an image from a registry.
docker pull <image>:<tag>ImagesPull a specific tagged version of an image.
docker push <image>:<tag>ImagesPush an image to a registry.
docker build -t <name>:<tag> .ImagesBuild an image from the Dockerfile in the current directory.
docker build -f <Dockerfile> -t <name> .ImagesBuild using a custom Dockerfile path.
docker build --no-cache -t <name> .ImagesBuild without using the layer cache.
docker build --build-arg KEY=val -t <n> .ImagesPass a build-time argument to the Dockerfile.
docker rmi <image>ImagesRemove a local image.
docker rmi -f <image>ImagesForce-remove an image even if in use.
docker rmi $(docker images -q)ImagesRemove all local images.
docker tag <image> <new-name>:<tag>ImagesTag an image with a new name/tag.
docker image inspect <image>ImagesDisplay detailed information about an image.
docker image history <image>ImagesShow the layer build history of an image.
docker image pruneImagesRemove all dangling (untagged) images.
docker image prune -aImagesRemove all unused images (not referenced by any container).
docker save -o out.tar <image>ImagesExport an image to a tar archive file.
docker load -i out.tarImagesImport an image from a tar archive.
docker search <term>ImagesSearch Docker Hub for images matching a term.
docker volume lsVolumesList all volumes.
docker volume create <name>VolumesCreate a named volume.
docker volume inspect <name>VolumesDisplay detailed info about a volume.
docker volume rm <name>VolumesRemove a volume.
docker volume pruneVolumesRemove all unused volumes.
docker run -v <name>:/data <image>VolumesMount a named volume into a container at /data.
docker run -v $(pwd):/app <image>VolumesBind-mount the current directory to /app in the container.
docker run --mount type=bind,src=.,dst=/app <image>VolumesBind-mount using the explicit --mount syntax.
docker run --volumes-from <container> <image>VolumesMount all volumes from another container.
docker run --tmpfs /tmp <image>VolumesMount a temporary in-memory filesystem at /tmp.
docker network lsNetworksList all networks.
docker network create <name>NetworksCreate a bridge network.
docker network create --driver overlay <n>NetworksCreate an overlay network (Swarm-compatible).
docker network inspect <name>NetworksShow detailed info about a network.
docker network rm <name>NetworksRemove a network.
docker network pruneNetworksRemove all unused networks.
docker network connect <net> <container>NetworksConnect a running container to a network.
docker network disconnect <net> <container>NetworksDisconnect a container from a network.
docker run --network host <image>NetworksRun container using the host network stack directly.
docker run --network none <image>NetworksRun container with no network access.
docker compose upComposeStart all services defined in docker-compose.yml.
docker compose up -dComposeStart all services in detached mode.
docker compose up --buildComposeRebuild images before starting services.
docker compose up --scale web=3ComposeScale a service to 3 replicas.
docker compose downComposeStop and remove containers, networks created by up.
docker compose down -vComposeAlso remove named volumes when bringing down.
docker compose down --rmi allComposeRemove images used by services on down.
docker compose psComposeList containers for the current Compose project.
docker compose logsComposeView output from all services.
docker compose logs -f <service>ComposeFollow logs for a specific service.
docker compose exec <service> bashComposeOpen a shell in a running service container.
docker compose run --rm <service> <cmd>ComposeRun a one-off command in a service container.
docker compose buildComposeBuild or rebuild service images.
docker compose pullComposePull latest images for all services.
docker compose stopComposeStop running services without removing containers.
docker compose restart <service>ComposeRestart a specific service.
docker compose configComposeValidate and print the resolved Compose configuration.
docker compose -f custom.yml upComposeUse a custom Compose file instead of the default.
docker loginRegistryLog in to Docker Hub (prompts for credentials).
docker login <registry>RegistryLog in to a private registry.
docker logoutRegistryLog out from Docker Hub.
docker push <user>/<image>:<tag>RegistryPush an image to Docker Hub.
docker pull <registry>/<image>:<tag>RegistryPull an image from a private registry.
docker tag <image> <registry>/<image>:<tag>RegistryTag an image for a private registry.
docker buildx build --platform linux/amd64,linux/arm64 -t <name> --push .RegistryBuild and push a multi-platform image.
docker manifest inspect <image>:<tag>RegistryInspect the manifest of a multi-platform image.
docker infoSystemDisplay Docker system-wide information.
docker versionSystemShow Docker client and daemon version details.
docker system dfSystemShow disk usage by images, containers, and volumes.
docker system pruneSystemRemove all stopped containers, unused networks and dangling images.
docker system prune -aSystemAlso remove all unused images (not just dangling).
docker system prune --volumesSystemAlso remove unused volumes during prune.
docker eventsSystemStream real-time events from the Docker daemon.
docker events --filter type=containerSystemFilter daemon events by resource type.
docker context lsSystemList all Docker contexts (local, remote, etc.).
docker context use <name>SystemSwitch to a different Docker context.
docker buildx lsSystemList available Buildx builder instances.
docker buildx create --useSystemCreate and use a new Buildx builder.
FROMSet the base image for subsequent instructions. Must be the first instruction (except ARG).
FROM node:20-alpineRUNExecute a command during the image build and commit the result as a new layer.
RUN apt-get update && apt-get install -y curlCOPYCopy files or directories from the build context into the image filesystem.
COPY package*.json ./ADDLike COPY, but also supports URLs and auto-extracts tar archives. Prefer COPY when possible.
ADD https://example.com/file.tar.gz /tmp/WORKDIRSet the working directory for RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow.
WORKDIR /appENVSet environment variables that persist into the running container.
ENV NODE_ENV=production PORT=3000ARGDefine a build-time variable that can be passed with --build-arg. Does not persist in the final image.
ARG VERSION=1.0.0EXPOSEDocument which port the container listens on at runtime (informational; does not publish).
EXPOSE 8080CMDProvide default command/args for the container. Overridden by docker run arguments. Only last CMD applies.
CMD ["node", "server.js"]ENTRYPOINTConfigure a container to run as an executable. CMD arguments are appended. Harder to override than CMD.
ENTRYPOINT ["python", "app.py"]VOLUMECreate a mount point and mark it as holding externally-mounted volumes from the host or other containers.
VOLUME ["/data"]USERSet the user (and optionally group) for subsequent RUN, CMD, and ENTRYPOINT instructions.
USER nodeLABELAdd metadata key-value pairs to the image (maintainer, version, description, etc.).
LABEL maintainer="team@example.com" version="1.0"HEALTHCHECKTell Docker how to test that the container is still working. Reports healthy/unhealthy status.
HEALTHCHECK --interval=30s CMD curl -f http://localhost/ || exit 1SHELLOverride the default shell used for the shell form of RUN, CMD, and ENTRYPOINT.
SHELL ["/bin/bash", "-c"]STOPSIGNALSet the system call signal that will be sent to the container to exit gracefully.
STOPSIGNAL SIGTERMONBUILDAdd a trigger instruction that runs when the image is used as a base in another build.
ONBUILD COPY . /app