Docker container images often contain security vulnerabilities inherited from their base operating system packages. Rather than rebuilding images from scratch, you can use Trivy to scan for vulnerabilities and Copa to patch them directly. This tutorial demonstrates how to identify and fix container vulnerabilities on Ubuntu, Debian, RHEL, CentOS, and Fedora systems using these two powerful open-source tools.
Docker installed and running, internet connection to download packages and container images
Conventions
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user
Scan Docker images for vulnerabilities with Trivy and patch them automatically using Copa.
Quick Steps to Patch Docker Container Vulnerabilities
Step
Command/Action
1. Scan image
sudo trivy image --vuln-type os --ignore-unfixed IMAGE_NAME
2. Export JSON report
trivy image --vuln-type os --ignore-unfixed -f json -o report.json IMAGE_NAME
sudo copa patch -i IMAGE_NAME -r report.json -t patched -a tcp://0.0.0.0:8888
Step 1: Install Trivy Vulnerability Scanner
Trivy is an open-source vulnerability scanner developed by Aqua Security. It detects vulnerabilities in OS packages, application dependencies, and container images.
Ubuntu/Debian
Install dependencies: First install wget and gnupg packages required for adding the repository:
$ sudo apt-get install wget gnupg -y
Add Trivy GPG key: Download and add the Trivy repository signing key:
Copa (Copacetic) is a CNCF sandbox project that patches container images directly without requiring a full rebuild. It uses vulnerability reports from scanners like Trivy to apply security updates. Copa is distributed as a single binary that works on all Linux distributions.
Before patching, you need to identify vulnerabilities in your container image. The following steps use linuxconfig/instantprivacy as an example – replace it with your own image name.
Pull the container image: Download the image you want to scan:
$ sudo docker pull linuxconfig/instantprivacy
Scan for vulnerabilities: Run Trivy to scan for OS-level vulnerabilities. The --ignore-unfixed flag excludes vulnerabilities without available fixes:
$ sudo trivy image --vuln-type os --ignore-unfixed linuxconfig/instantprivacy
Export JSON report: Copa requires the scan results in JSON format:
Patch the container image: Use Copa to apply security patches. The -t flag specifies the tag for the patched image:
$ sudo copa patch \
-i docker.io/linuxconfig/instantprivacy \
-r instantprivacy.json \
-t patched \
-a tcp://0.0.0.0:8888
Copa downloads security updates and creates a new image layer with the patches applied. The patched image will be tagged as linuxconfig/instantprivacy:patched.
You have successfully scanned a Docker container image for security vulnerabilities using Trivy and patched them automatically with Copa. This approach is faster than rebuilding images from scratch and adds only a single patch layer, preserving the original image structure. For production environments, integrate these tools into your CI/CD pipeline to automatically scan and patch container images before deployment. For more information, refer to the official Trivy documentation and the Copa project website.
Frequently Asked Questions
What types of vulnerabilities can Copa patch? Copa only patches OS-level package vulnerabilities (such as Debian, Ubuntu, Alpine, or RHEL packages). It cannot patch application-level dependencies like npm packages, Python pip modules, or Go modules. For those, you need to rebuild the application with updated dependencies.
Why do I need BuildKit to run Copa? Copa uses BuildKit’s diff and merge capabilities to create a new image layer containing only the patched files. BuildKit runs as a privileged container that handles the low-level image manipulation. Without BuildKit, Copa cannot apply patches to the container filesystem.
Can I patch images from private registries? Yes, Copa supports private registries. You need to authenticate with docker login before running the patch command. Copa uses your Docker credentials to pull the base image and push the patched result if needed.
What happens if Copa cannot patch all vulnerabilities? Some vulnerabilities may not have fixes available yet. The --ignore-unfixed flag in Trivy excludes these from the report. Copa will patch everything that has an available fix. Run Trivy again without --ignore-unfixed to see all vulnerabilities including those awaiting patches.
How do I update Copa to the latest version? Download the latest release from the Copa GitHub releases page and replace the binary in /usr/local/bin/. Check the current version with copa --version.