-
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. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
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:latest
env_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. |
Beta Was this translation helpful? Give feedback.
-
You can use the {
"containerEnv": {
"INTERNAL_VAR": "${localEnv:YOUR_EXTERNAL_VAR}",
"INTERNAL_VAR_DEFAULT": "${localEnv:YOUR_UNSET_VAR:default value}"
} The difference between |
Beta Was this translation helpful? Give feedback.
-
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 exist
touch ./.devcontainer/.env.build
touch ./.devcontainer/.env.run
# Read all lines from the file and add them as build arguments, ignore empty, starting with # or after #
ARGUMENTS_FROM_FILE=""
for i in `cat ./.devcontainer/.env.build | grep -vE "^(#.*|\s*)$" | sed 's/#.*//g'`; do
ARGUMENTS_FROM_FILE+="--build-arg $i "
done
# Run the build
docker build --tag=my-project-dev --add-host=host.docker.internal:host-gateway $ARGUMENTS_FROM_FILE .devcontainer |
Beta Was this translation helpful? Give feedback.
I got a fairly simple solution for this problem:
initializeCommand
which makes sure that the file.devcontainer/.env.run
exists viatouch .devcontainer/.env.run
runArgs
, I have--env-file
,${localWorkspaceFolder}/.devcontainer/.env.run
.devcontainer/.env.run
is 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 the
dockerfile
to theinitializeCommand
(see microsoft/vscode-remote-release#3545 (comment) for details).Then I can read the arguments fro…