Skip to content

Docker & Compose ​


Multi-Stage Dockerfile ​

goforge-template/Dockerfile builds a minimal, non-root Alpine image:

dockerfile
# Stage 1: Build
FROM golang:1.25-alpine AS builder
WORKDIR /app

ENV GOOS=linux \
    GO111MODULE=on \
    CGO_ENABLED=1

# Required for mattn/go-sqlite3
RUN apk add --no-cache gcc musl-dev

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

COPY . .
RUN go build -tags prod -ldflags="-s -w" -o goforge ./app

# Stage 2: Runtime
FROM alpine:3.21
WORKDIR /app

RUN apk add --no-cache ca-certificates tzdata && \
    adduser -D -g '' appuser

COPY --from=builder /app/goforge .

USER appuser

EXPOSE 3000 50051

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:3000/health || exit 1

ENTRYPOINT ["./goforge"]
CMD ["serve", "--no-tui"]

docker-compose.yaml (PostgreSQL + App) ​

yaml
services:
  postgres:
    image: postgres:17-alpine
    environment:
      POSTGRES_USER: goforge
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: goforge
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U goforge"]
      interval: 5s
      timeout: 3s
      retries: 5
    volumes:
      - pgdata:/var/lib/postgresql/data

  migrate:
    build: .
    command: ["migrate"]
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      APP_KEY: ${APP_KEY}
      DB_CONNECTION: postgres
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: goforge
      DB_USERNAME: goforge
      DB_PASSWORD: secret
      DB_SSL_MODE: disable
      DB_MIGRATOR: gorm

  app:
    build: .
    depends_on:
      migrate:
        condition: service_completed_successfully
    ports:
      - "3000:3000"
      - "50051:50051"
    environment:
      APP_KEY: ${APP_KEY}
      APP_DEBUG: "false"
      APP_HOST: 0.0.0.0
      GRPC_HOST: 0.0.0.0
      DB_CONNECTION: postgres
      DB_HOST: postgres
      DB_PORT: 5432
      DB_NAME: goforge
      DB_USERNAME: goforge
      DB_PASSWORD: secret
      DB_SSL_MODE: disable
      LOG_FORMAT: json
      LOG_COLOR: never

volumes:
  pgdata:

Released under the Apache-2.0 License.