Dockerfile Linter & Formatter
指导
Dockerfile Linter & Formatter
Lint and format your Dockerfiles with best-practice rules inspired by hadolint. Check for security issues, optimize layers, enforce conventions, and format consistently — all in the browser.
如何使用
Paste your Dockerfile into the Linter tab to scan for errors, warnings, and optimization opportunities. Each issue shows the line number, severity level, and an explanation. Switch to the Formatter tab to apply consistent formatting with uppercase instructions, combined RUN commands, and clean spacing.
特征
- Best Practice Linting – 50+ rules covering security, layer optimization, and Docker conventions
- Severity Levels – Error, warning, and info classifications with detailed explanations
- Security Scanning – Detect hardcoded secrets, root user, unpinned base images, and more
- Layer Optimization – Suggestions to minimize layers and improve build caching
- Consistent Formatting – Uppercase instructions, combined RUN commands, sorted arguments
- Syntax Highlighting – Color-coded Dockerfile display in both tabs
- Sample Dockerfile – Quick test with a pre-filled example
常问问题
-
Dockerfile 是什么,Docker 如何使用它?
Dockerfile 是一个包含指令序列的文本文件,Docker 使用这些指令来构建容器镜像。每条指令(FROM、RUN、COPY、CMD 等)都会在镜像中创建一个新层。Docker 从上到下读取 Dockerfile,执行每条指令来组装最终镜像。分层架构允许 Docker 缓存未更改的层,从而加快重新构建的速度。编写良好的 Dockerfile 可以生成更小、更安全、更高效的容器镜像。
-
为什么应该在 Dockerfile 中合并 RUN 命令?
每条 RUN 指令都会创建一个新的镜像层。当您在多个 RUN 命令中安装软件包时,即使后续的 RUN 命令删除了它们,较早的层也会保留包管理器缓存 — 层一旦创建就不可变。使用 && 将命令合并到单个 RUN 命令中可以减少总层数,并允许清理(如 rm -rf /var/lib/apt/lists/*)来实际减小镜像大小。这是 Docker 镜像大小最有效率的优化之一。
-
什么是多阶段构建,何时应使用它们?
多阶段构建在单个 Dockerfile 中使用多个 FROM 语句。每个 FROM 都以自己的基础镜像启动一个新的构建阶段。您可以使用 COPY --from=stage_name 将工件从早期阶段复制到后期阶段。这允许您使用完整的 SDK 镜像进行编译,但只将编译后的二进制文件打包到最小的运行时镜像中。其结果是生产镜像的体积大大减小 — 一个 Go 应用程序可能在一个 1GB 的 SDK 镜像中构建,但在一个 10MB 的 scratch 镜像中部署。
