-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
67 lines (49 loc) · 1.66 KB
/
Dockerfile
File metadata and controls
67 lines (49 loc) · 1.66 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
# Multi-stage build for optimal image size
# Stage 1: Build stage
FROM golang:1.21-alpine AS builder
# Build arguments for versioning
ARG VERSION=dev
ARG GIT_COMMIT=unknown
ARG BUILD_TIME=unknown
# Install build dependencies
RUN apk add --no-cache git make
# Set working directory
WORKDIR /app
# Copy go mod files first for better layer caching
COPY go.mod go.sum ./
# Download dependencies (cached unless go.mod/go.sum change)
RUN go mod download && go mod verify
# Copy source code
COPY . .
# Build the application with version info and optimizations
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-a -installsuffix cgo \
-ldflags="-w -s \
-X main.Version=${VERSION} \
-X main.GitCommit=${GIT_COMMIT} \
-X main.BuildTime=${BUILD_TIME}" \
-o shotify ./cmd/main.go
# Stage 2: Production stage (distroless for minimal attack surface)
FROM alpine:3.19
# Install ca-certificates for HTTPS and timezone data
RUN apk --no-cache add ca-certificates tzdata curl && \
rm -rf /var/cache/apk/*
# Create a non-root user for security
RUN addgroup -g 1001 -S appuser && \
adduser -u 1001 -S appuser -G appuser
WORKDIR /app
# Create uploads directory with proper permissions
RUN mkdir -p /app/uploads && chown -R appuser:appuser /app
# Copy the binary from builder stage
COPY --from=builder /app/shotify .
# Change ownership to non-root user
RUN chown appuser:appuser /app/shotify
# Switch to non-root user
USER appuser
# Expose port (configurable via PORT env)
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
# Run the application
CMD ["./shotify"]