AirborneOps Learning Labs · Container security
Container Image Foundations
A practical starting point for IT professionals and developers who build and release container images through GitLab. Choose a traceable base, keep the runtime image small and purposeful, then scan the exact image produced by the commit under review.
Ebook draft: This guidance is a starting point for review, not a guarantee of security. Adapt it to your application, threat model, GitLab version, registry policy, and operational requirements.
01 · Image inputs
Select and identify base images
Start with the smallest image that still has the libraries, certificates, locale data, and runtime behavior your application actually needs. A tiny image is not safer by itself: prefer a maintained upstream with a published support policy and a clear update path over an abandoned or unfamiliar image that happens to be smaller.
- Choose an official project image, a vendor-maintained image, or an organization- approved mirror. Check its publisher, repository, release cadence, supported operating-system lifecycle, and update advisories.
- Use a human-readable version tag to show intent, then pin the selected manifest by digest:
image:version@sha256:<64-hex-digest>. Tags help people read the file; the digest selects the immutable content. - Record the upstream name and tag, resolved digest, platform or architecture, retrieval date, source or signature checks performed, approval owner, and the evidence used to verify provenance. Recheck those records when updating the base.
- Pin every image that supplies build tools too: Dockerfile build stages, the Dockerfile frontend when you specify one, and GitLab CI job and service images. A pinned runtime base does not make an unpinned compiler or CI image reproducible.
Keep a short record with the repository or release metadata so a reviewer can trace the selected digest back to a source and an approval.
# SAMPLE ONLY — fill this from your approved publisher and verification process.
source: <official-registry>/<publisher>/<image>
tag: <release-tag>
digest: sha256:<verified-64-hex-manifest-digest>
platform: <linux/amd64>
verification: <source record, signature or attestation check, and result>
approved_by: <owner>
reviewed_on: <YYYY-MM-DD>02 · Dockerfile boundary
Keep the runtime image lean
Build in a separate stage, then copy only the runtime output into a maintained runtime base. The sample uses a statically linked Go service and a non-root distroless runtime; adapt the language, entry point, and runtime base to the application. Every angle-bracket value below is a placeholder, not a verified image reference or provenance claim, so resolve and review real values before using the example.
# Teaching sample: replace every <...> value with an approved, verified digest.
# syntax=docker/dockerfile:1.7@sha256:<approved-frontend-digest>
FROM golang:<supported-version>-alpine@sha256:<approved-builder-digest> AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=secret,id=netrc,target=/root/.netrc \
go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -o /out/service ./cmd/service
FROM gcr.io/distroless/static-debian12:nonroot@sha256:<approved-runtime-digest>
COPY --from=build /out/service /service
USER nonroot:nonroot
ENTRYPOINT ["/service"]- The first stage contains the compiler and module cache. The final stage starts from a separate runtime image and copies only the compiled service, so build tools and package caches are not part of the shipped layers.
USER nonroot:nonrootmakes non-root execution explicit. Test the container with the same user and filesystem settings used in production, including any required writable paths and listening ports.- The BuildKit secret mount gives dependency download a credential without baking it into an image layer. Do not put secrets in build arguments, copy them into the context, or commit them in the repository.
- Keep the build context free of private configuration and local tool output. Tailor these ignore rules to the project; do not ignore source files the build actually needs.
.git
.env
.env.*
**/.netrc
**/id_*
**/*.pemAvoid installing shells, package managers, compilers, debugging utilities, or extra certificates in the runtime stage unless the service needs them. Run the application as non-root and grant only the runtime files and permissions it uses. A multi-stage build helps keep the final image clean; it does not remove a secret that was copied into an earlier layer or left in a file later deleted from that same layer.
03 · GitLab pipeline
Build, scan, and release in GitLab
Use ordered stages for the overall path, then make the dependency between the image and its scan explicit. Tag the image with CI_COMMIT_SHA in the build job and set the included GitLab Container Scanning job's CS_IMAGE to that same reference. The scanner then receives the image built by this pipeline for this source commit, rather than a moving latest tag or a separate rebuild.
stages: [build, test, release]
include:
- template: Jobs/Container-Scanning.gitlab-ci.yml
build-image:
stage: build
image: docker:<supported-version>-cli@sha256:<approved-build-image-digest>
services:
- name: docker:<supported-version>-dind@sha256:<approved-dind-image-digest>
script:
- echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" --username "$CI_REGISTRY_USER" --password-stdin
- export IMAGE_REF="$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
- docker build --pull --tag "$IMAGE_REF" .
- docker push "$IMAGE_REF"
container_scanning:
stage: test
needs: ["build-image"]
allow_failure: false
variables:
CS_IMAGE: "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
release-image:
stage: release
needs:
- job: build-image
artifacts: false
- job: container_scanning
artifacts: false
rules:
- if: '$CI_COMMIT_TAG && $CI_COMMIT_REF_PROTECTED == "true"'
- when: never
image: docker:<supported-version>-cli@sha256:<approved-build-image-digest>
services:
- name: docker:<supported-version>-dind@sha256:<approved-dind-image-digest>
script:
- echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" --username "$CI_REGISTRY_USER" --password-stdin
- export IMAGE_REF="$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
- docker pull "$IMAGE_REF"
- docker tag "$IMAGE_REF" "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"
- docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"- Replace both Docker image digest placeholders with the digests approved for your runner environment. GitLab's predefined registry variables keep the login scoped to the job; do not print or hard-code credentials.
needs: ["build-image"]makes the scanner wait for the build job.CS_IMAGEnames the image tag produced fromCI_COMMIT_SHA. Preserve the pushed digest in your build record. If registry policy allows a commit tag to be replaced, configure the scan and promotion to use that recorded digest directly; a commit tag alone does not prevent replacement.- The release job needs both the build and scan jobs, so it cannot start before the image exists and the scan job finishes. The tag rule limits this sample release job to protected tag pipelines; configure the matching protected registry repository and a review or approval policy for your project.
- A completed scan is not automatically a risk decision. Define which findings block release, who may approve a documented exception, and how long that exception lasts. Verify that your chosen GitLab policy actually enforces that threshold; job ordering by itself does not make every reported vulnerability fail the pipeline.
04 · Human review
Review findings and retain evidence
When the pipeline finishes, inspect the container_scanning job and its result for the commit. Review vulnerability details, affected package and image layer, severity, available fixed version, and whether the finding affects the shipped runtime image. Confirm the reported image reference matches the commit-built image and record its digest with the decision.
- Open the pipeline's Security view when it is available in your GitLab tier and configuration, then open the scan job to inspect its attached report artifact.
- Download and retain
gl-container-scanning-report.jsonfrom the job artifacts. Treat the JSON report as the record of the findings generated by that scan; do not report a clean result unless you inspected the actual output. - Triage each release-relevant finding: patch or replace the affected package where practical, or record the rationale, compensating control, owner, expiry, and follow-up check for an accepted exception.
- Keep the source commit, base and build image digests, produced image digest, scan report, and approval with the release evidence. Rebuild and rescan after a base image update or a material change to the image contents.
Before release
Image-foundation review
- Every base and build image reference is pinned by an approved digest.
- The source, publisher, digest, and provenance checks are recorded.
- Build tools stay in the build stage; credentials and caches do not enter runtime layers.
- The final image runs as a non-root user with only the files and tools it needs.
- GitLab scans the commit's built image and reviewers inspect its report artifact.
- Release policy defines the finding threshold, exception owner, and protected registry path.
Further reading