如何使用Docker/Devcontainer buildargs中的文件中的环境变量?

发布于 2025-02-04 00:51:58 字数 1643 浏览 2 评论 0原文

使用vscode devcontainer,我想在我的devcontainer.json中动态为Docker设置一些构建参数。当前我的devcontainer.json看起来像这样:

{
  "name": "NodeJS & TypeScript",
  "build": {
    "dockerfile": "Dockerfile",
    "args": {
      "VARIANT": "16-bullseye",
      "NPM_PROXY": "http://proxy.domain.com:8080",
      "HTTPS_PROXY": "http://proxy.domain.com:8080",
      "HTTP_PROXY": "http://proxy.domain.com:8080"
    }
  },
  "settings": {},
  "extensions": [
    // ...
  ],      
  "remoteUser": "node",
  "runArgs": ["--env-file", ".devcontainer/.env"]
}

使用我的dockerfile

ARG VARIANT
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}

RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
  && apt-get -y install --no-install-recommends chromium

WORKDIR /usr/app

ARG NPM_PROXY
RUN npm config set https-proxy ${NPM_PROXY} -g

RUN npm i -g @angular/cli

ENV CHROME_BIN=/usr/bin/chromium

如您所见,variantnpm_proxy ARGS用于dockerfile中,但是我也需要http(s)_proxy用于构建参数。我希望这些定义在devcontainer.json中从.env文件中获取与devContainer.json 的文件。 Dockerfile

.env文件看起来像这样:

HTTP_PROXY="http://proxy.domain.com:8080"
HTTPS_PROXY="http://proxy.domain.com:8080"
http_proxy="http://proxy.domain.com:8080"
https_proxy="http://proxy.domain.com:8080"

我该如何实现?当我使用$ {localenv:http_proxy}时,它将从我的系统中获取用户环境变量。我不想要它,因为它具有不同的值,所以我希望它从.env文件中读取变量。任何帮助都将受到赞赏。

Using VSCode devcontainer I want to dynamically set some build arguments for Docker in my devcontainer.json. Currently my devcontainer.json looks like this:

{
  "name": "NodeJS & TypeScript",
  "build": {
    "dockerfile": "Dockerfile",
    "args": {
      "VARIANT": "16-bullseye",
      "NPM_PROXY": "http://proxy.domain.com:8080",
      "HTTPS_PROXY": "http://proxy.domain.com:8080",
      "HTTP_PROXY": "http://proxy.domain.com:8080"
    }
  },
  "settings": {},
  "extensions": [
    // ...
  ],      
  "remoteUser": "node",
  "runArgs": ["--env-file", ".devcontainer/.env"]
}

With my Dockerfile:

ARG VARIANT
FROM mcr.microsoft.com/vscode/devcontainers/typescript-node:0-${VARIANT}

RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
  && apt-get -y install --no-install-recommends chromium

WORKDIR /usr/app

ARG NPM_PROXY
RUN npm config set https-proxy ${NPM_PROXY} -g

RUN npm i -g @angular/cli

ENV CHROME_BIN=/usr/bin/chromium

As you can see, the VARIANT and NPM_PROXY args are used in the Dockerfile, but I need the HTTP(S)_PROXY for the build arguments as well. I'd like these definitions in devcontainer.json to get them from a .env file which resides in the same folder as the devcontainer.json and Dockerfile.

The .env file looks like this:

HTTP_PROXY="http://proxy.domain.com:8080"
HTTPS_PROXY="http://proxy.domain.com:8080"
http_proxy="http://proxy.domain.com:8080"
https_proxy="http://proxy.domain.com:8080"

How can I achieve this? When I use ${localEnv:HTTP_PROXY}, it will take the user environmental variable from my system. I don't want that because it has a different value, I want it to read the variable from the .env file. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文