-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor(Dockerfile): simplify build process using multi-stage build
- Loading branch information
Showing
1 changed file
with
6 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,13 @@ | ||
# Build stage | ||
FROM golang:1.23.3-alpine AS builder | ||
FROM golang:1.23.3 AS build | ||
|
||
# Install build dependencies | ||
RUN apk add --no-cache gcc musl-dev | ||
WORKDIR /build/src | ||
|
||
# Set working directory | ||
WORKDIR /app | ||
|
||
# Copy go mod files | ||
COPY go.mod go.sum ./ | ||
|
||
# Download dependencies | ||
RUN go mod download | ||
|
||
# Copy source code | ||
COPY . . | ||
|
||
# Build the application | ||
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/paste69 ./cmd/server | ||
|
||
# Final stage | ||
FROM alpine:3.19 | ||
|
||
# Install runtime dependencies | ||
RUN apk add --no-cache ca-certificates tzdata sqlite | ||
|
||
# Create app directory and uploads directory | ||
WORKDIR /app | ||
RUN mkdir -p /app/uploads | ||
|
||
# Create data directory for postgres | ||
RUN mkdir -p /app/data | ||
|
||
# Copy binary from builder | ||
COPY --from=builder /app/paste69 . | ||
|
||
# Copy config and views | ||
COPY views ./views | ||
COPY public ./public | ||
|
||
# Create non-root user | ||
RUN adduser -D -g '' appuser && \ | ||
chown -R appuser:appuser /app | ||
|
||
# Switch to non-root user | ||
USER appuser | ||
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o app ./cmd/server/main.go | ||
|
||
# Expose default port | ||
EXPOSE 3000 | ||
FROM scratch | ||
|
||
# Set environment variables | ||
ENV GIN_MODE=release | ||
COPY --from=build /build/src/app /usr/bin/app | ||
|
||
# Run the application | ||
CMD ["./paste69"] | ||
ENTRYPOINT ["/usr/bin/app"] |