如何使用Docker/Devcontainer buildargs中的文件中的环境变量?
使用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
如您所见,variant
和npm_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论