Time: Approximately 15 minutes
As part of Snyk's partnership with Docker, scanning container images for vulnerabilities is built into Docker Desktop and as simple as
docker scan. This lab shows how it works.
You will complete the following steps:
Step 1 - Clone a the sample application's GitHub Repo
Step 2 - Build some Docker images
Step 3 - Scan the images for vulnerabilities
Step 4 - Review scan results
Step 5 - Dig into provided Base Image recommendations
Step 6 - Apply a more secure Base Image and re-build the Image
Step 7 - Re-scan for Vulnerabilities
[Optional] Fork the Docker Goof Repo to your GitHub Account.
Check your installation by running
docker scan --version, it should print the current version of docker scan and the Snyk engine version.
docker scan --version
git clone https://github.com/snyk/docker-goof && cd docker-goof# If you forked the repo, clone your fork.
The Docker Goof repo has many Dockerfiles. You can build some, or all, of them out.
Use the included easy button
./build.sh to build them all at once.
# Easy button? Yes please. Build all images at once with:./build.sh
If you'd rather build the images one-by-one, remember to pass
-f pointing at the Dockerfile.
# Build your images with docker build.docker build -t docker-goof-slim -f slim.Dockerfile .docker build -t docker-goof -f Dockerfile .
The images are now in our local Docker cache. Run
docker images to list them out.
We'll use these images in the next step.
docker scan to scan for vulnerabilities. It's a best practice to pass the
Dockerfile used to build the image with
--file to get more robust results that include vulnerabilities from Dockerfile instruction and base image upgrade guidance. For example,
To scan docker-goof, and pass the Dockerfile:
# Scanning the docker-goof image and passing the Dockerfiledocker scan docker-goof --file=Dockerfile
To scan docker-goof-app, and pass the Dockerfile:
# Scanning the docker-goof-app image and passing the Dockerfiledocker scan docker-goof-app --file=app.Dockerfile
To scan docker-goof-n6-slim, without passing the Dockerfile:
# Scanning an image without passing the Dockerfiledocker scan docker-goof-n6-slim
Scanning images for Open Source vulnerabilities with Snyk is that easy! When finished, scan results are displayed in the Terminal, along with remediation guidance.
Vulnerabilities are broken up into sections, based on how they were introduced:
Vulnerabilities introduced by the container's base image can be identified by the presence of the
Introduced by your base image line. (Line 9 below)
✗ High severity vulnerability found in curl/libcurl3Description: Buffer OverflowInfo: https://snyk.io/vuln/SNYK-DEBIAN8-CURL-466507Introduced through: firstname.lastname@example.org+deb8u11, email@example.com+deb8u11, git@1:2.1.4-2.1+deb8u6From: firstname.lastname@example.org+deb8u11 > email@example.com+deb8u11From: firstname.lastname@example.org+deb8u11 > email@example.com+deb8u11From: firstname.lastname@example.org+deb8u11and 2 more...Introduced by your base image (node:10.4.0)Fixed in: 7.38.0-4+deb8u16
Some vulnerabilities are introduced by User Instruction in the Dockerfile. Snyk highlights the command that introduced the vulnerability, with the
Introduced in your Dockerfile by line. (Line 9)
✗ High severity vulnerability found in bzip2/bzip2Description: Out-of-bounds WriteInfo: https://snyk.io/vuln/SNYK-DEBIAN8-BZIP2-450781Introduced through: email@example.com+b3, firstname.lastname@example.org, email@example.com+b3, imagemagick/libmagickcore-dev@8:126.96.36.199-5+deb8u12, meta-common-packages@metaFrom: firstname.lastname@example.org+b3From: email@example.com > firstname.lastname@example.org+b3From: email@example.com+b3and 2 more...Introduced in your Dockerfile by 'RUN apt-get install -y imagemagick'Fixed in: 1.0.6-7+deb8u1
The last kind of vulnerability your images might contain are introduced by your application dependencies. Snyk highlights the package manifest
Target File that introduced it. (Line 14)
Issues to fix by upgrading:Upgrade @firstname.lastname@example.org to @email@example.com to fix✗ Remote Code Execution (RCE) [Medium Severity][https://snyk.io/vuln/SNYK-JS-BUNYAN-573166] in firstname.lastname@example.org by email@example.com > firstname.lastname@example.org and 8 other path(s)Upgrade @email@example.com to @firstname.lastname@example.org to fix✗ Remote Code Execution (RCE) [Medium Severity][https://snyk.io/vuln/SNYK-JS-BUNYAN-573166] in email@example.com by firstname.lastname@example.org > email@example.com and 8 other path(s)Organization: demo-incPackage manager: yarnTarget file: /var/lib/ghost/versions/2.37.2/package.jsonProject name: ghostDocker image: docker-goof-app
Snyk's remediation guidance helps developers spend less time remediating, and more time developing! One way to tackle vulnerabilities is by choosing a more secure base image. By providing the Dockerfile to
docker scan , Snyk can suggest other Base Images that can be used in the Dockerfile's
FROM statement to bring down those vulnerability counts.
These are grouped by how likely they are to be compatible with your application:
Minor upgrades are the most likely to be compatible with little work,
Major upgrades can introduce breaking changes depending on image usage,
Alternative architecture images are shown for more technical users to investigate.
Organization: demo-incPackage manager: debTarget file: DockerfileProject name: docker-image|docker-goofDocker image: docker-goofBase image: node:10.4.0Licenses: enabledTested 382 dependencies for known issues, found 459 issues.Base Image Vulnerabilities Severitynode:10.4.0 951 451 high, 480 medium, 20 lowRecommendations for base image upgrade:Minor upgradesBase Image Vulnerabilities Severitynode:10.22 498 53 high, 48 medium, 397 lowMajor upgradesBase Image Vulnerabilities Severitynode:14.13 497 53 high, 47 medium, 397 lowAlternative image typesBase Image Vulnerabilities Severitynode:14.13-buster-slim 51 9 high, 4 medium, 38 lownode:14.12.0-slim 70 17 high, 7 medium, 46 lownode:14.11.0-stretch-slim 70 17 high, 7 medium, 46 lownode:14.13.1-buster 254 31 high, 30 medium, 193 low
Let's choose a more secure base image for docker-goof. We'll do this by applying the
Minor upgrade recommended by Snyk. Change the FROM statement in the Dockerfile:
# Comment out the old FROM Statement# FROM node:10.4.0# Write in the new oneFROM node:10.22RUN apt-get install -y imagemagick
Now build the new Image. To compare results side-by-side with the previous scan, we'll specify a different tag when building the image.
docker build -t docker-goof:v2 -f Dockerfile .
Now let's use
docker scan to scan for vulnerabilities. Once again, pass the
Dockerfile used to build the image with
--file to get more robust results.
# Scanning the docker-goof image and passing the Dockerfiledocker scan docker-goof:v2 --file=Dockerfile
Continue this cycle of build-scan-push until you're running the most secure base image.
We hope you enjoyed this Lab! In this pattern, we checked for vulnerabilities in Images using the Docker CLI, and saw vulnerabilities introduced by our Base Image, Dockerfile instructions, and application dependencies.
Applying a more secure base image is a great first step toward making your images more secure. As noted above, vulnerabilities can come from your application dependencies and Dockerfile user instructions as well. Check out other courses in the Snyk Academy to learn how Snyk can help you fix and reduce the other vulnerabilities in your images.
As we continue to evolve our Partnership with Docker, we'll keep adding new capabilities that help developers build their container images securely and deploy with confidence. Try out this workflow on your own applications, and let us know what you think!