problem loading in ${localEnv:TOKEN} into devcontainer.json

Viewed 1270

In my devcontainer.json for vscode, I am trying to load in a build variable. This variable is on my local machine's environment, my code looks like the following:

//build arguments
 "build": { 
    "args": { 
        "TOKEN": "${localEnv:TOKEN}"
    } 
}

It seems like it works when I put in a direct string, or something like "${localEnv:HOME}", but it is not picking up this custom one. which is strange because I can do 'printenv TOKEN' and it prints out correctly.

any ideas on what I may be doing wrong?

2 Answers

Add your export BLA=1 to .profile, this was the only way VScode was able to pass through env variables to the devcontainer.

.devcontainer:

{
    "name": "devcontainer",
    "build": {
        "dockerfile": "${localWorkspaceFolder}/Dockerfile",
        "context": "${localWorkspaceFolder}",
    },
    "remoteEnv": {
        "FOO": "${localEnv:FOO}",
        "BAR": "${localEnv:BAR}",
    }
}

First, ensure that you have the VS Code Terminal -> Integrated: Inherit Env setting set to true. This is described on the Advanced Container Configuration page:

Adding env variable to dev container

Workarounds

If that doesn't fix your problem (it didn't for me), here are some of the workarounds that I have found:

  1. Set the variables in your ~/.bashrc file (or export them temporarily in the terminal) and start VS Code from a bash prompt (the executable is code).
$ export TOKEN=tokenvalue
$ code
  1. Set the variables in your ~/.pam_environment file (these are available session wide and are inherited by applications started with the launcher). You will need to logout and login or reboot for these to apply.
TOKEN=tokenvalue
  1. Set the environment variables in one of your VS Code settings files (user or workspace) using the Terminal -> Integrated Env: Linux setting:
// Object with environment variables that will be added to the VS Code process to be used by the terminal on Linux. Set to `null` to delete the environment variable.
"terminal.integrated.env.linux": {
    "TOKEN": "tokenvalue"
},
Related