RapidGo
No results found

Docker

Containerize your RapidGo application with Docker and Docker Compose.

The RapidGo starter includes a production-ready Dockerfile and Docker Compose configuration.

Dockerfile

The starter uses a multi-stage build for minimal image size:

# Build stage
FROM golang:1.25-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o server ./cmd

# Runtime stage
FROM alpine:3.19
RUN apk --no-cache add ca-certificates tzdata
WORKDIR /app
COPY --from=builder /app/server .
COPY --from=builder /app/resources ./resources
COPY --from=builder /app/.env.example .env.example
EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s \
    CMD wget -qO- http://localhost:8080/health || exit 1

CMD ["./server", "serve"]

Key points:

  • Multi-stage build — Go binary is compiled in the builder stage, only the binary and resources are copied to the runtime image
  • Alpine-based — Minimal image size (~20MB)
  • CGO disabled — Static binary, no C dependencies
  • Health check — Built-in Docker health check using the /health endpoint
  • CA certificates — Included for HTTPS outbound connections
  • Timezone data — Included for correct time handling

Docker Compose

services:
  app:
    build: .
    ports:
      - "8080:8080"
    env_file: .env
    volumes:
      - ./storage:/app/storage
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASS}
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
      interval: 5s
      timeout: 5s
      retries: 5
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    restart: unless-stopped

volumes:
  pgdata:

Building and Running

# Build and start all services
docker compose up -d --build

# View logs
docker compose logs -f app

# Run migrations inside the container
docker compose exec app ./server migrate

# Run seeders
docker compose exec app ./server db:seed

# Rebuild after code changes
docker compose build --no-cache app
docker compose up -d app

SQLite with Docker

For SQLite deployments, remove the db and redis services and mount the storage volume:

services:
  app:
    build: .
    ports:
      - "8080:8080"
    env_file: .env
    volumes:
      - ./storage:/app/storage
    restart: unless-stopped
DB_DRIVER=sqlite
DB_NAME=storage/database/app.db

Environment Variables

Create a .env file from the example:

cp .env.example .env
# Edit .env with production values

Never commit .env to version control. Use .env.example as a template.