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
| Syntax | What it does |
|---|---|
docker pull nginx:alpine | Download an image at a specific tag. |
docker images | List 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.0 | Give an image a second name before pushing it. |
docker push registry/app:1.0 | Publish an image to a registry. |
docker rmi app:1.0 | Delete a local image. |
docker history app:1.0 | Show the layers and what each one added to the size. |
docker save -o app.tar app:1.0 | Export an image to a tar file; docker load -i app.tar imports it. |
Running containers
| Syntax | What it does |
|---|---|
docker run nginx | Run a container in the foreground, streaming its output. |
docker run -d nginx | Run detached, in the background. |
docker run -it ubuntu bash | Interactive shell inside a container. |
docker run --rm alpine echo hi | Delete the container the moment it exits. Use this for one-off commands. |
docker run -p 8080:80 nginx | Publish the container's port 80 as port 8080 on the host. |
docker run -v $(pwd):/app node | Bind-mount the current directory into the container. |
docker run -v data:/var/lib/db postgres | Mount a named volume so the data survives the container. |
docker run -e NODE_ENV=production app | Set an environment variable. |
docker run --env-file .env app | Load environment variables from a file. |
docker run --name web -d nginx | Give the container a name you can use instead of an id. |
docker run --restart unless-stopped -d app | Restart automatically unless you stopped it yourself. |
docker run --memory 512m --cpus 1.5 app | Cap the memory and CPU the container may use. |
Managing and debugging containers
| Syntax | What it does |
|---|---|
docker ps | The running containers. |
docker ps -a | All containers, stopped ones included. |
docker stop web | Graceful stop: SIGTERM, then SIGKILL after a timeout. |
docker start web / docker restart web | Start a stopped container, or restart a running one. |
docker rm web | Delete a stopped container. |
docker rm -f web | Kill and delete in one step. |
docker logs -f web | Follow a container's output. |
docker logs --tail 100 --timestamps web | The last hundred lines, with timestamps. |
docker exec -it web sh | Open a shell inside a running container. Use bash if the image has it. |
docker exec web env | Run 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 stats | Live CPU, memory, network and I/O per container. |
docker inspect web | The full JSON description: mounts, networks, environment, health, exit code. |
docker top web | The processes running inside a container. |
Volumes and networks
| Syntax | What it does |
|---|---|
docker volume ls | List named volumes. |
docker volume create data | Create a named volume. |
docker volume inspect data | Where the volume actually lives on the host. |
docker volume rm data | Delete a volume and everything in it. |
docker network ls | List networks. |
docker network create appnet | Create a user-defined bridge. Containers on it can reach each other by name. |
docker network connect appnet web | Attach a running container to a network. |
docker network inspect appnet | Which containers are attached and what addresses they have. |
Dockerfile instructions
| Syntax | What it does |
|---|---|
FROM node:20-alpine | The base image. Pin a version — latest is not a version. |
WORKDIR /app | Set the working directory for every instruction that follows. |
COPY package*.json ./ | Copy the dependency manifests first so the install layer stays cached. |
RUN npm ci | Run 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=production | Set an environment variable inside the image. |
ARG VERSION | A build-time-only variable, not present at runtime. |
EXPOSE 3000 | Document the port the app listens on. It does not publish anything by itself. |
USER node | Drop out of root before the container runs. Do this in every production image. |
VOLUME /data | Declare a path whose contents belong outside the image. |
HEALTHCHECK CMD curl -f http://localhost:3000 || exit 1 | Teach 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. |
.dockerignore | Keep node_modules, .git and secrets out of the build context. It makes builds dramatically faster. |
Docker Compose
| Syntax | What it does |
|---|---|
docker compose up | Start every service defined in the compose file. |
docker compose up -d | Start them in the background. |
docker compose up --build | Rebuild the images before starting. |
docker compose down | Stop and remove the containers and networks. |
docker compose down -v | Also delete the named volumes. This destroys your database data. |
docker compose ps | The status of this project's services. |
docker compose logs -f api | Follow one service's logs. |
docker compose exec api sh | Open a shell in a running service. |
docker compose run --rm api npm test | Run a one-off command in a fresh container. |
docker compose restart api | Restart a single service. |
docker compose config | Print the fully resolved compose file. The way to debug variable substitution. |
Reclaiming disk space
| Syntax | What it does |
|---|---|
docker system df | How much disk images, containers, volumes and build cache are using. |
docker system prune | Remove stopped containers, unused networks, dangling images and build cache. |
docker system prune -a --volumes | Remove everything not currently in use, volumes included. Read that twice before running it. |
docker image prune | Delete dangling images only — the untagged leftovers of rebuilds. |
docker container prune | Delete every stopped container. |
docker volume prune | Delete volumes that no container references. |
docker builder prune | Clear 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 editorDocker — 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.