AirborneOps Learning Labs · Container security
GitLab CI and Registry Security
Connect a commit to the image that is built, scanned, reviewed, and released. This chapter focuses on practical GitLab CI protections, container-scanning results, and registry permissions for IT professionals and developers.
Ebook draft: These are recommended controls and teaching examples, not security guarantees. Adapt them to your runner, GitLab version and tier, project configuration, threat model, and release policy.
01 · Pipeline controls
Build once, scan that image, then release
Give the pipeline an explicit sequence: build an image tagged with the full CI_COMMIT_SHA, scan that exact commit-specific reference, and let release depend on both jobs. Do not point the scanner at a moving latest tag or rebuild in the release job; either can make the reviewed image differ from the one shipped.
# Teaching sample only. Replace every <...> value and adapt the runner settings.
include:
- template: Jobs/Container-Scanning.gitlab-ci.yml
stages: [build, test, release]
variables:
IMAGE_REF: "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
DOCKER_HOST: "tcp://docker:2376"
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_TLS_VERIFY: "1"
DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
build-image:
stage: build
image: docker:<version>-cli@sha256:<approved-cli-image-digest>
services:
- name: docker:<version>-dind@sha256:<approved-dind-image-digest>
alias: docker
script:
- echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" --username "$CI_REGISTRY_USER" --password-stdin
- docker buildx create --use
- docker buildx build --pull --tag "$IMAGE_REF" --push --metadata-file build-metadata.json .
- IMAGE_DIGEST="$(sed -n 's/.*"containerimage\.digest"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' build-metadata.json)"
- test -n "$IMAGE_DIGEST"
- printf 'IMAGE_DIGEST=%s\n' "$IMAGE_DIGEST" > image.env
artifacts:
reports:
dotenv: image.env
paths:
- build-metadata.json
container_scanning:
stage: test
allow_failure: false
variables:
CS_IMAGE: "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
release-image:
stage: release
needs:
- job: build-image
artifacts: true
- job: container_scanning
artifacts: false
rules:
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_REF_PROTECTED == "true"'
- when: never
image: docker:<version>-cli@sha256:<approved-cli-image-digest>
script:
- echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" --username "$CI_REGISTRY_USER" --password-stdin
- docker buildx imagetools create --tag "$CI_REGISTRY_IMAGE:release-$CI_COMMIT_SHA" "$CI_REGISTRY_IMAGE@$IMAGE_DIGEST"- Replace the CLI and Docker-in-Docker version and digest placeholders with approved images. Configure the runner for TLS-enabled Docker-in-Docker and isolate its privileges; the YAML alone does not secure a runner. Validate the final file with GitLab CI Lint before enabling it.
- The build job pushes one commit-tagged image and exports the digest from Buildx metadata as a dotenv artifact. The scanner's
CS_IMAGEuses that same commit-specific tag. Keep the recorded digest with the build evidence and promote that digest to the release tag rather than rebuilding. - The stage order makes the scan run after the build. The release job uses
needsto wait for both the build artifact containing the digest and a successful scan job. It runs only for the protected default branch in this sample; configure that branch as protected and adapt the rule if your release process uses a different protected ref. - This sample treats a failed scan job as a pipeline failure. It does not define which vulnerability findings block a release. Set an explicit project policy for severity thresholds, who may approve exceptions, their rationale and expiry, and how that policy is enforced. A report filter such as
CS_SEVERITY_THRESHOLDis not, by itself, a release approval policy.
02 · Scan review
Read the result before accepting the image
GitLab Container Scanning analyzes the image referenced by CS_IMAGE and produces a JSON report as a CI job artifact. The report represents the scanner's findings for that image at that time; it is evidence for a review, not a complete description of every possible weakness in the image or its deployment.
- Confirm the report and job correspond to the expected commit and that the reported image reference matches the image built in that pipeline. Record its digest where the build provides one.
- Open
gl-container-scanning-report.jsonin the job artifacts. For each relevant finding, inspect its identifier, severity, affected package and installed version, available fix, and whether the package is part of the image being released. Use GitLab's Security view when it is available for your tier and configuration, and keep the job report as the underlying scan record. - Prioritize an upgrade, package replacement, or base-image update when a fix is available. If the project accepts a finding, document why, any compensating control, the accountable approver, an expiry or review date, and the follow-up action.
- Retain the report with the source commit, image digest, scan job, and release approval for the retention period your organization requires. Rebuild and rescan after remediation; a new image needs its own review evidence.
03 · Registry controls
Limit who can publish release images
A project registry is not automatically protected just because a release job exists. GitLab documents that, by default, Developer, Maintainer, and Owner roles can push and delete images in container repositories. In project settings, create a protected container repository rule for the production or release repository path, then set Minimum access level for push to the lowest role appropriate for the workflow. This setting governs who can create or update images in the protected repository.
- Match the rule to the actual repository path used for production images. Check the rule after renaming or moving a project, and review who can create or change the rule itself.
- Restrict release credentials to the jobs and protected refs that need them. Prefer GitLab's job-scoped registry credentials where they fit; store any other credential as a protected CI/CD variable or use an approved secret manager, with only the required scope and lifetime.
- Do not hard-code credentials, put them in source files or image layers, pass them through Docker build arguments, or print them in job logs. Masking can reduce accidental display; it does not prevent code in a job from reading a variable. Avoid debug tracing on secret-bearing jobs and rotate credentials after exposure.
- Confirm that the identity used by CI can perform only the needed pushes. GitLab protected container repositories do not accept deploy-token pushes, even when the token has registry scopes, so test the chosen identity against the configured rule.
04 · Policy and limits
Make the control enforceable and reviewable
A sensible pipeline order reduces opportunities for an unreviewed image to reach a release path, but order only helps when required jobs are present and their status is enforced. Likewise, repository protection only limits pushes after a project administrator creates a rule that matches the target path and selects an appropriate push level. Review the effective pipeline configuration, protected refs, repository rules, CI variable settings, and runner configuration as part of rollout.
Set a written vulnerability policy that defines which findings block promotion, whether the rule uses severity, exploitability, fix availability, or service context, and who can approve exceptions. Test that policy with representative findings. The GitLab scanning job's success means the job completed according to its configuration; it does not mean every reported vulnerability is below an accepted threshold.
NIST SP 800-190 and the CIS Docker Benchmark offer useful container-security guidance to compare with local controls. They are references for risk review, not a certification, an attestation that a project follows every recommendation, or proof that a particular image or pipeline is secure. Apply only recommendations relevant to your platform and document the decisions.
Further reading