Container Scanning

Introduced in GitLab 10.4.

Your application’s Docker image may itself be based on Docker images that contain known vulnerabilities. By including an extra Container Scanning job in your pipeline that scans for those vulnerabilities and displays them in a merge request, you can use GitLab to audit your Docker-based apps.

Container Scanning is often considered part of Software Composition Analysis (SCA). SCA can contain aspects of inspecting the items your code uses. These items typically include application and system dependencies that are almost always imported from external sources, rather than sourced from items you wrote yourself.

GitLab offers both Container Scanning and Dependency Scanning to ensure coverage for all of these dependency types. To cover as much of your risk area as possible, we encourage you to use all of our security scanners.

Overview

GitLab integrates with open-source tools for vulnerability static analysis in containers:

To integrate GitLab with security scanners other than those listed here, see Security scanner integration.

You can enable container scanning by doing one of the following:

GitLab compares the found vulnerabilities between the source and target branches, and shows the information directly in the merge request.

Container Scanning Widget

Capabilities

Capability In Free In Ultimate
Configure Scanners Yes Yes
Customize Settings (Variables, Overriding, offline environment support, etc) Yes Yes
View JSON Report as a CI job artifact Yes Yes
Generation of a JSON report of dependencies as a CI job artifact Yes Yes
Ability to enable container scanning via an MR in the GitLab UI Yes Yes
UBI Image Support Yes Yes
Support for Trivy Yes Yes
Support for Grype Yes Yes
Inclusion of GitLab Advisory Database Limited to the time-delayed content from GitLab advisories-communities project Yes - all the latest content from Gemnasium DB
Presentation of Report data in Merge Request and Security tab of the CI pipeline job No Yes
Interaction with Vulnerabilities such as merge request approvals No Yes
Solutions for vulnerabilities (auto-remediation) No Yes
Support for the vulnerability allow list No Yes
Access to Security Dashboard page No Yes
Access to Dependency List page No Yes

Requirements

To enable container scanning in your pipeline, you need the following:

  • Container Scanning runs in the test stage, which is available by default. If you redefine the stages in the .gitlab-ci.yml file, the test stage is required.
  • GitLab Runner with the docker or kubernetes executor on Linux/amd64.
  • Docker 18.09.03 or higher installed on the same computer as the runner. If you’re using the shared runners on GitLab.com, then this is already the case.
  • An image matching the supported distributions.
  • Build and push the Docker image to your project’s container registry.
  • If you’re using a third-party container registry, you might need to provide authentication credentials through the DOCKER_USER and DOCKER_PASSWORD configuration variables. For more details on how to use these variables, see authenticate to a remote registry.
  • GitLab CI/CD pipeline must include the test stage, which is available unless overridden with the stages keyword.

Configuration

To enable container scanning, add the Container-Scanning.gitlab-ci.yml template to your .gitlab-ci.yml file:

include:
  - template: Security/Container-Scanning.gitlab-ci.yml

The included template:

  • Creates a container_scanning job in your CI/CD pipeline.
  • Pulls the built Docker image from your project’s container registry (see requirements) and scans it for possible vulnerabilities.

GitLab saves the results as a Container Scanning report artifact that you can download and analyze later. When downloading, you always receive the most-recent artifact. If dependency scan is enabled, a Dependency Scanning report artifact is also created.

The following is a sample .gitlab-ci.yml that builds your Docker image, pushes it to the container registry, and scans the image:

include:
  - template: Jobs/Build.gitlab-ci.yml
  - template: Security/Container-Scanning.gitlab-ci.yml

container_scanning:
  variables:
    CS_DEFAULT_BRANCH_IMAGE: $CI_REGISTRY_IMAGE/$CI_DEFAULT_BRANCH:$CI_COMMIT_SHA

Setting CS_DEFAULT_BRANCH_IMAGE avoids duplicate vulnerability findings when an image name differs across branches. The value of CS_DEFAULT_BRANCH_IMAGE indicates the name of the scanned image as it appears on the default branch. For more details on how this deduplication is achieved, see Setting the default branch image.

Customizing the container scanning settings

There may be cases where you want to customize how GitLab scans your containers. For example, you may want to enable more verbose output, access a Docker registry that requires authentication, and more. To change such settings, use the variables parameter in your .gitlab-ci.yml to set CI/CD variables. The variables you set in your .gitlab-ci.yml overwrite those in Container-Scanning.gitlab-ci.yml.

This example includes the container scanning template and enables verbose output for the analyzer:

include:
  - template: Security/Container-Scanning.gitlab-ci.yml

variables:
    SECURE_LOG_LEVEL: 'debug'

Scan an image in a remote registry

To scan images located in a registry other than the project’s, use the following .gitlab-ci.yml:

include:
  - template: Security/Container-Scanning.gitlab-ci.yml

container_scanning:
  variables:
    DOCKER_IMAGE: example.com/user/image:tag
Authenticate to a remote registry

Scanning an image in a private registry requires authentication. Provide the username in the DOCKER_USER variable, and the password in the DOCKER_PASSWORD configuration variable.

For example, to scan an image from AWS Elastic Container Registry:

container_scanning:
  before_script:
    - ruby -r open-uri -e "IO.copy_stream(URI.open('https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip'), 'awscliv2.zip')"
    - unzip awscliv2.zip
    - ./aws/install
    - aws --version
    - export AWS_ECR_PASSWORD=$(aws ecr get-login-password --region region)

include:
  - template: Security/Container-Scanning.gitlab-ci.yml
    DOCKER_IMAGE: <aws_account_id>.dkr.ecr.<region>.amazonaws.com/<image>:<tag>
    DOCKER_USER: AWS
    DOCKER_PASSWORD: "$AWS_ECR_PASSWORD"

Dependency list

Introduced in GitLab 14.6.

The CS_DISABLE_DEPENDENCY_LIST CI/CD variable controls whether the scan creates a Dependency List report. This variable is currently only supported when the trivy analyzer is used. The variable’s default setting of "false" causes the scan to create the report. To disable the report, set the variable to "true":

For example:

include:
  - template: Security/Container-Scanning.gitlab-ci.yml

container_scanning:
  variables:
    CS_DISABLE_DEPENDENCY_LIST: "true"

Report language-specific findings

Introduced in GitLab 14.6.

The CS_DISABLE_LANGUAGE_VULNERABILITY_SCAN CI/CD variable controls whether the scan reports findings related to programming languages. The languages supported depend on the scanner used:

By default, the report only includes packages managed by the Operating System (OS) package manager (for example, yum, apt, apk, tdnf). To report security findings in non-OS packages, set CS_DISABLE_LANGUAGE_VULNERABILITY_SCAN to "false":

include:
  - template: Security/Container-Scanning.gitlab-ci.yml

container_scanning:
  variables:
    CS_DISABLE_LANGUAGE_VULNERABILITY_SCAN: "false"

When you enable this feature, you may see duplicate findings in the Vulnerability Report if Dependency Scanning is enabled for your project. This happens because GitLab can’t automatically deduplicate findings across different types of scanning tools. Please reference this comparison between GitLab Dependency Scanning and Container Scanning for more details on which types of dependencies are likely to be duplicated.

Available CI/CD variables

You can configure analyzers by using the following CI/CD variables.

caution
All customization of GitLab security scanning tools should be tested in a merge request before merging these changes to the default branch. Failure to do so can give unexpected results, including a large number of false positives.
CI/CD Variable Default Description Scanner
ADDITIONAL_CA_CERT_BUNDLE "" Bundle of CA certs that you want to trust. See Using a custom SSL CA certificate authority for more details. All
CI_APPLICATION_REPOSITORY $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG Docker repository URL for the image to be scanned. All
CI_APPLICATION_TAG $CI_COMMIT_SHA Docker repository tag for the image to be scanned. All
CS_ANALYZER_IMAGE registry.gitlab.com/security-products/container-scanning:5 Docker image of the analyzer. All
CS_DEFAULT_BRANCH_IMAGE "" The name of the DOCKER_IMAGE on the default branch. See Setting the default branch image for more details.