Skip to main content
In this guide, we use Trivy to scan Docker base images for vulnerabilities and select the most secure option for a Spring Boot application. We’ll compare five candidates, update the Dockerfile, adjust our Jenkins Pipeline, and verify the build.

Prerequisites

  • Docker installed locally
  • Trivy image cache volume ($WORKSPACE mapped to /root/.cache/)
  • A Spring Boot JAR artifact in target/*.jar

1. Scan the Current Base Image

First, scan myorg/numeric-app:latest:
This reveals 4 critical and 32 high vulnerabilities—unsuitable for production.

2. Compare Alternative Base Images

We’ll evaluate these images:
  1. openjdk (latest)
  2. openjdk:8
  3. openjdk:8-alpine
  4. adoptopenjdk/openjdk8:alpine-slim
Use this scan command template:

2.1 Summary of Scan Results

Using the latest tag can introduce unexpected changes. Always pin to a specific version for production.

3. Detailed Scan Examples

3.1 openjdk (latest)

Result:

3.2 openjdk:8

Result:

3.3 openjdk:8-alpine

Result:

3.4 adoptopenjdk/openjdk8:alpine-slim

Result:
We choose adoptopenjdk/openjdk8:alpine-slim for its zero vulnerabilities and fixed version on Alpine Slim.

4. Update the Dockerfile

Switch the base image:
Commit and push these changes. The next Jenkins run will pick up the new base image.

5. Jenkins Pipeline Configuration

In your Jenkinsfile, ensure you have Trivy and Docker build stages:
Using sudo resolves permission issues on the Trivy cache directory. Alternatively, add the Trivy cache folder to .dockerignore.

6. Build & Scan Logs

Trivy in the pipeline reports:

Next Steps

In subsequent lessons, we’ll integrate OPA Conftest to enforce Dockerfile best practices and compliance policies.

Watch Video

Practice Lab