GitLab Documentation

Container Scanning with GitLab CI/CD

Container Scanning with GitLab CI/CD

You can check your Docker images (or more precisely the containers) for known vulnerabilities by using Clair and clair-scanner, two open source tools for Vulnerability Static Analysis for containers.

All you need is a GitLab Runner with the Docker executor (the shared Runners on will work fine). You can then add a new job to .gitlab-ci.yml, called container_scanning:

  image: docker:stable
    DOCKER_DRIVER: overlay2
    ## Define two new variables based on GitLab's CI/CD predefined variables
  allow_failure: true
    - docker:stable-dind
    - docker run -d --name db arminc/clair-db:latest
    - docker run -p 6060:6060 --link db:postgres -d --name clair arminc/clair-local-scan:v2.0.1
    - apk add -U wget ca-certificates
    - wget
    - mv clair-scanner_linux_amd64 clair-scanner
    - chmod +x clair-scanner
    - touch clair-whitelist.yml
    - while( ! wget -q -O /dev/null http://docker:6060/v1/namespaces ) ; do sleep 1 ; done
    - retries=0
    - echo "Waiting for clair daemon to start"
    - while( ! wget -T 10 -q -O /dev/null http://docker:6060/v1/namespaces ) ; do sleep 1 ; echo -n "." ; if [ $retries -eq 10 ] ; then echo " Timeout, aborting." ; exit 1 ; fi ; retries=$(($retries+1)) ; done
    - ./clair-scanner -c http://docker:6060 --ip $(hostname -i) -r gl-sast-container-report.json -l clair.log -w clair-whitelist.yml ${CI_APPLICATION_REPOSITORY}:${CI_APPLICATION_TAG} || true
    paths: [gl-sast-container-report.json]

The above example will create a container_scanning job in your CI/CD pipeline, pull the image from the Container Registry (whose name is defined from the two CI_APPLICATION_ variables) and scan it for possible vulnerabilities. The report will be saved as an artifact that you can later download and analyze.

If you want to whitelist some specific vulnerabilities, you can do so by defining them in a YAML file, in our case its named clair-whitelist.yml.

Tip: Starting with GitLab Ultimate 10.4, this information will be automatically extracted and shown right in the merge request widget. To do so, the CI/CD job must be named container_scanning and the artifact path must be gl-sast-container-report.json. Learn more on container scanning results shown in merge requests.

Was this helpful? Do you think that something is unclear? Use the comments area below and leave your feedback. For support and other enquiries, see getting help.