File size: 1,462 Bytes
93d826e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 example
FROM golang:1.21-alpine AS builder

# Build arguments
ARG VERSION=1.0.0
ARG BUILD_DATE

# Set working directory
WORKDIR /app

# Copy only necessary files for dependency resolution
COPY go.mod go.sum ./

# Download dependencies
RUN go mod download

# Copy source code
COPY . .

# Build the application
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-X main.Version=${VERSION} -X main.BuildDate=${BUILD_DATE}" -o /app/server

# Create final lightweight image
FROM alpine:latest

# Labels for metadata
LABEL maintainer="example@example.com" \
      version="${VERSION}" \
      description="Example Dockerfile with various syntax elements"

# Environment variables
ENV APP_ENV=production \
    PORT=8080

# Create non-root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# Install runtime dependencies
RUN apk add --no-cache \
    ca-certificates \
    tzdata

# Set working directory
WORKDIR /app

# Copy binary from builder stage
COPY --from=builder /app/server .

# Copy configuration files
COPY config/production.yaml /etc/app/config.yaml

# Create volume mount points
VOLUME ["/data", "/logs"]

# Expose ports
EXPOSE 8080 8443

# Switch to non-root user
USER appuser

# Health check
HEALTHCHECK --interval=30s --timeout=3s \
    CMD wget --quiet --tries=1 --spider http://localhost:${PORT}/health || exit 1

# Set entry point and default command
ENTRYPOINT ["/app/server"]
CMD ["--config", "/etc/app/config.yaml"]