Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to allow user-specific environment variables in a container?#255

AnsweredbyRoemer
Roemer asked this question inQ&A
Discussion options

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.
What is the proposal to do something like this? The values cannot be added to devcontainer.json as they should not be commited to git. I tried using separate optional .env files but that does not work as well because docker and compose do not support optional env files unfortunately.

You must be logged in to vote
Answered by RoemerSep 4, 2023

I got a fairly simple solution for this problem:

  • I have aninitializeCommand which makes sure that the file.devcontainer/.env.run exists viatouch .devcontainer/.env.run
  • In therunArgs, 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 thedockerfile to theinitializeCommand (seemicrosoft/vscode-remote-release#3545 (comment) for details).

Then I can read the arguments fro…

Replies: 3 comments 1 reply

Comment options

If you need it at container runtime, you can create an.env file viainitializeCommand. Then you can have docker compose set that env file for the service.
Consider something like this:

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.

You must be logged in to vote
1 reply
@jan-hudec
Comment options

Theenv_file option only sets environment inside the container, butdocker-compose also reads.env file in the same directory as thedocker-compose.yaml and those variables can be substituted to any and all parameters in thedocker-compose.yaml file itself including build-time parameters. At work we use that to vary some volume names to match what our other tooling uses depending on the checkout location.

Comment options

You can use thecontainerEnv|remoteEnv andlocalEnv with templating to set environment variables in the container.

{"containerEnv": {"INTERNAL_VAR":"${localEnv:YOUR_EXTERNAL_VAR}","INTERNAL_VAR_DEFAULT":"${localEnv:YOUR_UNSET_VAR:default value}"}

The difference betweencontainerEnv andremoteEnv is the former bakes the variable into the container but the later sets the variable when the container starts so it's better for things that may change regularly

You must be logged in to vote
0 replies
Comment options

I got a fairly simple solution for this problem:

  • I have aninitializeCommand which makes sure that the file.devcontainer/.env.run exists viatouch .devcontainer/.env.run
  • In therunArgs, 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 thedockerfile to theinitializeCommand (seemicrosoft/vscode-remote-release#3545 (comment) for details).

Then I can read the arguments from the file and add them to thedocker build. Here is an example of a workingbuild.sh file that is used ininitializeCommand:

#!/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
You must be logged in to vote
0 replies
Answer selected byRoemer
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
4 participants
@Roemer@evilhamsterman@jan-hudec@liiight

[8]ページ先頭

©2009-2025 Movatter.jp