Skip to content

Docker cheat sheet

The docker CLI commands you actually need, plus Dockerfile instructions. For multi-container setups see the Docker Compose cheat sheet.

20 entries

Run containers

docker run -d --name web -p 8080:80 nginx

Detached, named, host port 8080 → container 80.

docker run --rm -it python:3.12 bash

Interactive throwaway container.

docker run -e NODE_ENV=production --env-file .env app

Environment variables.

docker run -v $(pwd):/app -w /app node:22 npm test

Bind-mount the current folder.

Manage containers

docker ps
docker ps -a

Running / all containers.

docker logs -f web

Follow logs.

docker exec -it web sh

Shell inside a running container.

docker stop web
docker start web
docker rm web

Stop, start, remove.

docker inspect web
docker stats

Details; live resource usage.

Images

docker build -t myapp:1.0 .

Build from the Dockerfile in this folder.

docker images
docker rmi myapp:1.0

List / remove images.

docker tag myapp:1.0 ghcr.io/me/myapp:1.0
docker push ghcr.io/me/myapp:1.0

Tag and push to a registry.

docker pull postgres:16

Download an image.

Volumes & networks

docker volume create pgdata
docker run -v pgdata:/var/lib/postgresql/data postgres:16

Named volume survives container removal.

docker network create appnet
docker run --network appnet --name db postgres:16

Containers on one network reach each other by name.

Cleanup

docker system df

Disk used by images, containers, volumes.

docker system prune

Remove stopped containers, unused networks, dangling images.

docker image prune -a

Remove all unused images.

Dockerfile

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER 1000
EXPOSE 8000
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]

Copy dependency files first so the install layer is cached.

FROM node:22 AS build
WORKDIR /app
COPY . .
RUN npm ci && npm run build

FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html

Multi-stage build keeps the final image small.

Frequently asked questions

What is the difference between an image and a container?

An image is a read-only template (layers of files plus metadata). A container is a running (or stopped) instance of an image with its own writable layer.

CMD vs ENTRYPOINT?

ENTRYPOINT sets the executable that always runs; CMD supplies default arguments that docker run can override. Use exec form (JSON array) for both so signals reach your process.

How do I free disk space used by Docker?

Check with docker system df, then run docker system prune. Add -a to also remove unused images and --volumes to remove unused volumes (careful: that deletes data).

Related cheat sheets