# Add a Trivy Image Scan to a Docker Build Workflow

Yes. An agent adds a GitHub Actions workflow that builds each Dockerfile, scans the image with Trivy, uploads the findings and fails on fixable critical ones.

[Run this on Rush](https://cloud.getrush.ai/run/add-trivy-scan-to-ci/docker/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the repository into its own cloud sandbox and reads every Dockerfile and every workflow before adding anything. In a multi-service repository like dockersamples/example-voting-app there are three services with their own Dockerfiles: `vote/Dockerfile` starts from `python:3.11-slim` and installs `curl` for its health check, `result/Dockerfile` starts from `node:18-slim` and installs `curl` and `tini`, and `worker/Dockerfile` is a two-stage build from `mcr.microsoft.com/dotnet/sdk:7.0` into `mcr.microsoft.com/dotnet/runtime:7.0`. None of the three sets a `USER`, so every container runs as root. Two more Dockerfiles exist for tooling, `seed-data/Dockerfile` on `python:3.9-slim` and `result/tests/Dockerfile` on `node:8.9-slim`, and the agent notes them but leaves them out of the scan because nothing publishes them.

CI is three near-identical workflows, `call-docker-build-vote.yaml`, `call-docker-build-result.yaml` and `call-docker-build-worker.yaml`, each with `paths` filters on its service directory, each calling the reusable workflow `dockersamples/.github/.github/workflows/reusable-docker-build.yaml@main` with `platforms: linux/amd64,linux/arm64,linux/arm/v7` and the `ghcr.io` and Docker Hub image names. Because the build steps live in another repository, a scan cannot be slipped into them from here. The agent therefore adds one new workflow, `scan-images.yaml`, with a matrix over the three services, a `linux/amd64` build with `docker/build-push-action` set to `push: false` and `load: true` under a local tag, and `aquasecurity/trivy-action` on that tag with `format: sarif`, `severity: CRITICAL,HIGH`, `ignore-unfixed: true` and `exit-code: 1`, followed by `github/codeql-action/upload-sarif` so findings appear under the repository's Security tab. The job gets `contents: read` and `security-events: write` and nothing else.

The workflow runs on `pull_request` and on `push` to `main` without the per-service `paths` filters, because a base image picks up new CVEs while the Dockerfile sits unchanged, and it also runs on a weekly `schedule` for the same reason. A second job runs `trivy config` over the five Dockerfiles for misconfigurations, which is where the missing `USER` lines surface, and reports them without failing the build, since fixing that for the Node service means moving `node_modules` out of the way of a non-root user and the agent leaves such changes for their own pull request. An empty `.trivyignore` with a comment goes in the repository root so a reviewer has a documented place to accept a finding by CVE id.

The sandbox has no Docker, so the agent installs it first with `sudo apt-get install -y docker.io`, starts `dockerd`, builds the three images for the sandbox's own architecture and runs the same `trivy image` command the workflow will run, once as a table and once as SARIF. Every finding, its package, its installed and fixed versions and the layer it comes from goes into the pull request description, grouped by service, so the first review of the workflow is also the first review of what the images carry today. The agent does not change any `FROM` line: this pull request adds the scan, and the base-image bumps it suggests are listed as follow-up work with the exact tag that clears each finding.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a Trivy image scan to this repository's GitHub Actions. Read every Dockerfile and every workflow under .github/workflows first. Add one workflow that builds each published image for linux/amd64 without pushing, scans it with Trivy for CRITICAL and HIGH vulnerabilities with unfixed ones ignored, uploads SARIF to code scanning, and fails on a finding; run it on pull requests, pushes to the default branch and a weekly schedule. Add a non-blocking Trivy config scan of the Dockerfiles and an empty .trivyignore with a comment. Install Docker in the sandbox, build the images, run the scan yourself and put every finding with its package and fixed version in the pull request. Do not change any FROM line. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch with two new files, `.github/workflows/scan-images.yaml` and `.trivyignore`, and nothing else changed. The description carries the scan output from the sandbox per image: the base image and its OS packages, each CRITICAL or HIGH CVE with the package, the installed version and the version that fixes it, and the misconfiguration list from `trivy config`, which for these Dockerfiles is the missing `USER` instruction three times over.

The run's record keeps the Docker installation, the three builds and the Trivy invocations with their full output, so a reviewer can see the scan was run against the images this repository actually produces rather than copied from a template. The workflow itself runs on the pull request as soon as it is opened, so its first real result, red or green, is on the pull request before anyone merges it.

Before merging, a reviewer decides whether the first run should block: with `exit-code: 1` the pull request stays red until the listed packages are fixed or their CVEs are added to `.trivyignore` with a reason, and the agent's description names the base-image tag that clears each one. A reviewer also checks that the matrix covers every image the build workflows publish and no other.

## Run it on every new service directory

Start this run once from a terminal with `POST /o/{org}/sessions`, this body and the repository in `resources`; the run keeps going in its sandbox after the window closes, and a follow-up through `POST /o/{org}/sessions/{id}/messages` can tighten the severity list or add a service without starting over. Once merged, the workflow does the recurring work: every pull request, every push to `main` and every week gets a fresh scan of every image.

For an organization with many image repositories, a script can start one run per repository with the same body, each with its own `request_id`, a UUID, so a retried start returns the run it already began. The key's `webhook_url` receives `run.completed` for each run with a `session_url`, from which the handler reads the pull request link and posts it where the owning team will see it.

## Why run it on Rush?

Adding a scan by hand means guessing at the action's inputs and finding out on the third push whether SARIF upload needs `security-events: write`. The agent builds the real images and runs the real scan in a sandbox first, so the workflow it writes has already been exercised against this repository's Dockerfiles, and the pull request description is the baseline of what the images carry before the scan starts gating anything.

The sandbox is disposable: installing Docker, pulling three base images and building them leaves nothing on anyone's laptop, and the full command record stays with the run. Several repositories can be done at once in separate sandboxes, each ending in its own pull request with its own baseline.

## Frequently asked questions

### Why a separate workflow instead of adding a step to the build workflows?

Because the three build workflows only call a reusable workflow in another repository, `dockersamples/.github`, and have no steps of their own to add to. A separate workflow builds the image locally without pushing and scans that, so the published build stays untouched and the scan runs even when a service directory did not change.

### Will this fail my pull requests on day one?

It can. The pull request description lists every CRITICAL and HIGH finding with a fix available, so you can bump the base images first or accept specific CVEs in `.trivyignore` with a comment. If you prefer a warning period, change `exit-code` to `0` in the workflow; findings still reach the Security tab through the SARIF upload.

### Does it scan for Dockerfile mistakes too?

Yes, in a second non-blocking job that runs `trivy config` over the Dockerfiles. On these three it reports that no stage sets a `USER`. Fixing that is a code change per service, so the agent reports it and leaves it for a pull request of its own.

### Why does the workflow run on a schedule?

A CVE is published against a package already inside an image that has not changed. The existing build workflows only run when a service directory changes, so without the weekly run nobody would learn about it until the next unrelated commit.

### Does it change my images or base tags?

No. The prompt forbids changing any `FROM` line, so this pull request adds only the workflow and the ignore file. Base-image bumps are listed as follow-up work with the tag that clears each finding, and can be their own run.

## More Docker jobs

- [Fix "apt-key: not found" and "apt-key is deprecated" in a Dockerfile](https://getrush.ai/run/fix-apt-key-not-found/docker/)
- [Fix "The repository 'http://deb.debian.org/debian buster Release' does not have a Release file" in a Docker build](https://getrush.ai/run/fix-debian-buster-release-file-missing/docker/)
- [Fix a Broken Docker Build](https://getrush.ai/run/fix-docker-build/docker/)

## The same job on other stacks

- [Add Trivy Scanning to a Terraform Module's CI](https://getrush.ai/run/add-trivy-scan-to-ci/terraform/)

## More jobs like this: Hand off a long task

- [Add Twig Templates to a Slim 4 App](https://getrush.ai/run/add-twig-views/slim/)
- [Add Two-Factor Authentication to a Filament Admin Panel](https://getrush.ai/run/add-two-factor-auth/filament/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
