以下内容均来自个人笔记并重新梳理,如有错误欢迎指正!
背景介绍
笔者在《专题三:Dockerfile 相关》及《Dockerfile 指令对 Docker 镜像层数的影响》等文章中已经介绍过 Dockerfile 相关知识及其运用。但是在实际工作中 Dockerfile 肯定不是随便写写就行了,而是推荐遵照最佳实践原则对其进行优化,以期达到减少镜像体积、提升构建效率及容器安全性等目标。
工欲善其事,必先利其器。本文将针对 Dockerfile 的优化介绍一款辅助工具,帮助大家提升工作效率。
hadolint 介绍
hadolint 是一款专门用于检查 Dockerfile 语法的静态分析工具,可以帮助使用者构建符合最佳实践的 Docker 镜像。
hadolint 作为一个智能的 Dockerfile 筛选器,主要工作流程如下:
GitHub 地址:https://github.com/hadolint/hadolint 最佳实践:https://docs.docker.com/build/building/best-practices
hadolint 使用
1、在线检查方式
网址:https://hadolint.github.io/hadolint/
# Dockerfile 示例
FROM debian
RUN apt update && apt install -y supervisor
COPY supervisord.conf /etc/supervisor/conf.d/
EXPOSE 80000
CMD ["/usr/bin/supervisord"]
将以上 Dockerfile 内容粘贴至文本框后点击 Lint 按钮,高亮部分即为 hadolint 针对语法检查结果反馈的优化建议,并可点击跳转至详情页。
2、二进制检查方式
wget -O hadolint https://github.com/hadolint/hadolint/releases/download/v2.12.0/hadolint-Linux-x86_64
chmod +x hadolint && mv hadolint /usr/local/bin
hadolint -v
hadolint Dockerfile
hadolint Dockerfile --ignore DL3006 --ignore DL3027
3、Docker 检查方式
docker run --rm -i ghcr.io/hadolint/hadolint < Dockerfile
hadolint 命令行选项
# hadolint -h
hadolint - Dockerfile Linter written in Haskell
Usage: hadolint [-v|--version] [-c|--config FILENAME] [DOCKERFILE...]
[--file-path-in-report FILEPATHINREPORT] [--no-fail]
[--no-color] [-V|--verbose] [-f|--format ARG] [--error RULECODE]
[--warning RULECODE] [--info RULECODE] [--style RULECODE]
[--ignore RULECODE]
[--trusted-registry REGISTRY (e.g. docker.io)]
[--require-label LABELSCHEMA (e.g. maintainer:text)]
[--strict-labels] [--disable-ignore-pragma]
[-t|--failure-threshold THRESHOLD]
Lint Dockerfile for errors and best practices
Available options:
-h,--help Show this help text
-v,--version Show version
-c,--config FILENAME Path to the configuration file
--file-path-in-report FILEPATHINREPORT
The file path referenced in the generated report.
This only applies for the 'checkstyle' format and is
useful when running Hadolint with Docker to set the
correct file path.
--no-fail Don't exit with a failure status code when any rule
is violated
--no-color Don't colorize output
-V,--verbose Enables verbose logging of hadolint's output to
stderr
-f,--format ARG The output format for the results [tty | json |
checkstyle | codeclimate | gitlab_codeclimate | gnu |
codacy | sonarqube | sarif] (default: tty)
--error RULECODE Make the rule `RULECODE` have the level `error`
--warning RULECODE Make the rule `RULECODE` have the level `warning`
--info RULECODE Make the rule `RULECODE` have the level `info`
--style RULECODE Make the rule `RULECODE` have the level `style`
--ignore RULECODE A rule to ignore. If present, the ignore list in the
config file is ignored
--trusted-registry REGISTRY (e.g. docker.io)
A docker registry to allow to appear in FROM
instructions
--require-label LABELSCHEMA (e.g. maintainer:text)
The option --require-label=label:format makes
Hadolint check that the label `label` conforms to
format requirement `format`
--strict-labels Do not permit labels other than specified in
`label-schema`
--disable-ignore-pragma Disable inline ignore pragmas `# hadolint
ignore=DLxxxx`
-t,--failure-threshold THRESHOLD
Exit with failure code only when rules with a
severity equal to or above THRESHOLD are violated.
Accepted values: [error | warning | info | style |
ignore | none] (default: info)