- Notifications
You must be signed in to change notification settings - Fork405
-
I have the case that each of the developers has a different environment value, for example an api token that is bound to the developers account. |
BetaWas this translation helpful?Give feedback.
All reactions
I got a fairly simple solution for this problem:
- I have an
initializeCommandwhich makes sure that the file.devcontainer/.env.runexists viatouch .devcontainer/.env.run - In the
runArgs, I have--env-file,${localWorkspaceFolder}/.devcontainer/.env.run .devcontainer/.env.runis in.gitignore
This allows the file to be empty but whoever needs environment variables added, can add them there.
As a bonus, I also added the possibility to define a.env.build file which is used during the build phase.
When using this, I switched building the container from thedockerfile to theinitializeCommand (seemicrosoft/vscode-remote-release#3545 (comment) for details).
Then I can read the arguments fro…
Replies: 3 comments 1 reply
-
If you need it at container runtime, you can create an devcontainer.json {"initializeCommand":"echo\"USERNAME=$USER\nUSER_UID=$(id -u $USER)\nGROUPNAME=$(id -gn $USER)\nGROUP_GID=$(id -g $USER)\nIS_AMD=$(test $(uname -m) = amd64 && echo true || echo false)\" > .devcontainer/.env","dockerComposeFile":"../docker-compose.yml","service":"main"}docker-compose: services:main:image:my_image:latestenv_file: -.devcontainer/.env This would probably fail on the first run since the .env will be created in the same context as the docker-compose command, but after that it should work, I use a variation of this as well. Note that this will work ONLY for container environment variable and not environment variables you need to have present at build time. |
BetaWas this translation helpful?Give feedback.
All reactions
-
The |
BetaWas this translation helpful?Give feedback.
All reactions
-
You can use the {"containerEnv": {"INTERNAL_VAR":"${localEnv:YOUR_EXTERNAL_VAR}","INTERNAL_VAR_DEFAULT":"${localEnv:YOUR_UNSET_VAR:default value}"}The difference between |
BetaWas this translation helpful?Give feedback.
All reactions
-
I got a fairly simple solution for this problem:
This allows the file to be empty but whoever needs environment variables added, can add them there. As a bonus, I also added the possibility to define a Then I can read the arguments from the file and add them to the #!/bin/bash -x# Make sure the required env files existtouch ./.devcontainer/.env.buildtouch ./.devcontainer/.env.run# Read all lines from the file and add them as build arguments, ignore empty, starting with # or after #ARGUMENTS_FROM_FILE=""foriin`cat ./.devcontainer/.env.build| grep -vE"^(#.*|\s*)$"| sed's/#.*//g'`;do ARGUMENTS_FROM_FILE+="--build-arg$i"done# Run the builddocker build --tag=my-project-dev --add-host=host.docker.internal:host-gateway$ARGUMENTS_FROM_FILE .devcontainer |
BetaWas this translation helpful?Give feedback.