AdvancedLesson 17 of 26
Image security
Smaller, non-root, scanned images are safer images.
- Use minimal bases (
-slim, Alpine, distroless) and multi-stage builds. - Run as a non-root user.
- Pin versions or digests, and rebuild regularly to pick up fixes.
- Scan images for known CVEs (for example with Trivy or Docker Scout).
- Drop capabilities, use a read-only filesystem where you can.
dockerfile
RUN useradd --create-home app
USER appbash
docker run --read-only --cap-drop ALL --security-opt no-new-privileges myapp
trivy image myapp:1.0