Read build args from env file i.e. private npm registry
❓ It would be nice if VScode devcontainer.json supports reading build args from a file. So we won't need this workaround:
I have found a workaround to set ENV vars from a file during the docker BUILD. This is i.e. necessary if you want to use a private NPM registry during build, but you don't want to write the secrets in devcontainer.json or Dockerfile, as these files are committed to the repo.
.devcontainer/devcontainer.env ( add this file to .gitignore )
export NPM_USER=***
export NPM_PASS=***
export NPM_EMAIL=***
In the Dockerfile:
# Install npm-cli-adduser which can perform a cli npm login by reading env vars
RUN npm i -g --production --registry https://registry.npmjs.org/ npm-cli-adduser
# Set env variables from devcontainer.env. Needs to be in same RUN step as env vars will be lost in next RUN
COPY devcontainer.env devcontainer.env
RUN $(cat devcontainer.env) && npm-cli-adduser -r https://private.registry.url \
&& npm whoami
# install private registry package
RUN npm i -g -r https://private.registry.url private-registry-package
EDIT:
Another option to solve this would be to add a prebuild: property / script to devcontainer.json analog to postbuild:.
This could be even more flexible as users can copy files to the context, read envs via shell scripts and many more operations that are usually possible by calling docker build manually.
Another option would be to pass the values as local variables with the devcontainer.json:
{
"build": {
"dockerfile": "Dockerfile",
"args": {
"NPM_USER": "${localEnv:NPM_USER}"
}
}
}
This feature request is now a candidate for our backlog. The community has 60 days to upvote the issue. If it receives 10 upvotes we will move it to our backlog. If not, we will close it. To learn more about how we handle feature requests, please see our documentation.
Happy Coding!
Another option would be to pass the values as local variables with the devcontainer.json: "NPM_USER": "${localEnv:NPM_USER}"
This only seems to work when launching VScode via commandline, not via app icon.
Also we would prefer to not pollute the OS env with project specific NPM credentials
Another option to solve this would be to add a prebuild: script to devcontainer.json
This would probably be more flexible as we can copy files to the context, read envs via shell scripts etc.
:slightly_smiling_face: This feature request received a sufficient number of community upvotes and we moved it to our backlog. To learn more about how we handle feature requests, please see our documentation.
Happy Coding!
Currently waiting on this feature, any updates?
+1 would be very handy.
Another option would be to pass the values as local variables with the devcontainer.json:
{ "build": { "dockerfile": "Dockerfile", "args": { "NPM_USER": "${localEnv:NPM_USER}" } } }
This can work in some cases. It would pollute the dev machine with a user-global env var that exists all the time while it is only required when someone works on this specific project.
But also: we might be working in multiple devcontainers at the same time that all refer to the same env var name, simply because it is a meaningful quality name, but in each instance the env var needs to have a different value.