container-scan-hadolint
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDockerfile 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 (or download binary)
brew install hadolint - Verify:
hadolint --version
- 已安装Hadolint(执行或下载二进制文件)
brew install hadolint - 验证安装:
hadolint --version
Instructions
操作步骤
- Identify the target — Determine the Dockerfile(s) to lint.
- 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
- Multiple files:
- Parse the results — Read JSON output and present findings:
| # | Severity | Rule | Line | Finding | Remediation |
|---|----------|------|------|---------|-------------|- Summarize — Provide total issues by severity and specific Dockerfile fixes.
- 确定目标 —— 明确需要进行代码检查的Dockerfile文件。
- 运行扫描:
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
- 多文件扫描:
- 解析结果 —— 读取JSON输出并呈现检查结果:
| 序号 | 严重程度 | 规则 | 行号 | 问题描述 | 修复方案 |
|---|----------|------|------|---------|-------------|- 总结 —— 按严重程度统计问题总数,并给出具体的Dockerfile修复建议。
Key Hadolint Rules
核心Hadolint规则
| Rule | Description |
|---|---|
| DL3000 | Use absolute WORKDIR |
| DL3002 | Do not switch to root user |
| DL3003 | Use WORKDIR instead of |
| DL3006 | Always tag image version (no |
| DL3007 | Use specific package versions |
| DL3008 | Pin versions in |
| DL3009 | Delete apt lists after install |
| DL3018 | Pin versions in |
| DL3025 | Use JSON form for CMD |
| DL4006 | Set SHELL with pipefail |
| SC2086 | ShellCheck: double quote to prevent globbing |
| 规则 | 描述 |
|---|---|
| DL3000 | 使用绝对路径作为WORKDIR |
| DL3002 | 不要切换到root用户 |
| DL3003 | 使用WORKDIR替代 |
| DL3006 | 始终为镜像指定版本标签(不要使用 |
| DL3007 | 使用特定的软件包版本 |
| DL3008 | 在 |
| DL3009 | 安装完成后删除apt列表 |
| DL3018 | 在 |
| DL3025 | 使用JSON格式编写CMD命令 |
| DL4006 | 设置SHELL时启用pipefail |
| SC2086 | ShellCheck:使用双引号避免通配符展开 |