Containers
| Command | What it does |
|---|
docker run -d --name web -p 8080:80 nginx | run in the background, publish a port |
docker run --rm -it ubuntu bash | a throwaway interactive shell |
docker ps / docker ps -a | running / all containers |
docker logs -f web | follow output |
docker exec -it web sh | a shell inside a running container |
docker stop web / docker rm web | stop / remove |
docker inspect web | everything about it, as JSON |
docker stats | live CPU and memory |
Images
| Command | What it does |
|---|
docker build -t app:1.0 . | build from the Dockerfile here |
docker build --target build . | build one stage only |
docker images / docker history app:1.0 | list / size per layer |
docker tag app:1.0 registry/app:1.0 / docker push … | name / upload |
docker image prune | remove dangling images |
Dockerfile
FROM golang:1.23 AS build # a named build stage
WORKDIR /src
COPY go.mod go.sum ./ # dependency files first, for the cache
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app .
FROM alpine:3.20 # the image you ship
RUN adduser -D app
USER app
COPY --from=build /out/app /usr/local/bin/app
EXPOSE 8080
ENTRYPOINT ["app"]
CMD is the default command and can be overridden; ENTRYPOINT is the program. Add a .dockerignore.
Volumes and networks
| Command | What it does |
|---|
docker volume create data | a named volume |
-v data:/var/lib/postgresql/data | mount a volume |
-v "$PWD":/app | bind-mount a host folder |
docker network create appnet | a user-defined network |
--network appnet | containers here find each other by name |
Compose
| Command | What it does |
|---|
docker compose up -d | start the stack |
docker compose ps / logs -f api | status / one service's log |
docker compose down / down -v | stop / and delete volumes |
docker compose build / up -d --build | rebuild images |
services:
api:
build: .
ports: ["8000:8000"]
environment: { DATABASE_URL: postgres://app:secret@db/app }
depends_on: { db: { condition: service_healthy } }
db:
image: postgres:16
environment: { POSTGRES_USER: app, POSTGRES_PASSWORD: secret }
volumes: [db:/var/lib/postgresql/data]
healthcheck: { test: ["CMD", "pg_isready", "-U", "app"], interval: 5s }
volumes: { db: {} }