Docker Setup

Multi-stage Dockerfile and docker-compose for the bookmarks API with Postgres.

# Dockerfile
FROM golang:1.24-alpine AS builder

WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN CGO_ENABLED=0 go build -o /bin/bookmarks .

FROM alpine:3.21

COPY --from=builder /bin/bookmarks /bookmarks
COPY templates/ /templates/
COPY static/ /static/

EXPOSE 8080
ENTRYPOINT ["/bookmarks"]
# docker-compose.yml
services:
  db:
    image: postgres:17-alpine
    environment:
      POSTGRES_DB: bookmarks
      POSTGRES_USER: bookmarks
      POSTGRES_PASSWORD: localdev
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U bookmarks"]
      interval: 5s
      timeout: 3s
      retries: 5

  api:
    build: .
    ports:
      - "8080:8080"
    environment:
      PORT: "8080"
      DATABASE_URL: "postgres://bookmarks:localdev@db:5432/bookmarks?sslmode=disable"
    depends_on:
      db:
        condition: service_healthy

volumes:
  pgdata:

Run with docker compose up --build, then test: curl http://localhost:8080/health.

⚠️ Don't commit .env files with real credentials. Use platform secrets in production.

💻 Run locally

Copy the code above and run it on your machine

© 2026 ByteLearn.dev. Free courses for developers. · Privacy