-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDockerfile.prod
More file actions
134 lines (102 loc) · 3.42 KB
/
Copy pathDockerfile.prod
File metadata and controls
134 lines (102 loc) · 3.42 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
# ==============================================================================
# Production Dockerfile for AgentOS
# Multi-stage build with security optimizations
# ==============================================================================
# Build stage: Use specific Go version with security updates
FROM golang:1.22-alpine AS builder
# Set environment variables for Go
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOPROXY=https://goproxy.cn,direct \
GOPRIVATE=*.github.com
# Install build dependencies (minimal)
RUN apk add --no-cache \
git \
ca-certificates \
tzdata \
make \
curl \
&& rm -rf /var/cache/apk/*
# Create non-root user for builder
RUN addgroup -g 1000 -S builder && \
adduser -u 1000 -S builder -G builder
# Set working directory
WORKDIR /workspace
# Copy only dependency files first (for better caching)
COPY go.mod go.sum ./
# Download dependencies with checksum verification
RUN go mod download && go mod verify
# Copy source code
COPY . .
# Run security checks and tests
RUN go vet ./... && \
go test -short ./...
# Build the application with security flags
RUN GOOS=linux GOARCH=amd64 go build \
-trimpath \
-ldflags="-w -s \
-X main.Version=${VERSION:-unknown} \
-X main.Commit=${COMMIT:-unknown} \
-X main.BuildTime=${BUILD_TIME:-unknown}" \
-buildvcs=true \
-o /app/aos ./cmd/aos
# Security scanning stage
FROM aquasec/trivy:0.49.1 AS scanner
COPY --from=builder /app/aos /scan/
RUN trivy filesystem --security-checks vuln,config,secret --no-progress --exit-code 0 /scan/
# Final stage: Use distroless base for minimal attack surface
FROM gcr.io/distroless/static:nonroot AS production
# Copy CA certificates
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Copy binary from builder
COPY --from=builder /app/aos /usr/local/bin/aos
# Create necessary directories
USER nonroot:nonroot
# Set environment variables
ENV PATH="/usr/local/bin:${PATH}" \
AOS_CONFIG_PATH="/app/configs/config.yaml"
# Expose default ports
EXPOSE 8080 8081
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
CMD ["/usr/local/bin/aos", "health"]
# Entrypoint
ENTRYPOINT ["/usr/local/bin/aos"]
# Default command
CMD ["--config", "/app/configs/config.yaml"]
# ------------------------------------------------------------------------------
# Development stage (for debugging)
# ------------------------------------------------------------------------------
FROM alpine:3.19 AS development
# Install runtime dependencies
RUN apk add --no-cache \
ca-certificates \
tzdata \
curl \
bind-tools \
busybox-extras \
&& rm -rf /var/cache/apk/*
# Create non-root user
RUN addgroup -g 1000 -S aos && \
adduser -u 1000 -S aos -G aos
# Set working directory
WORKDIR /app
# Copy binary and configuration
COPY --from=builder --chown=aos:aos /app/aos /usr/local/bin/aos
COPY --chown=aos:aos configs/ /app/configs/
# Copy CA certificates
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Create necessary directories
RUN mkdir -p /app/logs /app/data && \
chown -R aos:aos /app
# Switch to non-root user
USER aos
# Expose ports
EXPOSE 8080 8081
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget -qO- http://localhost:8080/health || exit 1
# Entrypoint
ENTRYPOINT ["aos"]
# Default command
CMD ["--config", "/app/configs/config.yaml"]