Docker Cheatsheet

135+ Docker commands and Dockerfile instructions — searchable by keyword or category. Click Copy to grab any command.

Showing 135 of 135 entries
docker run <image>Containers

Create and start a container from an image.

docker run -d <image>Containers

Run container in detached (background) mode.

docker run -it <image> bashContainers

Run container interactively with a bash shell.

docker run --name <name> <image>Containers

Assign a custom name to the container.

docker run -p 8080:80 <image>Containers

Map host port 8080 to container port 80.

docker run -e KEY=value <image>Containers

Set an environment variable inside the container.

docker run -v /host:/container <image>Containers

Bind-mount a host directory into the container.

docker run --rm <image>Containers

Automatically remove container when it exits.

docker run --network <net> <image>Containers

Connect the container to a specific network.

docker run --restart always <image>Containers

Always restart the container if it stops.

docker run --memory 512m <image>Containers

Limit the container memory to 512 MB.

docker run --cpus 1.5 <image>Containers

Limit the container to 1.5 CPU cores.

docker psContainers

List all running containers.

docker ps -aContainers

List all containers including stopped ones.

docker ps -qContainers

List only container IDs (running).

docker start <container>Containers

Start a stopped container.

docker stop <container>Containers

Gracefully stop a running container (SIGTERM).

docker kill <container>Containers

Force-stop a container immediately (SIGKILL).

docker restart <container>Containers

Stop then start a container.

docker rm <container>Containers

Remove a stopped container.

docker rm -f <container>Containers

Force-remove a running container.

docker rm $(docker ps -aq)Containers

Remove all stopped containers.

docker exec -it <container> bashContainers

Open an interactive shell in a running container.

docker exec <container> <cmd>Containers

Run a command inside a running container.

docker logs <container>Containers

Fetch logs from a container.

docker logs -f <container>Containers

Follow (tail) container logs in real time.

docker logs --tail 100 <container>Containers

Show the last 100 lines of container logs.

docker inspect <container>Containers

Return low-level JSON info about a container.

docker statsContainers

Live resource usage stats for all running containers.

docker top <container>Containers

Show processes running inside a container.

docker cp <container>:/path ./localContainers

Copy a file from a container to the host.

docker cp ./local <container>:/pathContainers

Copy a file from the host into a container.

docker pause <container>Containers

Pause all processes in a container (SIGSTOP).

docker unpause <container>Containers

Unpause a paused container.

docker rename <old> <new>Containers

Rename a container.

docker update --memory 1g <container>Containers

Update resource limits of a running container.

docker wait <container>Containers

Block until a container stops, then print exit code.

docker diff <container>Containers

Inspect file changes in a container's filesystem.

docker commit <container> <image:tag>Containers

Create a new image from a container's state.

docker port <container>Containers

List port mappings for a container.

docker imagesImages

List all locally available images.

docker images -aImages

List all images including intermediate layers.

docker pull <image>Images

Download an image from a registry.

docker pull <image>:<tag>Images

Pull a specific tagged version of an image.

docker push <image>:<tag>Images

Push an image to a registry.

docker build -t <name>:<tag> .Images

Build an image from the Dockerfile in the current directory.

docker build -f <Dockerfile> -t <name> .Images

Build using a custom Dockerfile path.

docker build --no-cache -t <name> .Images

Build without using the layer cache.

docker build --build-arg KEY=val -t <n> .Images

Pass a build-time argument to the Dockerfile.

docker rmi <image>Images

Remove a local image.

docker rmi -f <image>Images

Force-remove an image even if in use.

docker rmi $(docker images -q)Images

Remove all local images.

docker tag <image> <new-name>:<tag>Images

Tag an image with a new name/tag.

docker image inspect <image>Images

Display detailed information about an image.

docker image history <image>Images

Show the layer build history of an image.

docker image pruneImages

Remove all dangling (untagged) images.

docker image prune -aImages

Remove all unused images (not referenced by any container).

docker save -o out.tar <image>Images

Export an image to a tar archive file.

docker load -i out.tarImages

Import an image from a tar archive.

docker search <term>Images

Search Docker Hub for images matching a term.

docker volume lsVolumes

List all volumes.

docker volume create <name>Volumes

Create a named volume.

docker volume inspect <name>Volumes

Display detailed info about a volume.

docker volume rm <name>Volumes

Remove a volume.

docker volume pruneVolumes

Remove all unused volumes.

docker run -v <name>:/data <image>Volumes

Mount a named volume into a container at /data.

docker run -v $(pwd):/app <image>Volumes

Bind-mount the current directory to /app in the container.

docker run --mount type=bind,src=.,dst=/app <image>Volumes

Bind-mount using the explicit --mount syntax.

docker run --volumes-from <container> <image>Volumes

Mount all volumes from another container.

docker run --tmpfs /tmp <image>Volumes

Mount a temporary in-memory filesystem at /tmp.

docker network lsNetworks

List all networks.

docker network create <name>Networks

Create a bridge network.

docker network create --driver overlay <n>Networks

Create an overlay network (Swarm-compatible).

docker network inspect <name>Networks

Show detailed info about a network.

docker network rm <name>Networks

Remove a network.

docker network pruneNetworks

Remove all unused networks.

docker network connect <net> <container>Networks

Connect a running container to a network.

docker network disconnect <net> <container>Networks

Disconnect a container from a network.

docker run --network host <image>Networks

Run container using the host network stack directly.

docker run --network none <image>Networks

Run container with no network access.

docker compose upCompose

Start all services defined in docker-compose.yml.

docker compose up -dCompose

Start all services in detached mode.

docker compose up --buildCompose

Rebuild images before starting services.

docker compose up --scale web=3Compose

Scale a service to 3 replicas.

docker compose downCompose

Stop and remove containers, networks created by up.

docker compose down -vCompose

Also remove named volumes when bringing down.

docker compose down --rmi allCompose

Remove images used by services on down.

docker compose psCompose

List containers for the current Compose project.

docker compose logsCompose

View output from all services.

docker compose logs -f <service>Compose

Follow logs for a specific service.

docker compose exec <service> bashCompose

Open a shell in a running service container.

docker compose run --rm <service> <cmd>Compose

Run a one-off command in a service container.

docker compose buildCompose

Build or rebuild service images.

docker compose pullCompose

Pull latest images for all services.

docker compose stopCompose

Stop running services without removing containers.

docker compose restart <service>Compose

Restart a specific service.

docker compose configCompose

Validate and print the resolved Compose configuration.

docker compose -f custom.yml upCompose

Use a custom Compose file instead of the default.

docker loginRegistry

Log in to Docker Hub (prompts for credentials).

docker login <registry>Registry

Log in to a private registry.

docker logoutRegistry

Log out from Docker Hub.

docker push <user>/<image>:<tag>Registry

Push an image to Docker Hub.

docker pull <registry>/<image>:<tag>Registry

Pull an image from a private registry.

docker tag <image> <registry>/<image>:<tag>Registry

Tag an image for a private registry.

docker buildx build --platform linux/amd64,linux/arm64 -t <name> --push .Registry

Build and push a multi-platform image.

docker manifest inspect <image>:<tag>Registry

Inspect the manifest of a multi-platform image.

docker infoSystem

Display Docker system-wide information.

docker versionSystem

Show Docker client and daemon version details.

docker system dfSystem

Show disk usage by images, containers, and volumes.

docker system pruneSystem

Remove all stopped containers, unused networks and dangling images.

docker system prune -aSystem

Also remove all unused images (not just dangling).

docker system prune --volumesSystem

Also remove unused volumes during prune.

docker eventsSystem

Stream real-time events from the Docker daemon.

docker events --filter type=containerSystem

Filter daemon events by resource type.

docker context lsSystem

List all Docker contexts (local, remote, etc.).

docker context use <name>System

Switch to a different Docker context.

docker buildx lsSystem

List available Buildx builder instances.

docker buildx create --useSystem

Create and use a new Buildx builder.

DOCKERFILEInstruction Reference17 instructions
FROM

Set the base image for subsequent instructions. Must be the first instruction (except ARG).

FROM node:20-alpine
RUN

Execute a command during the image build and commit the result as a new layer.

RUN apt-get update && apt-get install -y curl
COPY

Copy files or directories from the build context into the image filesystem.

COPY package*.json ./
ADD

Like COPY, but also supports URLs and auto-extracts tar archives. Prefer COPY when possible.

ADD https://example.com/file.tar.gz /tmp/
WORKDIR

Set the working directory for RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow.

WORKDIR /app
ENV

Set environment variables that persist into the running container.

ENV NODE_ENV=production PORT=3000
ARG

Define a build-time variable that can be passed with --build-arg. Does not persist in the final image.

ARG VERSION=1.0.0
EXPOSE

Document which port the container listens on at runtime (informational; does not publish).

EXPOSE 8080
CMD

Provide default command/args for the container. Overridden by docker run arguments. Only last CMD applies.

CMD ["node", "server.js"]
ENTRYPOINT

Configure a container to run as an executable. CMD arguments are appended. Harder to override than CMD.

ENTRYPOINT ["python", "app.py"]
VOLUME

Create a mount point and mark it as holding externally-mounted volumes from the host or other containers.

VOLUME ["/data"]
USER

Set the user (and optionally group) for subsequent RUN, CMD, and ENTRYPOINT instructions.

USER node
LABEL

Add metadata key-value pairs to the image (maintainer, version, description, etc.).

LABEL maintainer="team@example.com" version="1.0"
HEALTHCHECK

Tell Docker how to test that the container is still working. Reports healthy/unhealthy status.

HEALTHCHECK --interval=30s CMD curl -f http://localhost/ || exit 1
SHELL

Override the default shell used for the shell form of RUN, CMD, and ENTRYPOINT.

SHELL ["/bin/bash", "-c"]
STOPSIGNAL

Set the system call signal that will be sent to the container to exit gracefully.

STOPSIGNAL SIGTERM
ONBUILD

Add a trigger instruction that runs when the image is used as a base in another build.

ONBUILD COPY . /app