Optimize Dockerfile build process and add static assets

- 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
This commit is contained in:
2026-01-26 11:29:12 -05:00
parent 068d686fa1
commit e8a8db72a4
+14 -14
View File
@@ -3,33 +3,30 @@ FROM golang:1.25-alpine AS builder
WORKDIR /app WORKDIR /app
# Install Node.js, npm, and curl for Tailwind CSS building
RUN apk add --no-cache nodejs npm curl
# Install sqlc # Install sqlc
RUN go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest RUN go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
# Install templ # Install templ
RUN go install github.com/a-h/templ/cmd/templ@latest 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 source code
COPY . . COPY . .
# Download dependencies # Download dependencies
RUN go mod tidy RUN go mod tidy
# Install Node.js and npm for Tailwind CSS building
RUN apk add --no-cache nodejs npm
# Copy package files and install npm dependencies
COPY package*.json ./
RUN npm install
# Copy source code (after npm to avoid re-running npm install on code changes)
COPY . .
# Generate sqlc code # Generate sqlc code
RUN sqlc generate RUN cd internal/database && sqlc generate
# Generate templ code # Generate templ code
RUN templ generate RUN cd templates && templ generate
# Build Tailwind CSS and download htmx # Build Tailwind CSS and download htmx
RUN npm run build:css:prod RUN npm run build:css:prod
@@ -41,7 +38,7 @@ RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./cmd/server
# Final stage # Final stage
FROM alpine:latest FROM alpine:latest
RUN apk --no-cache add ca-certificates RUN apk --no-cache add ca-certificates curl
WORKDIR /root/ WORKDIR /root/
# Copy the binary from builder stage # Copy the binary from builder stage
@@ -50,9 +47,12 @@ COPY --from=builder /app/main .
# Copy database schema (if needed for initialization) # Copy database schema (if needed for initialization)
COPY --from=builder /app/database/schema ./database/schema COPY --from=builder /app/database/schema ./database/schema
# Copy templates # Copy generated templates
COPY --from=builder /app/templates ./templates COPY --from=builder /app/templates ./templates
# Copy static assets
COPY --from=builder /app/cmd/server/static ./static
# Expose port # Expose port
EXPOSE 8765 EXPOSE 8765