Skip to main content

77 entries · 7 sections

Docker Cheat Sheet

Docker commands for building images, running and debugging containers, wiring up volumes and networks, writing a Dockerfile, driving Compose, and reclaiming the disk space all of that quietly consumes.

Images

SyntaxWhat it does
docker pull nginx:alpineDownload an image at a specific tag.
docker imagesList local images with their sizes.
docker build -t app:1.0 .Build an image from the Dockerfile in this directory.
docker build -f api.Dockerfile -t api .Build from a differently named Dockerfile.
docker build --no-cache -t app .Rebuild ignoring every cached layer.
docker build --build-arg KEY=val -t app .Pass a build-time argument declared with ARG.
docker tag app:1.0 registry/app:1.0Give an image a second name before pushing it.
docker push registry/app:1.0Publish an image to a registry.
docker rmi app:1.0Delete a local image.
docker history app:1.0Show the layers and what each one added to the size.
docker save -o app.tar app:1.0Export an image to a tar file; docker load -i app.tar imports it.

Running containers

SyntaxWhat it does
docker run nginxRun a container in the foreground, streaming its output.
docker run -d nginxRun detached, in the background.
docker run -it ubuntu bashInteractive shell inside a container.
docker run --rm alpine echo hiDelete the container the moment it exits. Use this for one-off commands.
docker run -p 8080:80 nginxPublish the container's port 80 as port 8080 on the host.
docker run -v $(pwd):/app nodeBind-mount the current directory into the container.
docker run -v data:/var/lib/db postgresMount a named volume so the data survives the container.
docker run -e NODE_ENV=production appSet an environment variable.
docker run --env-file .env appLoad environment variables from a file.
docker run --name web -d nginxGive the container a name you can use instead of an id.
docker run --restart unless-stopped -d appRestart automatically unless you stopped it yourself.
docker run --memory 512m --cpus 1.5 appCap the memory and CPU the container may use.

Managing and debugging containers

SyntaxWhat it does
docker psThe running containers.
docker ps -aAll containers, stopped ones included.
docker stop webGraceful stop: SIGTERM, then SIGKILL after a timeout.
docker start web / docker restart webStart a stopped container, or restart a running one.
docker rm webDelete a stopped container.
docker rm -f webKill and delete in one step.
docker logs -f webFollow a container's output.
docker logs --tail 100 --timestamps webThe last hundred lines, with timestamps.
docker exec -it web shOpen a shell inside a running container. Use bash if the image has it.
docker exec web envRun a one-off command inside a running container.
docker cp web:/app/log.txt .Copy a file out of a container. It works in both directions.
docker statsLive CPU, memory, network and I/O per container.
docker inspect webThe full JSON description: mounts, networks, environment, health, exit code.
docker top webThe processes running inside a container.

Volumes and networks

SyntaxWhat it does
docker volume lsList named volumes.
docker volume create dataCreate a named volume.
docker volume inspect dataWhere the volume actually lives on the host.
docker volume rm dataDelete a volume and everything in it.
docker network lsList networks.
docker network create appnetCreate a user-defined bridge. Containers on it can reach each other by name.
docker network connect appnet webAttach a running container to a network.
docker network inspect appnetWhich containers are attached and what addresses they have.

Dockerfile instructions

SyntaxWhat it does
FROM node:20-alpineThe base image. Pin a version — latest is not a version.
WORKDIR /appSet the working directory for every instruction that follows.
COPY package*.json ./Copy the dependency manifests first so the install layer stays cached.
RUN npm ciRun a command at build time and commit the result as a layer.
COPY . .Copy the rest of the source, after the dependency layer.
ENV NODE_ENV=productionSet an environment variable inside the image.
ARG VERSIONA build-time-only variable, not present at runtime.
EXPOSE 3000Document the port the app listens on. It does not publish anything by itself.
USER nodeDrop out of root before the container runs. Do this in every production image.
VOLUME /dataDeclare a path whose contents belong outside the image.
HEALTHCHECK CMD curl -f http://localhost:3000 || exit 1Teach Docker how to tell whether the app is actually alive.
ENTRYPOINT ["node"] + CMD ["server.js"]ENTRYPOINT is the fixed command; CMD is the default argument, which docker run can override.
FROM base AS build ... COPY --from=build /app/dist ./Multi-stage build: compile in one image, ship only the artefact in the final one.
.dockerignoreKeep node_modules, .git and secrets out of the build context. It makes builds dramatically faster.

Docker Compose

SyntaxWhat it does
docker compose upStart every service defined in the compose file.
docker compose up -dStart them in the background.
docker compose up --buildRebuild the images before starting.
docker compose downStop and remove the containers and networks.
docker compose down -vAlso delete the named volumes. This destroys your database data.
docker compose psThe status of this project's services.
docker compose logs -f apiFollow one service's logs.
docker compose exec api shOpen a shell in a running service.
docker compose run --rm api npm testRun a one-off command in a fresh container.
docker compose restart apiRestart a single service.
docker compose configPrint the fully resolved compose file. The way to debug variable substitution.

Reclaiming disk space

SyntaxWhat it does
docker system dfHow much disk images, containers, volumes and build cache are using.
docker system pruneRemove stopped containers, unused networks, dangling images and build cache.
docker system prune -a --volumesRemove everything not currently in use, volumes included. Read that twice before running it.
docker image pruneDelete dangling images only — the untagged leftovers of rebuilds.
docker container pruneDelete every stopped container.
docker volume pruneDelete volumes that no container references.
docker builder pruneClear the BuildKit cache, which grows quietly and can reach many gigabytes.

Worth knowing

  • Layer order decides your build time. Copy package.json and run the install before copying the rest of the source: changing one line of application code then reuses the cached dependency layer instead of reinstalling everything. Put the files that change most often last.
  • docker compose down -v deletes named volumes, which on a typical setup means your local database. Plain down leaves them alone. If you only want a fresh start on code, down and up --build is enough.
  • When a container exits immediately, docker logs <name> almost always has the answer, and docker inspect <name> --format '{{.State.ExitCode}}' tells you how it died. Exit code 137 means it was SIGKILLed, usually by the memory limit.

Try a snippet instead of reading about it

Reference is faster when you can run the thing. Paste a snippet into AltF Code, the free in-browser editor — real syntax highlighting for HTML, CSS, JavaScript, Python, SQL, JSON and more, with HTML, CSS and JavaScript running live in the preview. No install and no sign-up.

Open the editor

Docker — common questions

What is the difference between an image and a container?
An image is a read-only stack of filesystem layers plus metadata — the template. A container is a running (or stopped) instance of that image with a thin writable layer on top. One image can back hundreds of containers, and deleting a container does not touch the image.
What is the difference between CMD and ENTRYPOINT?
ENTRYPOINT is the executable that always runs; CMD supplies default arguments to it. Anything you type after docker run image replaces CMD but not ENTRYPOINT. If you set only CMD, the whole thing is replaceable, which is what you usually want for a general-purpose image.
How do I get data out of a container after it stops?
If the container still exists, docker cp <container>:/path/file . copies files out even from a stopped container. If it was started with --rm it is gone, which is exactly why anything you care about should live in a named volume or a bind mount rather than in the container's writable layer.
Why is Docker using so much disk space?
Old images, stopped containers, orphaned volumes and the BuildKit cache all accumulate. Run docker system df to see the split, then docker system prune to clear the safe categories. Add -a to remove images not used by any container, and --volumes only when you are certain nothing in them matters.