-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile
More file actions
68 lines (46 loc) · 1.52 KB
/
Copy pathDockerfile
File metadata and controls
68 lines (46 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Multi-stage build for Go application
# Stage 1: Build stage
FROM golang:1.24-alpine AS builder
# Install build dependencies
RUN apk add --no-cache git build-base
# Set working directory
WORKDIR /app
# Copy go mod and sum files
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Install swag for generating documentation
RUN go install github.com/swaggo/swag/cmd/swag@latest
# Copy source code
COPY . .
# Generate swagger documentation
RUN swag init -g main/main.go -o ./docs
# Create build directory (matching Makefile structure)
RUN mkdir -p build
# Build the application (following Makefile build command)
RUN go build -o build/API main/*
# Stage 2: Runtime stage
FROM alpine:latest
# Install ca-certificates for HTTPS calls
RUN apk --no-cache add ca-certificates
# Create a non-root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# Set working directory
WORKDIR /app
# Copy the binary from builder stage
COPY --from=builder /app/build/API ./API
# Copy the the template directory
COPY --from=builder /app/templates ./templates
# Copy docs directory for swagger documentation
COPY --from=builder /app/docs ./docs
# Change ownership to non-root user
RUN chown -R appuser:appgroup /app
# Switch to non-root user
USER appuser
# Expose port 8000 (as defined in main.go)
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8000/api/health || exit 1
# Command to run the application
CMD ["./API"]