|
| 1 | + |
| 2 | +# Stage 1: Builder stage to compile and install tools |
| 3 | +FROM alpine:latest AS builder |
| 4 | + |
| 5 | +# Install dependencies needed for building tools |
| 6 | +RUN apk add --no-cache \ |
| 7 | + git \ |
| 8 | + go \ |
| 9 | + python3 \ |
| 10 | + nodejs \ |
| 11 | + npm \ |
| 12 | + openssl \ |
| 13 | + curl \ |
| 14 | + wget \ |
| 15 | + make \ |
| 16 | + gcc \ |
| 17 | + libc-dev \ |
| 18 | + # Clean up cache to reduce image size |
| 19 | + && rm -rf /var/cache/apk/* |
| 20 | + |
| 21 | +# Install Trivy (vulnerability scanner for containers) |
| 22 | +RUN wget -O /usr/local/bin/trivy https://github.com/aquasecurity/trivy/releases/latest/download/trivy_linux_64bit \ |
| 23 | + && chmod +x /usr/local/bin/trivy |
| 24 | + |
| 25 | +# Install Kube-Bench (Kubernetes security benchmark tool) |
| 26 | +RUN git clone https://github.com/aquasecurity/kube-bench.git /opt/kube-bench \ |
| 27 | + && cd /opt/kube-bench && go build -o /usr/local/bin/kube-bench |
| 28 | + |
| 29 | +# Install Checkov (Terraform and cloud infrastructure security scanner) |
| 30 | +RUN pip3 install --no-cache-dir checkov |
| 31 | + |
| 32 | +# Install KubeLinter (static analysis tool for Kubernetes YAML files) |
| 33 | +RUN go install github.com/stackrox/kube-linter/cmd/kube-linter@latest \ |
| 34 | + && mv /root/go/bin/kube-linter /usr/local/bin/ |
| 35 | + |
| 36 | +# Install Snyk CLI (vulnerability scanner for dependencies and infrastructure) |
| 37 | +RUN npm install -g snyk |
| 38 | + |
| 39 | +# Stage 2: Final image with only necessary runtime components |
| 40 | +FROM alpine:latest |
| 41 | + |
| 42 | +# Install runtime dependencies |
| 43 | +RUN apk add --no-cache \ |
| 44 | + python3 \ |
| 45 | + nodejs \ |
| 46 | + npm \ |
| 47 | + openssl \ |
| 48 | + # Create a non-root user for security :cite[6]:cite[9] |
| 49 | + && addgroup -S securitytools && adduser -S devsecops -G securitytools -u 1000 \ |
| 50 | + # Clean up cache |
| 51 | + && rm -rf /var/cache/apk/* |
| 52 | + |
| 53 | +# Copy installed tools from the builder stage |
| 54 | +COPY --from=builder /usr/local/bin/trivy /usr/local/bin/ |
| 55 | +COPY --from=builder /usr/local/bin/kube-bench /usr/local/bin/ |
| 56 | +COPY --from=builder /usr/local/bin/kube-linter /usr/local/bin/ |
| 57 | +COPY --from=builder /usr/local/bin/checkov /usr/local/bin/ |
| 58 | +COPY --from=builder /usr/bin/snyk /usr/local/bin/ |
| 59 | + |
| 60 | +# Set the working directory to a writable path for the non-root user |
| 61 | +WORKDIR /home/devsecops |
| 62 | + |
| 63 | +# Switch to non-root user :cite[6]:cite[9] |
| 64 | +USER devsecops |
| 65 | + |
| 66 | +# Set environment variables |
| 67 | +ENV PATH="/usr/local/bin:${PATH}" |
| 68 | + |
| 69 | +# Define default command |
| 70 | +CMD ["/bin/sh"] |
0 commit comments