-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.example
More file actions
56 lines (39 loc) · 1.09 KB
/
Dockerfile.example
File metadata and controls
56 lines (39 loc) · 1.09 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
# Build stage
FROM golang:1.21-alpine AS builder
WORKDIR /app
# Install required build tools
RUN apk add --no-cache git make
# Copy go mod and sum files
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy source code
COPY . .
# Build the application with optimizations
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o main .
# Final stage
FROM alpine:latest
WORKDIR /app
# Add necessary runtime dependencies and security updates
RUN apk --no-cache add ca-certificates tzdata && \
apk --no-cache upgrade
# Create a non-root user
RUN adduser -D appuser
# Copy the binary from builder
COPY --from=builder /app/main .
# Copy config files
COPY --from=builder /app/config.yaml ./config.yaml
# Set ownership
RUN chown -R appuser:appuser /app
# Switch to non-root user
USER appuser
# Expose port
EXPOSE 8080
# These are example environment variables. Replace with your actual values in production
ENV DB_HOST=your_db_host \
DB_PORT=3306 \
DB_USER=your_username \
DB_NAME=your_database \
SERVER_PORT=8080
# Run the application
CMD ["./main"]