container-scan-hadolint

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dockerfile Linting with Hadolint

使用Hadolint进行Dockerfile代码检查

You are a security engineer linting Dockerfiles using Hadolint to enforce best practices and detect security issues.
你是一名安全工程师,正在使用Hadolint对Dockerfile进行代码检查,以此强化最佳实践并检测安全问题。

When to use

适用场景

Use this skill when asked to lint or review a Dockerfile for security and best practice issues.
当需要对Dockerfile进行代码检查或审核,以排查安全与最佳实践相关问题时,可使用本技能。

Prerequisites

前置条件

  • Hadolint installed (
    brew install hadolint
    or download binary)
  • Verify:
    hadolint --version
  • 已安装Hadolint(执行
    brew install hadolint
    或下载二进制文件)
  • 验证安装:
    hadolint --version

Instructions

操作步骤

  1. Identify the target — Determine the Dockerfile(s) to lint.
  2. Run the scan:
    bash
    hadolint --format json <Dockerfile> > hadolint-results.json
    • Multiple files:
      hadolint --format json Dockerfile Dockerfile.dev
    • Ignore specific rules:
      hadolint --ignore DL3008 --ignore DL3009 --format json Dockerfile
    • Severity threshold:
      hadolint --failure-threshold warning --format json Dockerfile
  3. Parse the results — Read JSON output and present findings:
| # | Severity | Rule | Line | Finding | Remediation |
|---|----------|------|------|---------|-------------|
  1. Summarize — Provide total issues by severity and specific Dockerfile fixes.
  1. 确定目标 —— 明确需要进行代码检查的Dockerfile文件。
  2. 运行扫描:
    bash
    hadolint --format json <Dockerfile> > hadolint-results.json
    • 多文件扫描:
      hadolint --format json Dockerfile Dockerfile.dev
    • 忽略特定规则:
      hadolint --ignore DL3008 --ignore DL3009 --format json Dockerfile
    • 严重程度阈值:
      hadolint --failure-threshold warning --format json Dockerfile
  3. 解析结果 —— 读取JSON输出并呈现检查结果:
| 序号 | 严重程度 | 规则 | 行号 | 问题描述 | 修复方案 |
|---|----------|------|------|---------|-------------|
  1. 总结 —— 按严重程度统计问题总数,并给出具体的Dockerfile修复建议。

Key Hadolint Rules

核心Hadolint规则

RuleDescription
DL3000Use absolute WORKDIR
DL3002Do not switch to root user
DL3003Use WORKDIR instead of
cd
DL3006Always tag image version (no
:latest
)
DL3007Use specific package versions
DL3008Pin versions in
apt-get install
DL3009Delete apt lists after install
DL3018Pin versions in
apk add
DL3025Use JSON form for CMD
DL4006Set SHELL with pipefail
SC2086ShellCheck: double quote to prevent globbing
规则描述
DL3000使用绝对路径作为WORKDIR
DL3002不要切换到root用户
DL3003使用WORKDIR替代
cd
命令
DL3006始终为镜像指定版本标签(不要使用
:latest
DL3007使用特定的软件包版本
DL3008
apt-get install
中固定软件包版本
DL3009安装完成后删除apt列表
DL3018
apk add
中固定软件包版本
DL3025使用JSON格式编写CMD命令
DL4006设置SHELL时启用pipefail
SC2086ShellCheck:使用双引号避免通配符展开