如何借助Hadolint編寫高質量的 Dockerfile
前言
在容器化的世界中,Dockerfile 就像是構建輕量、便攜和自包含應用環境的藍圖。但是創建組織良好且優化的 Dockerfile 可能有些棘手,需要仔細關注細節并遵循最佳實踐。這就是 Hadolint 登場的地方,就像一位超級英雄,幫助您編寫完美的 Dockerfile。
Hadolint是一個開源工具,它會自動檢查您的Dockerfile是否存在任何問題。它使用一組預定義的規則和ShellCheck來檢查您Dockerfile的每一行,確保您的鏡像安全、快速,并符合行業標準。
在這個指南中,我們將學習如何使用 Hadolint 來編寫高質量的 Dockerfile。
我們將探索 Hadolint 的代碼檢查過程、它的許多規則,以及如何將 Hadolint 納入您的開發工作流程。
我們還將發現如何創建小巧、高效且安全免受常見安全弱點影響的鏡像。
圖片
hadolint簡介
Haskell Dockerfile Linter Hadolint 是一個 Dockerfile 文件檢查工具,幫助您構建符合最佳實踐的 Docker 鏡像。我在所有項目中都使用它,以確保我創建的鏡像小巧、安全、高效且易于維護。
使用代碼檢查工具來檢查 Dockerfile 的原因有很多:
- 遵循 Docker 鏡像的最佳實踐
- 在編寫 Dockerfile 時加快反饋速度,因為檢查工具-可以在構建鏡像之前發現語法錯誤和安全漏洞
- 可以檢查代碼風格是否符合規范
- 可以提高 Dockerfile 的可讀性和可維護性
- 在 CI/CD 流水線中使用它們
- 更深入地了解如何編寫更好的 Dockerfile
Hadolint配備了強大且易于使用的CLI。您可以在多種平臺上安裝它,包括macOS。
$ brew install hadolint
請使用以下命令確認安裝是否成功:
$ hadolint --help
hadolint - Dockerfile Linter written in Haskell
...
讓我們創建一個Dockerfile來測試這個工具,現在將以下內容添加到Dockerfile中。
FROM python
MAINTAINER johndoe@gmail.com
LABEL org.website="containiq.com"
RUN mkdir app && cd app
COPY requirements.txt ./
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . .
CMD python manage.py runserver 0.0.0.0:80000
現在使用這個命令驗證 Dockerfile:
$ hadolint Dockerfile
Dockerfile:1 DL3006 warning: Always tag the version of an image explicitly
Dockerfile:1 DL3049 info: Label `maintainer` is missing.
Dockerfile:2 DL4000 error: MAINTAINER is deprecated
Dockerfile:3 DL3052 warning: Label `org.website` is not a valid URL.
Dockerfile:5 DL3003 warning: Use WORKDIR to switch to a directory
Dockerfile:5 SC2164 warning: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.
Dockerfile:7 DL3045 warning: `COPY` to a relative destination without `WORKDIR` set.
Dockerfile:8 DL3013 warning: Pin versions in pip. Instead of `pip install <package>` use `pip install <package>==<version>` or `pip install --requirement <requirements file>`
Dockerfile:8 DL3042 warning: Avoid use of cache directory with pip. Use `pip install --no-cache-dir <package>`
Dockerfile:9 DL3059 info: Multiple consecutive `RUN` instructions. Consider consolidation.
Dockerfile:9 DL3042 warning: Avoid use of cache directory with pip. Use `pip install --no-cache-dir <package>`
Dockerfile:11 DL3045 warning: `COPY` to a relative destination without `WORKDIR` set.
Dockerfile:13 DL3025 warning: Use arguments JSON notation for CMD and ENTRYPOINT arguments
每行的結構如下:<LINE_NUMBER><RULE_CODE><SEVERITY_LEVEL>:
讓我們更詳細地探討這些參數。
代碼規則
一個規則代碼以DL或SC為前綴。DL前綴表示該規則來自Hadolint直接。SC前綴表示該規則來自SpellCheck,這是一個用于shell腳本的靜態分析工具,與Hadolint一起提供。您可以在這里找到規則的綜合列表。
每個規則都有一個專門的文檔頁面,列出了代碼示例、原理和其他重要細節。請查看DL3006的專門頁面。
您可以使用--ignore RULECODE選項忽略一個或多個規則。
$ hadolint --ignore DL3013 --ignore DL3042 Dockerfile
您也可以在 Dockerfile 中忽略規則。我更喜歡這種方法,因為您可以逐行排除規則代碼,這樣更清晰地知道違規實際發生在哪里。
# hadolint ignore=DL3013
RUN pip install --upgrade pip
Hadolint擁有一個活躍的開源社區。新的規則代碼定期添加,因此請確保定期檢查您是否在運行最新版本的Hadolint。
安全級別
嚴重級別表示違規的嚴重程度。共有六個級別:錯誤(error)、警告(warning)、信息(info)、樣式(style)、忽略(ignore)和無(none)。
CLI 包括一個 --failure-threshold(縮寫為 -t)選項,用于排除特定嚴重級別導致失敗。例如,如果您只希望 Hadolint 在錯誤違規時失敗。
$ hadolint -t error Dockerfile
請注意,來自其他嚴重級別的不符合規范行為仍將被報出來,但不會導致失敗。
如果您不同意某個規則代碼的嚴重級別,您可以通過使用--<SEVERITY_LEVEL> RULECODE選項輕松更改它。例如,以下命令將DL3006升級為錯誤,將DL3045降級為信息(這兩個代碼默認為警告):
$ hadolint --error DL3006 --info DL3045 Dockerfile
Dockerfile:1 DL3006 error: Always tag the version of an image explicitly
Dockerfile:7 DL3045 info: `COPY` to a relative destination without `WORKDIR` set.
標簽檢查
Dockerfile標簽是注釋您的Docker鏡像的絕佳工具。Hadolint提供了一些驗證選項,以確保您的標簽設置正確。
--require-label LABELSCHEMA選項驗證您的標簽是否遵循特定格式。您可以在這里查看所有可接受的格式值。
$ hadolint --require-label maintainer:text --require-label org.website:url Dockerfile
Dockerfile:2 DL3049 info: Label `maintainer` is missing.
Dockerfile:3 DL3052 warning: Label `org.website` is not a valid URL.
The --strict-labels 選項會驗證在您的模式中定義的標簽之外是否有額外的標簽。
$ hadolint --require-label maintainer:text --strict-labels Dockerfile
Dockerfile:3 DL3050 info: Superfluous label(s) present.
配置文件
將選項手動傳遞到每次 Hadolint 運行中可能會很煩人且容易出錯。Hadolint 很方便地提供了配置文件支持,可以將所有選項存儲在一個地方。這個文件可以存在于各種位置,但我通常會將其放在存儲庫的根目錄下,命名為 .hadolint.yaml。
override:
error:
- DL3006
info:
- DL3045
label-schema:
maintainer: text
org.website: url
strict-labels: true
修復 Dockerfile
逐個解決每個錯誤是學習 Dockerfile 最佳實踐的絕佳方法。如上所述,每條規則都有非常清晰和詳細的文檔頁面。嘗試一下,完成后再回顧這篇文章。
到這一步,Hadolint 應該不會報任何錯誤。你的文件應該看起來類似于這樣:
FROM python:3.10
LABEL maintainer="johndoe@gmail.com"
LABEL org.website="https://www.airplane.dev/"
WORKDIR /app
COPY requirements.txt ./
# hadolint ignore=DL3013
RUN pip install --upgrade --no-cache-dir pip && \
pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
需要進一步解釋的一些變化:
- 我們將使用最新可用的 Python 次要版本(目前為 3.10)標記 Python 基礎鏡像。我們不包括補丁版本(3.10.2),因為 Python 的補丁版本向后兼容,通常包含有用的 bug 修復。
- 我通常喜歡使用/app工作目錄來保持我的 Docker 鏡像一致,但您可以使用任何您想要的新目錄或現有目錄。
- 我們忽略 DL3013,因為我們想要下載最新版本的 pip。沒有必要將其固定到特定版本。
集成
Hadolint包含許多方便的集成功能,可以在整個開發過程中自動運行代碼檢查工具。我最喜歡的集成有:
- VS Code:直接在編輯器中運行Hadolint
- pre-commit:在每次git提交時運行Hadolint
- GitHub Actions:在GitHub的CI/CD中運行Hadolint
集成非常重要,特別是在較大的團隊中,因為一些開發人員會忘記手動運行代碼檢查工具。我在開始新的Docker項目時立即設置這些集成。
總結
正如您所看到的,這個工具很容易上手,它可以在幾秒鐘內提高您的 Dockerfile 的質量。Hadolint 并不是唯一一個用于 Dockerfile 的代碼檢查工具。Docker 引擎本身也包含一個,但更多用于檢查基本錯誤。此外,還有來自 Snyk 的一個代碼檢查工具,可能更專注于安全問題。