Docker
The Docker commands and Dockerfile lines for images, containers, storage, networks and Compose.
Run containers
docker run -d --name web -p 8080:80 nginx:1.27Start a container in the background, publishing host port 8080 to container port 80
docker run --rm -it ubuntu:24.04 bashA throwaway interactive shell (removed on exit)
docker run -e KEY=value --env-file .env myappPass environment variables
docker run --memory 256m --cpus 0.5 myappLimit memory and CPU
docker run --restart unless-stopped myappRestart it after a crash or reboot
docker run -v "$PWD":/app -w /app node:20 npm testRun a command in a container against the current folder
Inspect and control
docker ps -aList containers, including stopped ones
docker logs -f --tail 100 webFollow the last 100 lines of a container's output
docker exec -it web shOpen a shell inside a running container
docker inspect webEverything about it as JSON (add --format '{{.State.Status}}' for one field)
docker stats --no-streamCPU and memory use right now
docker stop web && docker rm webStop and delete a container
docker cp web:/etc/nginx/nginx.conf .Copy a file out of a container
docker top webThe processes running inside it
Images
docker build -t myapp:1.0 .Build an image from the Dockerfile here
docker build --no-cache -t myapp:1.0 .Rebuild without reusing cached layers
docker imagesList local images
docker history myapp:1.0The layers of an image and their sizes
docker tag myapp:1.0 registry.example.com/team/myapp:1.0Give an image another name
docker push registry.example.com/team/myapp:1.0Upload an image to a registry (docker login first)
docker pull nginx:1.27Download an image
docker save -o app.tar myapp:1.0 | docker load -i app.tarMove an image as a file
Dockerfile
Each instruction is a layer. Put what changes least first, so the cache helps.
FROM node:20-slimThe base image: pin a version and prefer small ones
WORKDIR /appSet the folder for the following instructions
COPY package*.json ./ RUN npm ci --omit=dev COPY . .Install dependencies before copying the code, so they cache
ENV NODE_ENV=productionSet an environment variable
EXPOSE 3000Document the port the app listens on
USER nodeDo not run as root
HEALTHCHECK CMD wget -qO- http://localhost:3000/health || exit 1Let Docker know when the app is healthy
ENTRYPOINT ["node"] CMD ["server.js"]ENTRYPOINT is the program; CMD is its default arguments
FROM golang:1.22 AS build ... FROM gcr.io/distroless/static COPY --from=build /out/app /appA multi-stage build: ship only the result, not the compiler
.dockerignore: .git node_modules *.logKeep files out of the build context
Volumes and networks
docker volume create dataMake a named volume that outlives containers
docker run -v data:/var/lib/postgresql/data postgres:16Mount a volume for a database
docker run -v "$PWD/conf":/etc/app:ro myappBind-mount a folder read-only
docker network create backendMake a network; containers on it reach each other by name
docker run --network backend --name db postgres:16Attach a container to it
docker network inspect backendSee who is connected
Compose
docker compose up -d --buildBuild and start all services in the background
docker compose ps | docker compose logs -f webStatus and logs of the stack
docker compose exec web shA shell in a service
docker compose down -vStop everything and remove the volumes too
services: web: build: . ports: ["8080:3000"] depends_on: [db] db: image: postgres:16 volumes: [pgdata:/var/lib/postgresql/data] volumes: pgdata:A compose.yaml with an app and a database
Clean up
docker system dfHow much space images, containers and volumes take
docker image prune -aDelete images no container uses
docker container pruneDelete all stopped containers
docker system prune --volumesDelete unused containers, networks, images and volumes (check first)
Nothing matches that search.
Cheatsheet for the Docker path · Deboistech · All cheatsheets