Docker Cheatsheet 100+ commands

Interactive Docker command reference. Search, filter by category, copy instantly.

Images
docker pull image:tag Download an image from Docker Hub (or registry)Omit tag to get :latest
docker build -t name:tag . Build image from Dockerfile in current directory-t sets the name and tag
docker build --no-cache -t name:tag . Build without using cached layersForces re-download of dependencies
docker images List all local images with sizes and tags
docker rmi image:tag Remove an image (must stop containers using it first)
docker image prune Remove dangling (untagged) imagesAdd -a to remove all unused images
docker tag source:tag target:tag Create a tag alias for an image
docker push image:tag Push image to Docker Hub or registryMust be logged in: docker login
docker inspect image:tag Show detailed JSON metadata about an image
docker image history image:tag Show layer-by-layer build history and sizes
docker save -o file.tar image:tag Export an image to a tar archiveTransfer images without a registry
docker load -i file.tar Import an image from a tar archive
docker search image_name Search Docker Hub for imagesUse --filter stars=100 to filter by popularity
Containers
docker run image:tag Create and start a container from an image
docker run -d --name myapp -p 8080:80 image:tag Run in background, set name, map port 8080→80Access at localhost:8080
docker run -it image:tag /bin/bash Run interactively with a shellUse /bin/sh for Alpine images
docker run --rm image:tag Auto-remove container when it exitsGreat for one-off commands
docker run -e KEY=value image:tag Pass environment variable to containerUse --env-file .env to load from file
docker run -v /host/path:/container/path image:tag Bind-mount a host directory into the containerChanges are reflected immediately
docker run -v vol_name:/container/path image:tag Use a named volume (persists across containers)Better than bind mounts for DBs
docker run --network net_name image:tag Connect container to a specific network
docker start container_id Start a stopped container
docker stop container_id Gracefully stop a container (SIGTERM → SIGKILL after 10s)
docker kill container_id Immediately kill a container with SIGKILL
docker restart container_id Stop and restart a container
docker rm container_id Remove a stopped containerAdd -f to force remove running containers
docker container prune Remove all stopped containers
docker ps List running containers with status and ports
docker ps -a List all containers including stopped ones
docker exec -it container_id bash Open an interactive shell inside a running containerUse 'sh' if bash isn't available
docker exec container_id command Run a one-off command inside a running container
docker logs container_id Print container stdout and stderr logs
docker logs -f container_id Follow (stream) container logs in real time
docker logs --tail 100 container_id Show last 100 lines of logsAdd -f to follow from there
docker inspect container_id Show full JSON metadata (env, mounts, network, etc.)
docker cp file.txt container_id:/app/ Copy a file from host into a running containerReverse: docker cp container:/path ./local
docker stats Live stream of CPU, memory, network, I/O for all containers
docker top container_id Show running processes inside a container
docker rename old_name new_name Rename an existing container
docker pause container_id Suspend all processes in a container (SIGSTOP)
docker unpause container_id Resume a paused container
docker commit container_id image:tag Create an image from a container's current statePrefer Dockerfiles over commit for reproducibility
Docker Compose
docker compose up Start all services defined in docker-compose.ymlAdd -d to run in background
docker compose up -d Start all services in detached (background) mode
docker compose up --build Rebuild images before starting servicesUse after Dockerfile changes
docker compose down Stop and remove containers, networksDoes NOT remove volumes by default
docker compose down -v Stop, remove containers AND named volumesWARNING: destroys data in volumes
docker compose build Build (or rebuild) all service images
docker compose ps List containers for this Compose project with status
docker compose logs Show logs from all servicesAdd -f to follow, or service name to filter
docker compose logs -f web Follow logs from the 'web' service only
docker compose exec web bash Open shell in the running 'web' service container
docker compose run --rm web python manage.py migrate Run a one-off command in a new service container--rm removes the container after
docker compose restart Restart all services (or add service name to target one)
docker compose stop Stop services without removing containers
docker compose pull Pull latest versions of service images
docker compose config Validate and display the resolved Compose config
docker compose up -d --scale web=3 Start 3 replicas of the 'web' service
Networks
docker network ls List all Docker networks
docker network create my_network Create a new bridge networkContainers on same network can reach each other by name
docker network rm my_network Remove a network (must disconnect containers first)
docker network inspect my_network Show detailed network info including connected containers
docker network connect my_network container_id Connect a running container to a network
docker network disconnect my_network container_id Disconnect a container from a network
docker network prune Remove all unused networks
Volumes
docker volume ls List all Docker volumes
docker volume create my_volume Create a named volumeNamed volumes persist even after container removal
docker volume rm my_volume Remove a volume (data is permanently deleted)
docker volume inspect my_volume Show volume details including mount point on host
docker volume prune Remove all unused volumesAdd -f to skip confirmation
System
docker info Display system-wide Docker info (version, containers, images)
docker version Show Docker client and server version details
docker system df Show disk usage by images, containers, volumesAdd -v for per-item breakdown
docker system prune Remove stopped containers, unused networks, dangling images, build cache
docker system prune -a Deep clean: also removes unused images (not just dangling)Frees significant disk space
docker system prune -a --volumes Full cleanup including unused volumesWARNING: destroys volume data
docker login Log in to Docker Hub (or add registry URL for private registries)
docker logout Log out and remove stored credentials
docker system events Stream real-time events from the Docker daemon
docker context ls List Docker contexts (local, remote, Docker Desktop)
docker context use context_name Switch active Docker context (e.g., switch to remote server)
Dockerfile Instructions
FROM node:20-alpine Set the base image (first instruction in every Dockerfile)Prefer slim/alpine variants to reduce image size
WORKDIR /app Set the working directory for subsequent instructionsCreated automatically if it doesn't exist
COPY package*.json ./ Copy files from build context into imageCopy package.json before source to cache npm install layer
COPY . . Copy all source files into working directoryUse .dockerignore to exclude node_modules, .git, etc.
ADD archive.tar.gz /app/ Like COPY but auto-extracts archives and supports URLsPrefer COPY unless you need auto-extraction
RUN npm ci --only=production Execute a command during build (creates a new layer)Chain commands with && to reduce layer count
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* Chain commands to minimize layers and clean upCleanup in same RUN keeps layer small
ENV NODE_ENV=production PORT=3000 Set environment variables (available at runtime too)Use ARG for build-time-only values
ARG VERSION=latest Define build-time variable (not available in running container)Override with: docker build --build-arg VERSION=1.2
EXPOSE 3000 Document which port the container listens onDoes not actually publish the port — use -p at runtime
CMD ["node", "server.js"] Default command to run when container startsCan be overridden at docker run time
ENTRYPOINT ["node"] Fixed executable that always runs (CMD provides default args)Combine: ENTRYPOINT ["node"] + CMD ["app.js"]
USER node Set user for subsequent instructions and container runtimeRun as non-root for security
VOLUME /app/data Declare a mount point for external volumesData written here persists when container is removed
LABEL version="1.0" maintainer="team@example.com" Add metadata key-value pairs to the image
HEALTHCHECK --interval=30s --timeout=5s CMD curl -f http://localhost:3000/health || exit 1 Define health check commandDocker reports healthy/unhealthy status
FROM node:20 AS builder\nRUN npm run build\nFROM node:20-alpine\nCOPY --from=builder /app/dist ./dist Multi-stage build: compile in large image, ship in slim oneCan cut production image size by 80%+

⚡ Pro Tips

Frequently Asked Questions

How do I see all running Docker containers?

Use docker ps to list all currently running containers. Add the -a flag (docker ps -a) to see all containers including stopped ones. The output shows container ID, image, command, creation time, status, ports, and name. Use docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" to customize the output columns.

How do I run a Docker container in the background?

Use the -d (detach) flag: docker run -d image:tag. This starts the container in the background and prints the container ID. Use docker logs container_id to view its output later, or docker logs -f container_id to follow the logs in real time. Use docker attach container_id to reconnect to the container's main process.

How do I remove all stopped Docker containers?

Run docker container prune to remove all stopped containers with a confirmation prompt. Alternatively use docker rm $(docker ps -aq -f status=exited) to remove only exited containers. To force-remove all containers (including running ones) use docker rm -f $(docker ps -aq). Add --volumes to also remove associated anonymous volumes.

How do I access a running container's shell?

Use docker exec -it container_name /bin/bash (or /bin/sh for Alpine-based images). The -i flag keeps stdin open and -t allocates a pseudo-TTY. You can also run any one-off command: docker exec container_name env to list environment variables, or docker exec container_name cat /etc/hosts to read a file. If the container doesn't have bash, try sh, ash, or install bash with apt-get install bash or apk add bash.

What is the difference between CMD and ENTRYPOINT in Dockerfile?

ENTRYPOINT defines the executable that always runs when a container starts. CMD provides default arguments that can be overridden at docker run time. Best practice: use ENTRYPOINT ["node"] + CMD ["app.js"] — this runs node app.js by default, but docker run image server.js would run node server.js. If you only use CMD, the entire command can be replaced at runtime. Use exec form (["node", "app.js"]) not shell form (node app.js) to ensure signals are forwarded correctly.

How do I rebuild a Docker image without cache?

Use docker build --no-cache -t image:tag . to rebuild without using any cached layers. This forces re-downloading dependencies and re-running all RUN commands. In Docker Compose: docker compose build --no-cache. To selectively bust the cache for a specific RUN command in your Dockerfile, add ARG CACHEBUST before it and pass --build-arg CACHEBUST=$(date +%s) at build time.

How do I view Docker container logs?

Use docker logs container_name to print all logs. Options: -f to follow in real time, --tail 100 for the last 100 lines, --since 1h for logs from the last hour, --until 2026-05-29T12:00:00 for logs up to a time. For Docker Compose: docker compose logs -f web follows logs for just the 'web' service. View logs since a timestamp with docker logs --since "2026-05-29T10:00:00" container.

How do I clean up unused Docker resources?

Use docker system prune to remove stopped containers, unused networks, dangling images, and build cache. Add -a to also remove unused images (not just dangling): docker system prune -a. Add --volumes to include anonymous volumes. Check disk usage first with docker system df. On a development machine this can recover 5–50 GB. Individual pruning: docker image prune -a, docker container prune, docker volume prune, docker network prune.