- Reorder package installation to improve layer caching - Add curl to runtime dependencies - Use relative paths for sqlc and templ generation - Copy static assets to final stage - Improve build step organization
60 lines
1.2 KiB
Docker
60 lines
1.2 KiB
Docker
# Build stage
|
|
FROM golang:1.25-alpine AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# Install Node.js, npm, and curl for Tailwind CSS building
|
|
RUN apk add --no-cache nodejs npm curl
|
|
|
|
# Install sqlc
|
|
RUN go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
|
|
|
|
# Install templ
|
|
RUN go install github.com/a-h/templ/cmd/templ@latest
|
|
|
|
# Copy package files and install npm dependencies
|
|
COPY package*.json ./
|
|
RUN npm install
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Download dependencies
|
|
RUN go mod tidy
|
|
|
|
# Generate sqlc code
|
|
RUN cd internal/database && sqlc generate
|
|
|
|
# Generate templ code
|
|
RUN cd templates && templ generate
|
|
|
|
# Build Tailwind CSS and download htmx
|
|
RUN npm run build:css:prod
|
|
RUN npm run postinstall
|
|
|
|
# Build the application
|
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./cmd/server
|
|
|
|
# Final stage
|
|
FROM alpine:latest
|
|
|
|
RUN apk --no-cache add ca-certificates curl
|
|
WORKDIR /root/
|
|
|
|
# Copy the binary from builder stage
|
|
COPY --from=builder /app/main .
|
|
|
|
# Copy database schema (if needed for initialization)
|
|
COPY --from=builder /app/database/schema ./database/schema
|
|
|
|
# Copy generated templates
|
|
COPY --from=builder /app/templates ./templates
|
|
|
|
# Copy static assets
|
|
COPY --from=builder /app/cmd/server/static ./static
|
|
|
|
# Expose port
|
|
EXPOSE 8765
|
|
|
|
# Run the binary
|
|
CMD ["./main"] |