-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use multi-stage build docker image
- Loading branch information
YBR Dev
committed
Oct 6, 2024
1 parent
c7db7d9
commit 7269040
Showing
1 changed file
with
30 additions
and
3 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,7 +1,34 @@ | ||
FROM golang:alpine | ||
RUN apk update && apk add --no-cache git | ||
# Stage 1: Build Stage | ||
FROM golang:alpine AS builder | ||
RUN apk add --no-cache git | ||
WORKDIR /app | ||
|
||
# Copy go.mod to cache dependencies | ||
COPY go.mod ./ | ||
RUN go mod download | ||
|
||
# Copy the rest of the source code | ||
COPY . . | ||
RUN go mod tidy | ||
|
||
# Build the Go binary and name it 'binary' | ||
RUN go build -o binary | ||
|
||
# Stage 2: Minimal Runtime Stage | ||
FROM alpine:latest | ||
WORKDIR /app | ||
|
||
# Copy the binary from the builder stage | ||
COPY --from=builder /app/binary /app/binary | ||
|
||
# Copy the SSL certificate and key files into the image | ||
COPY server.crt server.key /app/ | ||
|
||
# Set a non-root user for security | ||
RUN addgroup -S appgroup && adduser -S appuser -G appgroup | ||
USER appuser | ||
|
||
# Expose port 443 for HTTPS | ||
EXPOSE 443 | ||
|
||
# Set the entry point to run the 'binary' binary | ||
ENTRYPOINT ["/app/binary"] |