-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
69 lines (50 loc) · 1.93 KB
/
Copy pathDockerfile
File metadata and controls
69 lines (50 loc) · 1.93 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
# Multi-stage build for netscan
# Stage 1: Build the Go binary
FROM golang:1.26-alpine AS builder
# Install build dependencies
RUN apk add --no-cache git ca-certificates
# Set working directory
WORKDIR /build
# Copy go mod files first for better caching
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Version build argument
ARG VERSION=1.0.0
# Build the binary with optimizations for linux/amd64
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags="-w -s -X github.com/kljama/netscan/internal/version.Version=${VERSION}" \
-o netscan \
./cmd/netscan
# Stage 2: Create minimal runtime image
FROM alpine:latest
# Install runtime dependencies (including wget for healthcheck)
RUN apk add --no-cache ca-certificates libcap wget \
&& apk upgrade --no-cache zlib
# Create non-root user for running the service
RUN addgroup -S netscan && adduser -S netscan -G netscan
# Set working directory
WORKDIR /app
# Copy binary from builder stage
COPY --from=builder /build/netscan /app/netscan
# Copy config template
COPY config.yml.example /app/config.yml.example
# Set CAP_NET_RAW capability for ICMP access
RUN setcap cap_net_raw+ep /app/netscan
# Change ownership to non-root user
RUN chown -R netscan:netscan /app
# NOTE: Running as root is required for ICMP raw socket access in Docker
# Even with CAP_NET_RAW capability, non-root users cannot create raw ICMP sockets
# This is a limitation of the Linux kernel's security model in containerized environments
# USER netscan # Commented out - must run as root for ICMP to work
# Set default config path (can be overridden with -config flag)
ENV CONFIG_PATH=/app/config.yml
# Expose health check port
EXPOSE 8080
# Add health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health/live || exit 1
# Run netscan
ENTRYPOINT ["/app/netscan"]
CMD ["-config", "/app/config.yml"]