本文內容均來自個人筆記并重新梳理,如有錯誤歡迎指正!
如果對您有幫助,煩請點贊、關注、轉發、訂閱專欄!
專欄訂閱入口
|?精選文章?|?Kubernetes?| Docker | Linux?| 羊毛資源?|?工具推薦?|
往期精彩文章
【Docker】(全網首發)Kylin V10 下 MySQL 容器內存占用異常的解決方法
【Docker】(全網首發)Kylin V10 下 MySQL 容器內存占用異常的解決方法(續)
【Linux】全面講解 Shell 變量的那些事
目錄
一、背景介紹
二、hadolint 介紹
三、hadolint 使用
1、在線檢查方式
2、二進制檢查方式
3、Docker 檢查方式
四、hadolint 命令行選項
一、背景介紹
筆者在《專題三:Dockerfile 相關》及《Dockerfile 指令對 Docker 鏡像層數的影響》等文章中已經介紹過 Dockerfile 相關知識及其運用。但是在實際工作中 Dockerfile 肯定不是隨便寫寫就行了,而是推薦遵照最佳實踐原則對其進行優化,以期達到減少鏡像體積、提升構建效率及容器安全性等目標。
工欲善其事,必先利其器。本文將針對 Dockerfile 的優化介紹一款輔助工具,幫助大家提升工作效率。
二、hadolint 介紹
?hadolint 是一款專門用于檢查 Dockerfile 語法的靜態分析工具,可以幫助使用者構建符合最佳實踐的 Docker 鏡像。
hadolint 作為一個智能的 Dockerfile 篩選器,主要工作流程如下:
-
hadolint 將 Dockerfile 解析為 AST(抽象語法樹),以標識與其關聯的每條指令和參數
-
hadolint 根據內置的語法規則,在 AST 之上檢查每條指令和參數。這些語法規則涵蓋了效率、代碼質量、安全性等多個方面。此外,hadolint 還使用了著名的 Shellcheck 來檢查 RUN?指令中的 Bash 代碼
-
Hadolint 會標記所有不符合語法規則的指令和參數,并提供針對性的優化建議?
GitHub 地址:https://github.com/hadolint/hadolint
Dockerfile 最佳實踐:https://docs.docker.com/build/building/best-practices/
三、hadolint 使用
1、在線檢查方式
網址:https://hadolint.github.io/hadolint/
# Dockerfile 示例
FROM?debianRUN 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 HaskellUsage: 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 practicesAvailable options:-h,--help Show this help text-v,--version Show version-c,--config FILENAME Path to the configuration file--file-path-in-report FILEPATHINREPORTThe file path referenced in the generated report.This only applies for the 'checkstyle' format and isuseful when running Hadolint with Docker to set thecorrect file path.--no-fail Don't exit with a failure status code when any ruleis violated--no-color Don't colorize output-V,--verbose Enables verbose logging of hadolint's output tostderr-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 theconfig file is ignored--trusted-registry REGISTRY (e.g. docker.io)A docker registry to allow to appear in FROMinstructions--require-label LABELSCHEMA (e.g. maintainer:text)The option --require-label=label:format makesHadolint check that the label `label` conforms toformat requirement `format`--strict-labels Do not permit labels other than specified in`label-schema`--disable-ignore-pragma Disable inline ignore pragmas `# hadolintignore=DLxxxx`-t,--failure-threshold THRESHOLDExit with failure code only when rules with aseverity equal to or above THRESHOLD are violated.Accepted values: [error | warning | info | style |ignore | none] (default: info)