Docker Cheat Sheet
Comprehensive Docker CLI reference with 70 commands across 8 categories. Search, filter, copy, and click to validate your configurations with DevBolt tools.
Showing 70 of 70 commands
Images
docker build .Build an image from a Dockerfile in the current directory
Reads Dockerfile in . and creates an unnamed image
docker build -t name:tag .Build and tag an image with a name and version
docker build -t myapp:1.0 . -- tags as myapp:1.0
docker imagesList all locally stored images
Add --format to customize output columns
docker pull image:tagDownload an image from a registry
docker pull node:20-alpine -- pulls from Docker Hub
docker push image:tagUpload an image to a registry
docker push myrepo/myapp:1.0 -- push to Docker Hub or private registry
docker rmi imageRemove one or more images
docker rmi myapp:1.0 -- add -f to force removal
docker image pruneRemove all dangling (untagged) images
Add -a to remove ALL unused images, not just dangling ones
docker tag source:tag target:tagCreate a new tag that refers to an existing image
docker tag myapp:1.0 myrepo/myapp:latest
docker save -o file.tar imageSave an image to a tar archive file
docker save -o myapp.tar myapp:1.0 -- for offline transfer
docker load -i file.tarLoad an image from a tar archive file
docker load -i myapp.tar -- imports the image locally
Containers
docker run imageCreate and start a new container from an image
docker run ubuntu -- runs and attaches to the container
docker run -d imageRun a container in detached (background) mode
docker run -d nginx -- returns the container ID
docker run -p host:container imageMap a host port to a container port
docker run -p 8080:80 nginx -- access via localhost:8080
docker run -v host:container imageMount a host directory or volume into the container
docker run -v $(pwd):/app node:20 -- bind mount current dir
docker run --rm imageAutomatically remove the container when it exits
docker run --rm alpine echo hello -- cleans up after itself
docker run --name myname imageAssign a custom name to the container
docker run --name web -d nginx -- easier to reference later
docker psList currently running containers
Shows container ID, image, command, status, ports, and names
docker ps -aList all containers including stopped ones
Useful for finding exited containers to restart or remove
docker stop containerGracefully stop a running container (SIGTERM, then SIGKILL)
docker stop web -- waits 10s by default, use -t to change
docker start containerStart a previously stopped container
docker start web -- resumes with original run configuration
docker rm containerRemove a stopped container
docker rm web -- add -f to force-remove a running container
docker exec -it container commandRun a command inside a running container interactively
docker exec -it web /bin/sh -- opens a shell session
Networks
docker network create nameCreate a new user-defined network
docker network create mynet -- bridge driver by default
docker network lsList all Docker networks
Shows network ID, name, driver, and scope
docker network inspect nameDisplay detailed information about a network
Shows connected containers, subnet, gateway, and config
docker network connect network containerConnect a running container to a network
docker network connect mynet web -- container joins network
docker network disconnect network containerDisconnect a container from a network
docker network disconnect mynet web -- removes from network
docker network rm nameRemove one or more networks
docker network rm mynet -- network must have no containers
docker network pruneRemove all unused networks
Removes networks not used by any container. Add -f to skip prompt
Volumes
docker volume create nameCreate a named volume for persistent data storage
docker volume create pgdata -- data survives container removal
docker volume lsList all Docker volumes
Shows volume driver and name
docker volume inspect nameDisplay detailed information about a volume
Shows mount point, driver, creation date, and labels
docker volume rm nameRemove one or more volumes
docker volume rm pgdata -- volume must not be in use
docker volume pruneRemove all volumes not used by any container
WARNING: deletes data. Add -f to skip confirmation prompt
-v name:/path vs --mount type=volume,src=name,dst=/pathTwo syntaxes for mounting volumes in docker run
--mount is more explicit and recommended for clarity; -v is shorter
Docker Compose
docker compose upCreate and start all services defined in compose.yml
Builds images if needed, creates networks and volumes
docker compose up -dStart all services in detached (background) mode
docker compose up -d -- returns control to your terminal
docker compose downStop and remove containers, networks created by up
Add -v to also remove named volumes, --rmi all to remove images
docker compose buildBuild or rebuild service images
docker compose build --no-cache -- force full rebuild
docker compose psList containers for the current Compose project
Shows status, ports, and command for each service
docker compose logsView log output from all services
docker compose logs -f web -- follow logs for specific service
docker compose exec service commandExecute a command in a running service container
docker compose exec db psql -U postgres -- open psql shell
docker compose pullPull the latest images for all services
Run before 'up' to ensure you have the newest versions
docker compose restartRestart all or specific services
docker compose restart web -- restarts only the web service
docker compose configValidate and display the resolved Compose configuration
Merges all compose files and shows the final config. Great for debugging
Dockerfile Instructions
FROM image:tagSet the base image for the build stage
FROM node:20-alpine -- always pin a specific version
RUN commandExecute a command during the image build process
RUN apt-get update && apt-get install -y curl -- chain commands to reduce layers
CMD ["executable", "arg1"]Set the default command to run when the container starts
CMD ["node", "server.js"] -- can be overridden at runtime
ENTRYPOINT ["executable"]Set the main executable that always runs in the container
ENTRYPOINT ["python"] with CMD ["app.py"] -- CMD provides default args
COPY src destCopy files or directories from host to image filesystem
COPY package*.json ./ -- copy only what is needed first for caching
ADD src destCopy files with extra features (auto-extract tar, fetch URLs)
ADD app.tar.gz /app -- prefer COPY unless you need tar extraction
WORKDIR /pathSet the working directory for subsequent instructions
WORKDIR /app -- creates the directory if it does not exist
ENV KEY=valueSet an environment variable in the image
ENV NODE_ENV=production -- persists in running containers
EXPOSE portDocument which port the container listens on at runtime
EXPOSE 3000 -- informational only, does not publish the port
VOLUME /pathCreate a mount point for persistent or shared data
VOLUME /data -- anonymous volume created at runtime
ARG NAME=defaultDefine a build-time variable (not available in running container)
ARG NODE_VERSION=20 then FROM node:${NODE_VERSION}-alpine
HEALTHCHECK CMD ["curl", "-f", "http://localhost/"]Define a command to check container health at intervals
HEALTHCHECK --interval=30s --timeout=3s CMD curl -f http://localhost/ || exit 1
Debugging & Logs
docker logs containerFetch the stdout/stderr logs of a container
docker logs web -- add --tail 100 to limit output
docker logs -f containerFollow (stream) log output in real-time
docker logs -f web -- like tail -f, Ctrl+C to stop
docker inspect containerReturn detailed JSON info about a container or image
docker inspect web | jq '.[0].NetworkSettings' -- pipe to jq for readability
docker statsDisplay a live stream of container resource usage
docker stats web db -- monitor specific containers' CPU, memory, net, I/O
docker top containerDisplay the running processes inside a container
docker top web -- similar to running ps inside the container
docker eventsStream real-time events from the Docker daemon
docker events --filter type=container -- filter by object type
docker diff containerShow filesystem changes made inside a container since start
A = added, C = changed, D = deleted files relative to the image
docker cp container:/path /host/pathCopy files between a container and the local filesystem
docker cp web:/app/logs ./logs -- works with running or stopped containers
Registry & Hub
docker loginLog in to a Docker registry (Docker Hub by default)
docker login ghcr.io -- log in to GitHub Container Registry
docker logoutLog out from a Docker registry
docker logout ghcr.io -- removes stored credentials
docker search termSearch Docker Hub for images matching a term
docker search nginx --filter stars=100 -- filter by popularity
docker pull image:tagDownload an image or repository from a registry
docker pull ghcr.io/org/app:v2 -- pull from any registry
docker push image:tagUpload an image to a registry
Tag first, then push: docker tag app myrepo/app:v1 && docker push myrepo/app:v1