現已釋出!閱讀關於 11 月新增功能和修復的內容。

減少 Docker 構建警告

以下是一些消除 Dockerfile 構建中可能出現的警告的提示。

debconf: delaying package configuration, since apt-utils is not installed

此錯誤通常可以安全忽略,並且難以完全消除。但是,您可以透過將以下內容新增到 Dockerfile 中,將其減少為在安裝所需軟體包時在標準輸出中顯示一條訊息

RUN apt-get update \
    && export DEBIAN_FRONTEND=noninteractive \
    && apt-get -y install --no-install-recommends apt-utils dialog 2>&1

Warning: apt-key output should not be parsed (stdout is not a terminal)

此非關鍵警告告訴您不要解析 apt-key 的輸出,因此只要您的指令碼不解析,就沒有問題。您可以安全地忽略它。

這在 Dockerfile 中發生是因為 apt-key 命令不是從終端執行的。不幸的是,此錯誤無法完全消除,但可以隱藏,除非 apt-key 命令返回非零退出程式碼(表示失敗)。

例如

# (OUT=$(apt-key add - 2>&1) || echo $OUT) will only print the output with non-zero exit code is hit
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | (OUT=$(apt-key add - 2>&1) || echo $OUT)

您還可以設定 APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE 環境變數來抑制警告,但這看起來有點嚇人,因此如果您使用它,請務必在 Dockerfile 中添加註釋

# Suppress an apt-key warning about standard out not being a terminal. Use in this script is safe.
ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn

以紅色顯示的提示資訊

某些 CLI 將特定資訊(例如除錯詳細資訊)輸出到標準錯誤而不是標準輸出。這些資訊將以紅色顯示在 Visual Studio Code 的終端和輸出日誌中。

如果這些訊息是無害的,您可以透過在命令末尾附加 2>&1 將命令的輸出從標準錯誤重定向到標準輸出。

例如

RUN apt-get -y install --no-install-recommends apt-utils dialog 2>&1

如果命令失敗,您仍然可以看到錯誤,但它們不會是紅色的。

避免使用 Docker 構建的映象出現問題

鑑於 Dockerfile 和 Docker Compose 檔案可以在沒有 VS Code 或 devcontainer CLI 的情況下使用,您可能希望告知使用者,如果映象不能按預期工作,他們不應嘗試直接構建它。要解決此問題,您可以新增一個需要指定才能使一切正常工作的構建引數。

例如,您可以將以下內容新增到 Dockerfile 中

ARG vscode
RUN if [[ -z "$devcontainercli" ]] ; then printf "\nERROR: This Dockerfile needs to be built with VS Code !" && exit 1; else printf "VS Code is detected: $devcontainercli"; fi

並將以下內容新增到 devcontainer.json

"build": {
      "dockerfile": "Dockerfile",
      "args": {
          // set devcontainer-cli arg for Dockerfile
          "devcontainercli": "true"
      },
    }

在 Docker Compose 的情況下,您可以將此引數新增到單獨的覆蓋檔案中以擴充套件您的配置,該檔案位於原始碼樹中與主 Docker Compose 檔案不同的位置。

© . This site is unofficial and not affiliated with Microsoft.