Skip to content

Commit

Permalink
♻️ refactor(Dockerfile): simplify build process using multi-stage build
Browse files Browse the repository at this point in the history
  • Loading branch information
watzon committed Nov 13, 2024
1 parent 2df4f45 commit 9604aaa
Showing 1 changed file with 6 additions and 49 deletions.
55 changes: 6 additions & 49 deletions Dockerfile
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"]

0 comments on commit 9604aaa

Please sign in to comment.