docker \
run
docker run -d --name web -p 8080:80 nginxbackground, host 8080 to 80docker run --rm -it alpine shthrowaway shelldocker run -d --restart unless-stopped appback after crashes and rebootsdocker run -e LOG=debug --env-file .env appenv vars, inline or from a filedocker run -v data:/var/lib/app appnamed volumedocker run -v "$PWD":/src -w /src appbind mount the current dirdocker run --network appnet appjoin a user network
containers
docker ps -astopped ones toodocker stop websigterm, sigkill after 10sdocker start webdocker rm -f webkill + removedocker logs -f --tail 100 weblast 100 lines, then followdocker exec -it web shshell in a running containerdocker cp web:/etc/nginx/nginx.conf .copy out; swap the args to copy indocker stats --no-streamcpu and memory, oncedocker inspect webfull config as json
images
docker imageslocal images and sizesdocker pull nginx:alpinedownload without runningdocker tag app:1.0 registry.example.com/team/app:1.0name it for a registrydocker login registry.example.comcredentials for push and pulldocker push registry.example.com/team/app:1.0uploaddocker rmi app:olddelete a local image
build
docker build -t app:1.0 .Dockerfile in the current dirdocker build -f Dockerfile.prod -t app .another Dockerfiledocker build --build-arg VERSION=1.2 -t app .sets ARG VERSIONdocker build --no-cache -t app .rerun every stepdocker build --platform linux/amd64,linux/arm64 -t registry.example.com/app:1.0 --push .multi-arch, straight to the registry
dockerfile
# build stage: the full go toolchain
FROM golang:1.27-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /app .
# final stage: only the binary
FROM alpine:3.24
RUN adduser -D -H -u 10001 app
COPY --from=build /app /usr/local/bin/app
USER app
EXPOSE 8080
ENTRYPOINT ["app"]
compose
docker compose up -dbuild if needed, start in the backgrounddocker compose up -d --buildrebuild firstdocker compose downstop, remove containers and networksdocker compose down -valso named volumesdocker compose psservices and their statedocker compose logs -f apifollow one servicedocker compose exec api shshell in a running servicedocker compose run --rm api npm testone-off containerdocker compose pullnewer images; then up -d
compose.yaml
services:
api:
build: . # rebuilt by up --build
ports:
- "8080:8080"
environment:
DATABASE_URL: postgres://app:app@db/app
depends_on:
db:
condition: service_healthy # waits for it
restart: unless-stopped
db:
image: postgres:18
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app # example only
volumes:
- pgdata:/var/lib/postgresql # 18+ path
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 5s
volumes:
pgdata: # kept by down, removed by down -v
volumes & networks
docker volume lsevery volumedocker volume create datadocker run -v data:/path makes it toodocker volume rm datadeletes its datadocker network lsdocker network create appnetcontainers on it reach each other by namedocker network connect appnet webattach a running container
cleanup
docker system dfwhat takes the spacedocker system prunestopped containers, unused networks, dangling images, build cachedocker system prune -a --volumesplus unused images and anonymous volumesdocker image prune -aevery image without a container