46 lines
1.0 KiB
Docker
46 lines
1.0 KiB
Docker
FROM golang:1.23-alpine AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy go.mod and go.sum first to leverage Docker cache
|
|
COPY go.mod go.sum ./
|
|
RUN go mod download
|
|
|
|
# Copy the rest of the code
|
|
COPY . .
|
|
|
|
# Build the application
|
|
RUN go build -o app ./cmd/main.go
|
|
|
|
# Use a minimal alpine image for the final container
|
|
FROM alpine:latest
|
|
|
|
# Install necessary packages
|
|
RUN apk --no-cache add ca-certificates netcat-openbsd
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy the pre-built binary from the builder stage
|
|
COPY --from=builder /app/app .
|
|
|
|
# Copy source code and tests for testing
|
|
COPY --from=builder /app/pkg ./pkg
|
|
COPY --from=builder /app/api ./api
|
|
COPY --from=builder /app/tests ./tests
|
|
COPY --from=builder /app/scripts ./scripts
|
|
COPY --from=builder /app/go.mod ./go.mod
|
|
COPY --from=builder /app/go.sum ./go.sum
|
|
|
|
# Copy the Go binary and environment for tests
|
|
COPY --from=builder /usr/local/go /usr/local/go
|
|
ENV PATH="/usr/local/go/bin:${PATH}"
|
|
ENV GOPATH="/go"
|
|
|
|
# Set environment variables
|
|
ENV GO_ENV=production
|
|
|
|
# Expose the application port
|
|
EXPOSE 8080
|
|
|
|
# Command to run the application
|
|
CMD ["./app"] |