DevBolt

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 images

List all locally stored images

Add --format to customize output columns

docker pull image:tag

Download an image from a registry

docker pull node:20-alpine -- pulls from Docker Hub

docker push image:tag

Upload an image to a registry

docker push myrepo/myapp:1.0 -- push to Docker Hub or private registry

docker rmi image

Remove one or more images

docker rmi myapp:1.0 -- add -f to force removal

docker image prune

Remove all dangling (untagged) images

Add -a to remove ALL unused images, not just dangling ones

docker tag source:tag target:tag

Create a new tag that refers to an existing image

docker tag myapp:1.0 myrepo/myapp:latest

docker save -o file.tar image

Save an image to a tar archive file

docker save -o myapp.tar myapp:1.0 -- for offline transfer

docker load -i file.tar

Load an image from a tar archive file

docker load -i myapp.tar -- imports the image locally

Containers

docker run image

Create and start a new container from an image

docker run ubuntu -- runs and attaches to the container

docker run -d image

Run a container in detached (background) mode

docker run -d nginx -- returns the container ID

docker run -p host:container image

Map a host port to a container port

docker run -p 8080:80 nginx -- access via localhost:8080

docker run -v host:container image

Mount a host directory or volume into the container

docker run -v $(pwd):/app node:20 -- bind mount current dir

docker run --rm image

Automatically remove the container when it exits

docker run --rm alpine echo hello -- cleans up after itself

docker run --name myname image

Assign a custom name to the container

docker run --name web -d nginx -- easier to reference later

docker ps

List currently running containers

Shows container ID, image, command, status, ports, and names

docker ps -a

List all containers including stopped ones

Useful for finding exited containers to restart or remove

docker stop container

Gracefully stop a running container (SIGTERM, then SIGKILL)

docker stop web -- waits 10s by default, use -t to change

docker start container

Start a previously stopped container

docker start web -- resumes with original run configuration

docker rm container

Remove a stopped container

docker rm web -- add -f to force-remove a running container

docker exec -it container command

Run a command inside a running container interactively

docker exec -it web /bin/sh -- opens a shell session

Networks

docker network create name

Create a new user-defined network

docker network create mynet -- bridge driver by default

docker network ls

List all Docker networks

Shows network ID, name, driver, and scope

docker network inspect name

Display detailed information about a network

Shows connected containers, subnet, gateway, and config

docker network connect network container

Connect a running container to a network

docker network connect mynet web -- container joins network

docker network disconnect network container

Disconnect a container from a network

docker network disconnect mynet web -- removes from network

docker network rm name

Remove one or more networks

docker network rm mynet -- network must have no containers

docker network prune

Remove all unused networks

Removes networks not used by any container. Add -f to skip prompt

Volumes

docker volume create name

Create a named volume for persistent data storage

docker volume create pgdata -- data survives container removal

docker volume ls

List all Docker volumes

Shows volume driver and name

docker volume inspect name

Display detailed information about a volume

Shows mount point, driver, creation date, and labels

docker volume rm name

Remove one or more volumes

docker volume rm pgdata -- volume must not be in use

docker volume prune

Remove 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=/path

Two syntaxes for mounting volumes in docker run

--mount is more explicit and recommended for clarity; -v is shorter

Docker Compose

docker compose up

Create and start all services defined in compose.yml

Builds images if needed, creates networks and volumes

docker compose up -d

Start all services in detached (background) mode

docker compose up -d -- returns control to your terminal

docker compose down

Stop and remove containers, networks created by up

Add -v to also remove named volumes, --rmi all to remove images

docker compose build

Build or rebuild service images

docker compose build --no-cache -- force full rebuild

docker compose ps

List containers for the current Compose project

Shows status, ports, and command for each service

docker compose logs

View log output from all services

docker compose logs -f web -- follow logs for specific service

docker compose exec service command

Execute a command in a running service container

docker compose exec db psql -U postgres -- open psql shell

docker compose pull

Pull the latest images for all services

Run before 'up' to ensure you have the newest versions

docker compose restart

Restart all or specific services

docker compose restart web -- restarts only the web service

docker compose config

Validate and display the resolved Compose configuration

Merges all compose files and shows the final config. Great for debugging

Dockerfile Instructions

FROM image:tag

Set the base image for the build stage

FROM node:20-alpine -- always pin a specific version

RUN command

Execute 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 dest

Copy files or directories from host to image filesystem

COPY package*.json ./ -- copy only what is needed first for caching

ADD src dest

Copy files with extra features (auto-extract tar, fetch URLs)

ADD app.tar.gz /app -- prefer COPY unless you need tar extraction

WORKDIR /path

Set the working directory for subsequent instructions

WORKDIR /app -- creates the directory if it does not exist

ENV KEY=value

Set an environment variable in the image

ENV NODE_ENV=production -- persists in running containers

EXPOSE port

Document which port the container listens on at runtime

EXPOSE 3000 -- informational only, does not publish the port

VOLUME /path

Create a mount point for persistent or shared data

VOLUME /data -- anonymous volume created at runtime

ARG NAME=default

Define 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 container

Fetch the stdout/stderr logs of a container

docker logs web -- add --tail 100 to limit output

docker logs -f container

Follow (stream) log output in real-time

docker logs -f web -- like tail -f, Ctrl+C to stop

docker inspect container

Return detailed JSON info about a container or image

docker inspect web | jq '.[0].NetworkSettings' -- pipe to jq for readability

docker stats

Display a live stream of container resource usage

docker stats web db -- monitor specific containers' CPU, memory, net, I/O

docker top container

Display the running processes inside a container

docker top web -- similar to running ps inside the container

docker events

Stream real-time events from the Docker daemon

docker events --filter type=container -- filter by object type

docker diff container

Show filesystem changes made inside a container since start

A = added, C = changed, D = deleted files relative to the image

docker cp container:/path /host/path

Copy files between a container and the local filesystem

docker cp web:/app/logs ./logs -- works with running or stopped containers

Registry & Hub

docker login

Log in to a Docker registry (Docker Hub by default)

docker login ghcr.io -- log in to GitHub Container Registry

docker logout

Log out from a Docker registry

docker logout ghcr.io -- removes stored credentials

docker search term

Search Docker Hub for images matching a term

docker search nginx --filter stars=100 -- filter by popularity

docker pull image:tag

Download an image or repository from a registry

docker pull ghcr.io/org/app:v2 -- pull from any registry

docker push image:tag

Upload an image to a registry

Tag first, then push: docker tag app myrepo/app:v1 && docker push myrepo/app:v1

Frequently Asked Questions

What are the most common Docker commands?
The most commonly used Docker commands are: docker run (create and start a container), docker build (build an image from a Dockerfile), docker ps (list running containers), docker images (list local images), docker pull (download an image from a registry), docker stop (stop a running container), docker rm (remove a container), docker exec -it (run a command inside a running container), docker logs (view container logs), and docker compose up (start services defined in a Compose file). These commands cover the majority of daily Docker workflows.
How do I remove all unused Docker images?
To remove all unused (dangling) Docker images, run 'docker image prune' which removes images not tagged and not referenced by any container. To remove ALL images not used by at least one container, run 'docker image prune -a'. For a broader cleanup that also removes stopped containers, unused networks, and build cache, use 'docker system prune'. Add the -f flag to skip the confirmation prompt. Be cautious with 'prune -a' in production environments as it will remove images you may need to quickly restart containers.
What's the difference between CMD and ENTRYPOINT in a Dockerfile?
CMD and ENTRYPOINT both specify what runs when a container starts, but they serve different purposes. ENTRYPOINT defines the main executable that always runs — it cannot be easily overridden by command-line arguments (you need --entrypoint flag). CMD provides default arguments that can be overridden when running the container. The best practice is to use them together: ENTRYPOINT for the fixed command and CMD for default arguments. For example, ENTRYPOINT ["python"] with CMD ["app.py"] means running 'docker run myimage' executes 'python app.py', but 'docker run myimage test.py' executes 'python test.py'. Always use the exec form (JSON array) over the shell form for both.
How do I troubleshoot a running Docker container?
To troubleshoot a running Docker container, use these commands: 'docker logs <container>' to view stdout/stderr output (add -f to follow in real-time and --tail 100 to limit lines). 'docker exec -it <container> /bin/sh' to open an interactive shell inside the container. 'docker inspect <container>' to view detailed configuration, networking, mounts, and environment variables. 'docker stats <container>' to monitor real-time CPU, memory, network, and disk I/O usage. 'docker top <container>' to list processes running inside the container. 'docker diff <container>' to see filesystem changes made since the container started. For networking issues, 'docker network inspect <network>' shows connected containers and their IP addresses.

Related Tools