Zum Inhalt springen
Trainingslager IT-Trainingslager

Tag 12: Linten – hadolint, dockle und ShellCheck

Ziel: Du prüfst Containerfiles vor dem Build mit hadolint, fertige Images mit dockle und die Shell-Skripte des Labors mit ShellCheck, steuerst Regeln gezielt und lässt alle drei eine Pipeline stoppen. · Dauer: ca. 75 min · Voraussetzung: Tag 11

Briefing

  • hadolint liest das Containerfile, nicht das Image: über 60 eigene Regeln (DL…) plus ShellCheck (SC…) für jeden RUN.
  • dockle prüft das fertige Image gegen den CIS Docker Benchmark (CIS-DI-…) und eigene Regeln (DKL-…): root-User, Secrets in ENV, setuid-Dateien, Paket-Caches.
  • ShellCheck prüft Shell-Skripte (SC…): fehlende Anführungszeichen, cd ohne Fehlerbehandlung, nicht gefundene source-Dateien. hadolint nutzt es für RUN.
  • Alle drei kennen Schweregrade, Ausnahmen per Datei und einen Exit-Code für die Pipeline.
  • dockle spricht die Docker-API, ignoriert aber DOCKER_HOST. Lokale Podman-Images bekommt es über DOCKLE_HOST (Podman-Socket) oder als Archiv.
 Containerfile ──hadolint──► DL/SC-Befunde ──build──► Image ──dockle──► CIS/DKL-Befunde
   .hadolint.yaml · # hadolint ignore=…               .dockleignore · --accept-key
 *.sh ──shellcheck──► SC-Befunde        .shellcheckrc · # shellcheck disable=…

Drill 1: hadolint – installieren und laufen lassen

Steckbrief hadolint
Aufgabe Dockerfiles/Containerfiles auf Fehler und Best Practices prüfen
Besonderheit parst das Dockerfile, prüft RUN-Zeilen zusätzlich mit ShellCheck
Projekt github.com/hadolint/hadolint (GPL-3.0)
Version im Buch 2.15.1
source ~/ct-lab/setup/env.sh
mkdir -p ~/ct-lab/tag12 && cd ~/ct-lab/tag12
cp -R "$LAB/code/notes/." . && cp -R "$LAB/code/tag11/notes/." .
grep "^hadolint $(dpkg --print-architecture) " "$LAB/setup/tools.lock" | cut -c1-60
hadolint --version

Erwartet (gekürzt):

hadolint arm64 https://github.com/hadolint/hadolint/releases
Haskell Dockerfile Linter 2.15.1

Installiert hast du hadolint an Tag 0, geprüft gegen diese Zeile aus tools.lock. Jetzt die drei echten Dockerfiles:

hadolint --no-color notes-api/Dockerfile notes-web/Dockerfile notes-worker/Dockerfile
echo "Exit: $?"

Erwartet:

notes-web/Dockerfile:25 DL3066 info: Non-numeric user-id may not be resolvable by
host system
Exit: 0

Format: Datei:Zeile Regel Schweregrad: Text. USER node ist ein Name; ob er auf dem Host auflösbar ist, weiß hadolint nicht – nur ein Hinweis. Exit 0, weil die .hadolint.yaml im Projektordner erst ab warning scheitern lässt.

Fortschritt speichern? Kostenlos registrieren

Drill 2: Regeln an einem kaputten Containerfile

cat > notes-api/Containerfile.lint <<'EOF'
# notes-api/Containerfile.lint – Stand Ende Tag 12 (absichtlich fehlerhaft)
FROM golang:latest
RUN apt-get update && apt-get install -y curl
ADD . /src
RUN cd /src && go build -o /usr/local/bin/notes-api . \
 && chmod 755 $(which notes-api)
FROM quay.io/example/runtime:1.0
COPY --from=0 /usr/local/bin/notes-api /notes-api
USER root
CMD /notes-api
EOF
hadolint --no-color notes-api/Containerfile.lint; echo "Exit: $?"

Erwartet (Texte gekürzt):

notes-api/Containerfile.lint:2 DL3007 warning: Using latest is prone to errors …
notes-api/Containerfile.lint:3 DL3015 info: Avoid additional packages by specifying
`--no-install-recommends`
notes-api/Containerfile.lint:3 DL3008 warning: Pin versions in apt get install. …
notes-api/Containerfile.lint:4 DL3020 error: Use COPY instead of ADD for files and
folders
notes-api/Containerfile.lint:5 DL3003 warning: Use WORKDIR to switch to a directory
notes-api/Containerfile.lint:5 SC2046 warning: Quote this to prevent word splitting.
notes-api/Containerfile.lint:7 DL3026 error: Use only an allowed registry in the FROM
image
notes-api/Containerfile.lint:9 DL3002 error: Last USER should not be root
notes-api/Containerfile.lint:9 DL3066 info: Non-numeric user-id may not be resolvable
by host system
notes-api/Containerfile.lint:10 DL3025 error: Use arguments JSON notation for CMD and
ENTRYPOINT arguments
Exit: 1

Neun DL-Regeln und eine aus ShellCheck (SC2046: $(which …) ungequotet). Zwei Befunde kommen aus der Projektkonfiguration: DL3026, weil quay.io nicht unter trustedRegistries steht, und error statt warning bei DL3002/DL3025 über override. Jede Regel ist im Wiki erklärt: github.com/hadolint/hadolint/wiki/DL3020.

Die Schweregrade von oben nach unten: error, warning, info, style. --failure-threshold (kurz -t) legt fest, ab wann der Exit-Code 1 wird:

hadolint -t error notes-api/Containerfile.lint >/dev/null; echo "Exit: $?"
hadolint --no-fail notes-api/Containerfile.lint >/dev/null; echo "Exit: $?"

Erwartet:

Exit: 1
Exit: 0

--no-fail meldet alles, bricht aber nie ab – gut für die Einführung in ein bestehendes Projekt. (Vorsicht: -t none lieferte im Test trotzdem Exit 1.)

Fortschritt speichern? Kostenlos registrieren

Drill 3: Regeln steuern – Kommentar, Flag, Konfiguration

Drei Ebenen, von eng nach weit. Kommentar direkt über der Zeile – gilt nur für den folgenden Befehl:

sed -i '3i # hadolint ignore=DL3008,DL3015' notes-api/Containerfile.lint
sed -n '2,4p' notes-api/Containerfile.lint
hadolint --no-color notes-api/Containerfile.lint | grep -c ':4 DL30'
hadolint --no-color --disable-ignore-pragma notes-api/Containerfile.lint \
  | grep -c ':4 DL30'

Erwartet:

FROM golang:latest
# hadolint ignore=DL3008,DL3015
RUN apt-get update && apt-get install -y curl
0
2

--disable-ignore-pragma hebelt alle Kommentare aus – so prüft eine strenge Pipeline, was Entwickler lokal wegdrücken. Flag für einen Lauf: --ignore DL3059. Konfiguration für das Projekt: Die .hadolint.yaml bekommt eine Liste ignored.

cat > .hadolint.yaml <<'EOF'
# .hadolint.yaml – Stand Ende Tag 12: gilt, wenn hadolint im Projektordner läuft
failure-threshold: warning
override:
  error:
    - DL3002   # letzter USER ist root
    - DL3025   # CMD/ENTRYPOINT in Shell-Form
ignored:
  - DL3059     # mehrere RUN hintereinander: die Layer misst dive (Tag 11)
trustedRegistries:
  - docker.io
  - gcr.io
  - ghcr.io
EOF
hadolint --no-color notes-api/Containerfile.schlecht; echo "Exit: $?"

Erwartet:

notes-api/Containerfile.schlecht:3 DL3018 warning: Pin versions in apk add. Instead
of `apk add <package>` use `apk add <package>=<version>`
notes-api/Containerfile.schlecht:3 DL3019 info: Use the `--no-cache` switch to avoid
the need to use `--update` and remove `/var/cache/apk/*` when done installing packages
Exit: 1

Ohne ignored stünden hier zusätzlich zwei DL3059-Hinweise für die drei RUN hintereinander. hadolint sucht die Konfiguration im aktuellen Ordner, dann unter ~/.config/hadolint.yaml; -c datei wählt sie ausdrücklich.

Labels prüft hadolint auch – als Einmalprüfung per Flag:

hadolint --no-color --require-label org.opencontainers.image.source:url \
  notes-api/Dockerfile notes-web/Dockerfile | cut -c1-72

Erwartet:

notes-api/Dockerfile:7 DL3049 info: Label `org.opencontainers.image.sour
notes-api/Dockerfile:13 DL3049 info: Label `org.opencontainers.image.sou
notes-web/Dockerfile:15 DL3049 info: Label `org.opencontainers.image.sou
notes-web/Dockerfile:25 DL3066 info: Non-numeric user-id may not be reso

notes-web hat gar keine Labels. Bei notes-api fehlen sie in den Stages base (Zeile 7) und test (Zeile 13) – hadolint prüft jede Stage, auch solche, die nie ausgeliefert werden. Deshalb steht label-schema hier nicht in der Projektdatei.

Fortschritt speichern? Kostenlos registrieren

Drill 4: Formate für Pipelines

-f kennt tty (Standard), json, sarif, checkstyle, codeclimate, gitlab_codeclimate, gnu, codacy, sonarqube und junit:

hadolint -f json notes-api/Containerfile.lint | jq -c '.[0]'
hadolint -f sarif notes-api/Containerfile.lint -o lint.sarif; echo "Exit: $?"
jq -c '{v: .version, tool: .runs[0].tool.driver.name,
        n: (.runs[0].results | length)}' lint.sarif

Erwartet:

{"code":"DL3007","column":1,"file":"notes-api/Containerfile.lint","level":"warning",
"line":2,"message":"Using latest is prone to errors if the image will ever update. Pin
the version explicitly to a release tag"}
Exit: 1
{"v":"2.1.0","tool":"Hadolint","n":8}

SARIF 2.1.0 lesen GitHub Code Scanning und viele IDEs direkt ein; GitLab will gitlab_codeclimate. Der Exit-Code hängt nicht vom Format ab.

Fortschritt speichern? Kostenlos registrieren

Drill 5: dockle – installieren, erster Befund

Steckbrief dockle
Aufgabe fertige Images auf Sicherheits- und Best-Practice-Regeln prüfen
Besonderheit CIS Docker Benchmark plus eigene Checks; liest Docker-API, Registry, Archiv
Projekt github.com/goodwithtech/dockle (Apache-2.0)
Version im Buch 0.4.15
dockle --version
dockle --no-color docker.io/library/nginx:1.30.6-alpine | cut -c1-80

Erwartet (gekürzt):

dockle version 0.4.15
FATAL   - CIS-DI-0010: Do not store credential in environment variables/files
    * Suspicious ENV key found : KEY_SHA512 on RUN /bin/sh -c set -x     && addgr
WARN    - CIS-DI-0001: Create a user for the container
    * Last user should not be root
INFO    - CIS-DI-0005: Enable Content trust for Docker
    * export DOCKER_CONTENT_TRUST=1 before docker pull/build
INFO    - CIS-DI-0006: Add HEALTHCHECK instruction to the container image
    * not found HEALTHCHECK statement

dockle hat das Image direkt aus Docker Hub gelesen – ohne Podman. Der FATAL-Befund ist ein Fehlalarm: KEY_SHA512 ist die Prüfsumme des nginx-Signaturschlüssels, kein Secret. Solche Namen nimmst du mit --accept-key aus. --exit-code 1 (kurz -c) macht dockle pipelinetauglich:

dockle --no-color --accept-key KEY_SHA512 --exit-code 1 \
  docker.io/library/nginx:1.30.6-alpine | grep -E '^(FATAL|WARN)'; \
  echo "Exit: ${PIPESTATUS[0]}"

Erwartet:

WARN    - CIS-DI-0001: Create a user for the container
Exit: 1

nginx läuft als root (der Master-Prozess wechselt erst später auf User nginx). Ab welchem Grad dockle scheitert, legt --exit-level fest (Standard WARN).

Fortschritt speichern? Kostenlos registrieren

Drill 6: Wie dockle an Podman-Images kommt

dockle --no-color localhost/notes-api:0.1.0 2>&1 | cut -c1-90

Erwartet:

2026-10-30T22:16:36.012Z    FATAL   unable to initialize a image struct: failed to init
ialize source: pinging container registry localhost: Get "https://localhost/v2/": dial

dockle sucht die Docker-API unter /var/run/docker.sock, findet keine und hält localhost/… für eine Registry – DOCKER_HOST aus Tag 4 beachtet es nicht. Zwei Auswege. Erstens das Archiv:

podman save -q -o api.tar localhost/notes-api:0.1.0
dockle --no-color --input api.tar; echo "Exit: $?"
rm api.tar

Erwartet:

SKIP    - DKL-LI-0001: Avoid empty password
    * failed to detect etc/shadow,etc/master.passwd
SKIP    - DKL-LI-0002: Be unique UID/GROUP
    * failed to detect etc/passwd
    * failed to detect etc/group
INFO    - CIS-DI-0005: Enable Content trust for Docker
    * export DOCKER_CONTENT_TRUST=1 before docker pull/build
Exit: 0

Zweitens der Podman-Socket, der seit Tag 4 läuft und die Docker-API spricht. dockle will ihn ausdrücklich, als --host oder in DOCKLE_HOST:

systemctl --user is-active podman.socket
export DOCKLE_HOST="unix://$XDG_RUNTIME_DIR/podman/podman.sock"
dockle --no-color localhost/notes-api:0.1.0 | grep -E '^[A-Z]'

Erwartet:

active
SKIP    - DKL-LI-0001: Avoid empty password
SKIP    - DKL-LI-0002: Be unique UID/GROUP
INFO    - CIS-DI-0005: Enable Content trust for Docker

SKIP: In scratch gibt es kein /etc/passwd – nichts zu prüfen. Images in deiner Registry liest dockle direkt, das Login nimmt es aus ~/.docker/config.json (Tag 7):

dockle --no-color "$REG/notes/notes-api:0.1.0" | grep -c 'CIS-DI-0006'

Erwartet:

0

Kein Befund zu HEALTHCHECK – weil du seit Tag 1 mit --format docker baust. Ohne den Schalter baut Podman im OCI-Format, und das kennt kein HEALTHCHECK. Probier es mit einer Wegwerf-Kopie:

podman build --build-arg VERSION=0.1.0 -t localhost/notes-api:0.1.0-oci \
  "$NOTES/notes-api" 2>&1 | awk '/not supported/ && !n++' | cut -c1-88
dockle --no-color localhost/notes-api:0.1.0-oci | grep -A1 'CIS-DI-0006'
podman rmi localhost/notes-api:0.1.0-oci >/dev/null

Erwartet:

time="2026-10-30T22:20:23Z" level=warning msg="HEALTHCHECK is not supported for OCI imag
INFO    - CIS-DI-0006: Add HEALTHCHECK instruction to the container image
    * not found HEALTHCHECK statement

Podman warnt nur und baut weiter. dockle merkt den Verlust – ein guter Grund, das Image-Format in der Pipeline nicht dem Zufall zu überlassen.

Fortschritt speichern? Kostenlos registrieren

Drill 7: Befunde an den notes-Images

dockle --no-color localhost/notes-api:schlecht | grep -E '^[A-Z]'
dockle --no-color localhost/notes-worker:0.1.0 | grep -A1 -E '^(FATAL|WARN|INFO)' \
  | cut -c1-80 | head -9

Erwartet:

FATAL   - DKL-DI-0004: Use "apk add" with --no-cache
WARN    - CIS-DI-0001: Create a user for the container
INFO    - CIS-DI-0005: Enable Content trust for Docker
INFO    - CIS-DI-0006: Add HEALTHCHECK instruction to the container image
INFO    - DKL-LI-0003: Only put necessary files
FATAL   - DKL-DI-0005: Clear apt-get caches
    * Use 'rm -rf /var/lib/apt/lists' after 'apt-get install|update' : RUN /bin/sh
INFO    - CIS-DI-0005: Enable Content trust for Docker
    * export DOCKER_CONTENT_TRUST=1 before docker pull/build
INFO    - CIS-DI-0006: Add HEALTHCHECK instruction to the container image
    * not found HEALTHCHECK statement
INFO    - CIS-DI-0008: Confirm safety of setuid/setgid files
    * setgid file: grwxr-xr-x usr/sbin/unix_chkpwd

Das schlechte Image läuft als root, lässt den apk-Cache liegen und enthält mit dem kompletten Go-SDK auch dessen Beispiel-Dockerfiles (DKL-LI-0003). Beim worker ist DKL-DI-0005 ein Fehlalarm: Der Befund stammt aus python:3.15-slim, das mit apt-get dist-clean aufräumt – den Befehl kennt dockle 0.4.15 nicht. CIS-DI-0008 listet su, passwd, mount & Co. mit setuid-Bit aus Debian – ein Kandidat für die Härtung, kein akuter Fehler.

Bewusst akzeptierte Checks gehören in .dockleignore im aktuellen Ordner – mit Grund:

cat > .dockleignore <<'EOF'
# .dockleignore – Stand Ende Tag 12: bewusst akzeptierte dockle-Checks
# Docker Content Trust gibt es bei Podman nicht; signiert wird an Tag 14 mit cosign
CIS-DI-0005
# python:3.15-slim räumt mit "apt-get dist-clean" auf, das kennt dockle 0.4.15 nicht
DKL-DI-0005
EOF
dockle --no-color -c 1 localhost/notes-worker:0.1.0 | grep -E '^[A-Z]'
echo "Exit: ${PIPESTATUS[0]}"
dockle --no-color -c 1 localhost/notes-api:schlecht >/dev/null; echo "Exit: $?"

Erwartet:

INFO    - CIS-DI-0006: Add HEALTHCHECK instruction to the container image
INFO    - CIS-DI-0008: Confirm safety of setuid/setgid files
Exit: 0
Exit: 1

Für einen Lauf geht dasselbe mit -i CIS-DI-0005. Für die Pipeline gibt es wie bei hadolint -f json und -f sarif:

dockle -f json localhost/notes-api:0.1.0 | jq -c '.summary'

Erwartet:

{"fatal":0,"warn":0,"info":0,"skip":2,"pass":14}

Fortschritt speichern? Kostenlos registrieren

Drill 8: ShellCheck – ein Skript mit Fehlern

Steckbrief ShellCheck
Aufgabe Shell-Skripte (bash, sh, dash, ksh) statisch auf Fehler prüfen
Besonderheit jeder Befund mit Code SC…, Erklärung und Vorschlag; steckt auch in hadolint
Projekt github.com/koalaman/shellcheck (GPL-3.0)
Version im Buch 0.11.0 (Ubuntu-Paket, entspricht dem aktuellen Release)

ShellCheck hast du an Tag 0 mit apt installiert. Ein kleines Skript soll alle notes-Images in die Registry schieben – mit typischen Fehlern:

cd ~/ct-lab/tag12
dpkg -l shellcheck | tail -1 | awk '{print $2, $3}'
cat > push-alle.sh <<'EOF'
#!/bin/bash
# push-alle.sh – Stand Ende Tag 12 (absichtlich fehlerhaft)
source ~/ct-lab/setup/env.sh
ziel=$1
cd $LAB/tag12
for img in $(podman images --format '{{.Repository}}:{{.Tag}}' | grep notes); do
  podman push $img $REG/$ziel/${img##*/}
done
echo "fertig: $(ls $LAB/tag12 | wc -l) Dateien im Ordner"
EOF
shellcheck push-alle.sh; echo "Exit: $?"

Erwartet (gekürzt):

shellcheck 0.11.0-2

In push-alle.sh line 3:
source ~/ct-lab/setup/env.sh
       ^-------------------^ SC1090 (warning): ShellCheck can't follow non-constant
source. Use a directive to specify location.

In push-alle.sh line 5:
cd $LAB/tag12
^-----------^ SC2164 (warning): Use 'cd ... || exit' or 'cd ... || return' in case cd
fails.
   ^--^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean:
cd "$LAB"/tag12 || exit

In push-alle.sh line 7:
  podman push $img $REG/$ziel/${img##*/}
              ^--^ SC2086 (info): Double quote to prevent globbing and word splitting.
…
In push-alle.sh line 9:
echo "fertig: $(ls $LAB/tag12 | wc -l) Dateien im Ordner"
                ^-----------^ SC2012 (info): Use find instead of ls to better handle
non-alphanumeric filenames.
…
Exit: 1

Jeder Befund zeigt die Stelle, den Code und einen Vorschlag. Die wichtigsten: SC2164 – schlägt cd fehl, pusht das Skript im falschen Ordner weiter. SC2086 – $ziel mit Leerzeichen zerfällt in mehrere Wörter. SC1090 – den Pfad mit ~ kann ShellCheck nicht verfolgen, also prüft es env.sh nicht mit. Jeder Code ist erklärt unter shellcheck.net/wiki/SC2086. Die bereinigte Fassung:

cat > push-alle.sh <<'EOF'
#!/bin/bash
# push-alle.sh – Stand Ende Tag 12 (bereinigt)
# shellcheck source=../setup/env.sh
source ~/ct-lab/setup/env.sh
ziel=$1
cd "$LAB/tag12" || exit 1
for img in $(podman images --format '{{.Repository}}:{{.Tag}}' | grep notes); do
  podman push "$img" "$REG/$ziel/${img##*/}"
done
echo "fertig: $(find . -maxdepth 1 -type f | wc -l) Dateien im Ordner"
EOF
shellcheck push-alle.sh; echo "Exit: $?"
shellcheck -x push-alle.sh; echo "Exit: $?"

Erwartet:


In push-alle.sh line 4:
source ~/ct-lab/setup/env.sh
       ^-------------------^ SC1091 (info): Not following: ../setup/env.sh was not
specified as input (see shellcheck -x).

For more information:
  https://www.shellcheck.net/wiki/SC1091 -- Not following: ../setup/env.sh wa...
Exit: 1
Exit: 0

Die Direktive # shellcheck source=… sagt, welche Datei hinter dem source steckt (relativ zum aktuellen Ordner). Folgen darf ShellCheck ihr erst mit -x – dann prüft es env.sh mit und kennt dort $LAB und $REG. Exit 0: keine Befunde.

Fortschritt speichern? Kostenlos registrieren

Drill 9: Laborskripte, Ausnahmen und Formate

Jetzt die Skripte, die du selbst geschrieben hast, aus dem Laborordner heraus:

cd ~/ct-lab
shellcheck tag07/run-registry.sh tag12/push-alle.sh; echo "Exit: $?"

Erwartet (gekürzt):


In tag12/push-alle.sh line 4:
source ~/ct-lab/setup/env.sh
       ^-------------------^ SC1091 (info): Not following: ../setup/env.sh was not
specified as input (see shellcheck -x).

For more information:
  https://www.shellcheck.net/wiki/SC1091 -- Not following: ../setup/env.sh wa...
Exit: 1

Nur ein Hinweis (info): Ohne -x folgt ShellCheck dem source nicht. run-registry.sh besteht bereits, es trägt seit Tag 7 die Direktive # shellcheck source=/dev/null. Statt bei jedem Aufruf -x anzugeben, bekommt der Laborordner eine .shellcheckrc – ShellCheck sucht sie vom Skript aus aufwärts:

cat > .shellcheckrc <<'EOF'
# ~/ct-lab/.shellcheckrc – Stand Ende Tag 12: source-Pfade relativ zum Skript auflösen
source-path=SCRIPTDIR
external-sources=true
EOF
shellcheck tag07/run-registry.sh tag12/push-alle.sh; echo "Exit: $?"

Erwartet:

Exit: 0

source-path=SCRIPTDIR löst Pfade relativ zum Skript auf, external-sources=true wirkt wie -x. Eine einzelne Zeile nimmst du mit einer Direktive darüber aus, etwa # shellcheck disable=SC2086; vor der ersten Zeile mit Code gilt sie für die ganze Datei.

Für die Pipeline: -S legt den Mindest-Schweregrad fest (error, warning, info, style), -f das Format. Die fehlerhafte Fassung aus Drill 8 liegt im Begleitcode:

cp "$LAB/code/tag12/push-alle.sh" tag12/push-alle-kaputt.sh
shellcheck -S warning -f gcc tag12/push-alle-kaputt.sh; echo "Exit: $?"
shellcheck -f json1 tag12/push-alle-kaputt.sh | jq -c '.comments[] | [.line, .code]' \
  | tr '\n' ' '; echo

Erwartet:

tag12/push-alle-kaputt.sh:3:8: warning: ShellCheck can't follow non-constant source.
Use a directive to specify location. [SC1090]
tag12/push-alle-kaputt.sh:5:1: warning: Use 'cd ... || exit' or 'cd ... || return' in
case cd fails. [SC2164]
Exit: 1
[3,1090] [5,2164] [5,2086] [7,2086] [7,2086] [7,2086] [7,2086] [9,2012] [9,2086]

-f gcc versteht fast jeder Editor und jede CI, json1 ist für eigene Auswertungen; dazu gibt es checkstyle und diff (Vorschläge als Patch). Mit -S warning fallen die info-Befunde weg, der Exit-Code bleibt 1 wegen der beiden Warnungen.

Und hadolint? Es prüft jedes RUN mit genau diesem ShellCheck – derselbe Code wie im Skript:

cd ~/ct-lab/tag12
cat > Containerfile.sc <<'EOF'
FROM docker.io/library/alpine:3.24
RUN for f in /etc/*.conf; do echo $f; done
EOF
hadolint --no-color Containerfile.sc; echo "Exit: $?"
rm Containerfile.sc

Erwartet:

Containerfile.sc:2 SC2086 info: Double quote to prevent globbing and word splitting.
Exit: 0

Exit 0, weil die .hadolint.yaml erst ab warning scheitern lässt.

Fortschritt speichern? Kostenlos registrieren

Kontrollpunkt

Die echten Dockerfiles bestehen hadolint, Containerfile.lint nicht:

cd ~/ct-lab/tag12
hadolint notes-api/Dockerfile notes-web/Dockerfile notes-worker/Dockerfile
echo "Exit: $?"
hadolint notes-api/Containerfile.lint >/dev/null; echo "Exit: $?"
grep DL3059 .hadolint.yaml

Erwartet:

notes-web/Dockerfile:25 DL3066 info: Non-numeric user-id may not be resolvable by host…
Exit: 0
Exit: 1
  - DL3059     # mehrere RUN hintereinander: die Layer misst dive (Tag 11)

notes-worker besteht dockle, schlecht nicht:

export DOCKLE_HOST="unix://$XDG_RUNTIME_DIR/podman/podman.sock"
dockle -c 1 localhost/notes-worker:0.1.0 >/dev/null 2>&1; echo "Exit: $?"
dockle -c 1 localhost/notes-api:schlecht >/dev/null 2>&1; echo "Exit: $?"

Erwartet:

Exit: 0
Exit: 1

Challenge

Schreib notes-api/Containerfile.lint so um, dass hadolint ohne einen einzigen Befund durchläuft und das gebaute Image localhost/notes-api:lint mit dockle -c 1 besteht.

Hinweis: Ein Build-Stage auf golang:1.27-alpine, ein Laufzeit-Stage auf scratch, numerischer User.

Lösung

cat > notes-api/Containerfile.lint <<'EOF'
# notes-api/Containerfile.lint – Stand Ende Tag 12 (bereinigt)
FROM docker.io/library/golang:1.27-alpine AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/notes-api .
FROM scratch
COPY --from=build /out/notes-api /notes-api
USER 65532:65532
ENTRYPOINT ["/notes-api"]
EOF
hadolint notes-api/Containerfile.lint; echo "Exit: $?"
podman build -q --format docker -f notes-api/Containerfile.lint \
  -t localhost/notes-api:lint notes-api >/dev/null 2>&1
dockle --no-color -c 1 localhost/notes-api:lint | grep -E '^[A-Z]'
echo "Exit: ${PIPESTATUS[0]}"

Erwartet:

Exit: 0
SKIP    - DKL-LI-0001: Avoid empty password
SKIP    - DKL-LI-0002: Be unique UID/GROUP
INFO    - CIS-DI-0006: Add HEALTHCHECK instruction to the container image
Exit: 0

Der Begleitcode code/tag12 enthält die fehlerhafte Fassung – zum Üben.

Fortschritt speichern? Kostenlos registrieren

Enterprise-Notiz

  • hadolint läuft vor dem Build (schnell, kein Image nötig), dockle danach – beide mit Exit-Code als Gate, Ergebnisse als SARIF ins Code-Scanning.
  • Ausnahmen gehören versioniert ins Repository (.hadolint.yaml, .dockleignore), jede mit Grund; in der Pipeline --disable-ignore-pragma erwägen.
  • trustedRegistries auf die interne Registry bzw. den Proxy begrenzen – dann fällt jedes FROM aus fremden Quellen sofort auf.
  • dockle nur mit gepinnter Version und eigener Fehlalarm-Liste: Regeln wie DKL-DI-0005 hinken neuen Basisimages hinterher; das Projekt veröffentlicht selten.
  • ShellCheck gehört in denselben Schritt wie hadolint: shellcheck -S warning -f gcc über alle *.sh im Repository, Ausnahmen in .shellcheckrc mit Kommentar.
  • HEALTHCHECK gibt es nur im Docker-Format; unter Kubernetes übernehmen Probes die Aufgabe, dort ist CIS-DI-0006 ein Kandidat für .dockleignore.