Docker Cheatsheet

View saved

Docker packages an app with its runtime into an image. You start containers from that image so the same build can run on your laptop and a server.

Learn a few everyday commands well: run, logs, exec, stop, and prune. Keep secrets out of images and prefer Compose when more than one service needs to start together.

Concepts

Image vs container

An image is a read-only template. A container is a running (or stopped) instance created from an image.

docker images
docker ps -a

docker version / info

Confirm the client and daemon are working and see basic host details.

docker version
docker info

Registry

A place that hosts images (Docker Hub is the default public one). You pull from and push to registries.

docker pull nginx:alpine

Tags

Labels on an image such as nginx:1.27 or nginx:alpine. Prefer specific tags over floating latest in real projects.

docker pull python:3.12-slim

Layers

Images are stacked filesystem layers. Reusing unchanged layers makes rebuilds faster.

# Each Dockerfile instruction often becomes a layer
FROM python:3.12-slim
COPY requirements.txt .
RUN pip install -r requirements.txt

Run & manage

docker run

Create and start a container. Flags control name, ports, detach mode, and cleanup.

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

docker ps

List running containers. Add -a to include stopped ones.

docker ps
docker ps -a

docker logs

Print stdout/stderr from a container. Follow with -f while debugging.

docker logs web
docker logs -f --tail 100 web

docker exec

Run a command inside a running container (often an interactive shell).

docker exec -it web sh

docker stop / start

Gracefully stop a container, or start a previously stopped one again.

docker stop web
docker start web

Ports and volumes

Map host:container ports with -p. Persist files with -v so data survives container removal.

docker run -d -p 3000:3000 -v app-data:/data myapp:1.0

Images

docker pull

Download an image (and its layers) from a registry without starting a container.

docker pull redis:7-alpine

docker images

List local images with tags, IDs, and sizes.

docker images

docker build

Build an image from a Dockerfile in the current directory. -t sets a name:tag.

docker build -t myapp:1.0 .

docker tag / push

Retag for your registry path, then upload.

docker tag myapp:1.0 myuser/myapp:1.0
docker push myuser/myapp:1.0

docker rmi

Remove a local image. Failures usually mean a container still references it.

docker rmi myapp:1.0

Dockerfile

FROM

Start from a base image. Slim or alpine variants keep images smaller when they fit your needs.

FROM node:20-alpine

WORKDIR / COPY / RUN

Set the working directory, copy files, and run build steps during image creation.

WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .

CMD vs ENTRYPOINT

CMD is the default command; ENTRYPOINT is harder to override and often wraps the main binary.

CMD ["node", "server.js"]

EXPOSE

Documents which port the app listens on inside the container. Still publish with -p at run time.

EXPOSE 3000

.dockerignore

Exclude files from the build context (like .git and local node_modules) so builds stay fast and clean.

# .dockerignore
.git
node_modules
.env
*.md

Compose

docker compose up

Start the services defined in compose.yaml (or docker-compose.yml). Use -d for background.

docker compose up -d

Minimal compose file

Declare services, images or builds, ports, and environment in one place.

services:
  web:
    build: .
    ports:
      - "8000:8000"
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: example

docker compose ps / logs

See service status and combined or per-service logs.

docker compose ps
docker compose logs -f web

docker compose down

Stop and remove containers created by the project. Add -v only when you intend to delete named volumes too.

docker compose down

Env files

Keep secrets and local overrides in .env (do not commit real secrets). Compose reads them for variable substitution.

# .env
POSTGRES_PASSWORD=change-me
# compose: POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

Cleanup

docker rm

Delete a stopped container by name or ID.

docker rm web

docker container prune

Remove all stopped containers after confirmation.

docker container prune

docker image prune

Delete dangling images. Add -a to remove images not used by any container (more aggressive).

docker image prune
docker image prune -a

docker volume prune

Remove unused volumes. Double-check—this deletes persisted data that no container claims.

docker volume prune

docker system prune

Broad cleanup of unused containers, networks, and dangling images. Add --volumes only when you mean it.

docker system prune
# docker system prune --volumes   # also unused volumes

Comments

One comment per signed-in account. Comments are saved with this page’s URL.