Now let's take a look at container image vulnerability scanning in practice. In this tutorial, you'll be using Trivy, an open source container security tool that demonstrates the ability to scan both a container configuration file (Dockerfile) and a container image. In a project scenario, Trivy could be provided to developers on their workstations to allow scanning and feedback during coding, as well as being integrated into an automated CI/CD pipeline for formal build validation. Other open source and commercial container image scanning tools are also available.
This tutorial shows you how to create an image and scan it to highlight any vulnerabilities and configuration flaws. Using this information, you can reduce and remove vulnerabilities and flaws where possible.
Prerequisites
To complete this tutorial, you need to meet the following requirements:
Must be running Linux or Mac
Need Podman, Docker, or another container builder installed
Need "sed" and "awk" installed
Caveat: Podman/Docker on Mac utilizes a virtual machine running Linux. This is taken care of by the Podman machine init command, or through the use of Docker Desktop, as described in the installation instructions which can be accessed via the links above.
Estimated time
It should take you approximately 20 minutes to complete this tutorial.
Note the number of vulnerabilities now: ~2 in the image, with ~2 fixable, consisting of (approximately) 2 high.
Now let's try fixing the vulnerable package. (Note: The number of vulnerabilities and the affected packages might change after this lab is published, so please add any additional packages to be fixed to the Dockerfile config below, specifying the fixed version):
cat << 'EOF' > Dockerfile
FROM alpine:3.14.4
RUN apk add bash "libretls=3.3.3p1-r3""zlib=1.2.12-r0" --no-cache
ADD ./myfile.tar /tmp
EXPOSE 22
EOF
You should now see that there are no vulnerabilities (assuming no unfixable vulnerabilities have been identified since this lab was published). Also note that newer images are being created all the time, and it is likely that when alpine:3.14.5 is released it will resolve these vulnerabilities so you won't need to install the packages yourself.
Let's now check if there are any configuration concerns, by scanning the Dockerfile:
trivy config ./Dockerfile
Show more
As it turns out, there are two configuration flaws to fix:
You should run the container with a non-root user.
You shouldn't expose port 22 unless needed (let's assume it's not).
Amend the Dockerfile (note that “ARG” arguments are included to parameterize some values that are used in multiple places):
Note: To adhere to the best practice of “shift-left” (AKA “fail fast”), you should further optimize this process by scanning candidate base images first, before utilizing them in a Dockerfile and creating your own image to scan.
It's also a good idea to look at the images you created to see how selecting a minimal base image can impact image size:
Podman:
podman images "mytestimage"
Show more
Docker:
docker images "mytestimage"
Show more
Result:
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/mytestimage 0.5 df9b52a58052 7 minutes ago 8.29 MB
localhost/mytestimage 0.41aa1bf71b769 10 minutes ago 8.11 MB
localhost/mytestimage 0.3 eb1b2a234b87 11 minutes ago 83.9 MB
localhost/mytestimage 0.27bf756f9e33f 13 minutes ago 72.5 MB
localhost/mytestimage 0.1940908ddbf06 16 minutes ago 119 MB
Show more
Notice that you have reduced it from 119MB to 8.29MB, which means less to transfer around a network!
Tidy up
When you have completed all the steps above, you can tidy up as follows:
Remove the lab directory and contents that you created:
cd ../ && rm -rf vuln-lab
Show more
Remove the images you created from Podman/Docker:
Podman:
podman image rm $(podman images "mytestimage" -q)
Show more
Docker:
docker image rm $(docker images "mytestimage" -q)
Show more
Conclusion
The steps in this tutorial have showed you how to ensure that you have followed an educated process for selecting the base image that you use to create a container image. This involved ensuring the use of up-to-date images to reduce vulnerability count, as well as using slim/minimal images to reduce and remove unecessary components in the base image which further improves security posture and reduces vulnerabilities. In addition, you checked the configuration for any security weaknesses to ensure that your resultant container image is aligned with best security practices. By putting these checks in place and taking remedial action, you can reduce the risks imposed on the system to which these are deployed.
About cookies on this siteOur websites require some cookies to function properly (required). In addition, other cookies may be used with your consent to analyze site usage, improve the user experience and for advertising.For more information, please review your cookie preferences options. By visiting our website, you agree to our processing of information as described in IBM’sprivacy statement. To provide a smooth navigation, your cookie preferences will be shared across the IBM web domains listed here.