-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile
More file actions
155 lines (112 loc) · 4.75 KB
/
Dockerfile
File metadata and controls
155 lines (112 loc) · 4.75 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# VoidRunner Multi-Stage Dockerfile
# Supports development, production, and scheduler targets
# =============================================================================
# Base builder stage
# =============================================================================
FROM golang:1.24.5-alpine AS builder
# Install build dependencies
RUN apk --no-cache add ca-certificates git make
# Set working directory
WORKDIR /app
# Copy go mod files first for better caching
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy source code
COPY . .
# Build all binaries
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o bin/api cmd/api/main.go
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o bin/scheduler cmd/scheduler/main.go
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o bin/migrate cmd/migrate/main.go
# =============================================================================
# Development stage
# =============================================================================
FROM golang:1.24.5-alpine AS development
# Install development tools
RUN apk --no-cache add ca-certificates curl git make bash
# Install air for live reloading (optional)
RUN go install github.com/air-verse/air@latest
# Create non-root user
RUN addgroup -g 1001 -S voidrunner && \
adduser -u 1001 -S voidrunner -G voidrunner
# Set working directory
WORKDIR /app
# Copy go mod files first for better caching
COPY --chown=voidrunner:voidrunner go.mod go.sum ./
# Create logs and tmp directories, and set up Go module cache permissions
RUN mkdir -p logs tmp && \
chown voidrunner:voidrunner logs tmp && \
mkdir -p /go/pkg/mod/cache && \
chown -R voidrunner:voidrunner /go/pkg/mod
# Switch to non-root user
USER voidrunner
# Download dependencies as non-root user
RUN go mod download
# Copy source code for development
COPY --chown=voidrunner:voidrunner . .
# Expose port
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
# Development entry point (use Go directly for now)
CMD ["go", "run", "cmd/api/main.go"]
# =============================================================================
# Production base stage
# =============================================================================
FROM alpine:3.22 AS base
# Install runtime dependencies
RUN apk --no-cache add ca-certificates curl tzdata
# Create non-root user
RUN addgroup -g 1001 -S voidrunner && \
adduser -u 1001 -S voidrunner -G voidrunner
# Set working directory
WORKDIR /app
# Create necessary directories
RUN mkdir -p logs && chown voidrunner:voidrunner logs
# Switch to non-root user
USER voidrunner
# =============================================================================
# Production API stage
# =============================================================================
FROM base AS production
# Copy API binary from builder
COPY --from=builder --chown=voidrunner:voidrunner /app/bin/api ./api
# Expose port
EXPOSE 8080
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
# Run the API server
CMD ["./api"]
# =============================================================================
# Scheduler stage (for future distributed mode)
# =============================================================================
FROM base AS scheduler
# Copy scheduler binary from builder
COPY --from=builder --chown=voidrunner:voidrunner /app/bin/scheduler ./scheduler
# Health check for scheduler (no HTTP endpoint, check process)
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD pgrep scheduler || exit 1
# Run the scheduler service
CMD ["./scheduler"]
# =============================================================================
# Migration stage (for database migrations)
# =============================================================================
FROM base AS migration
# Copy migration binary from builder
COPY --from=builder --chown=voidrunner:voidrunner /app/bin/migrate ./migrate
# Copy migration files
COPY --chown=voidrunner:voidrunner migrations/ ./migrations/
# Default command (can be overridden)
CMD ["./migrate", "up"]
# =============================================================================
# All-in-one stage (includes all binaries for flexibility)
# =============================================================================
FROM base AS all
# Copy all binaries from builder
COPY --from=builder --chown=voidrunner:voidrunner /app/bin/ ./bin/
# Copy migration files
COPY --chown=voidrunner:voidrunner migrations/ ./migrations/
# Default to API server
CMD ["./bin/api"]