51 lines
1010 B
Docker
51 lines
1010 B
Docker
# Build stage
|
|
FROM --platform=$BUILDPLATFORM golang:1.21-alpine AS builder
|
|
|
|
# Install git and ca-certificates
|
|
RUN apk --no-cache add git ca-certificates
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy go mod files
|
|
COPY go.mod go.sum ./
|
|
|
|
# Download dependencies
|
|
RUN go mod download
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Build the application for target platform
|
|
ARG TARGETPLATFORM
|
|
ARG BUILDPLATFORM
|
|
ARG TARGETOS
|
|
ARG TARGETARCH
|
|
|
|
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -installsuffix cgo -o hello-api main.go
|
|
|
|
# Final stage
|
|
FROM alpine:latest
|
|
|
|
# Install jq and ca-certificates
|
|
RUN apk --no-cache add ca-certificates jq
|
|
|
|
# Create non-root user
|
|
RUN addgroup -g 1001 -S appgroup && \
|
|
adduser -u 1001 -S appuser -G appgroup
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy binary from builder stage
|
|
COPY --from=builder /app/hello-api .
|
|
|
|
# Change ownership to non-root user
|
|
RUN chown appuser:appgroup hello-api
|
|
|
|
# Switch to non-root user
|
|
USER appuser
|
|
|
|
# Expose port (if your app uses one)
|
|
EXPOSE 8080
|
|
|
|
# Run the application
|
|
CMD ["./hello-api"] |