Skip to main content

Environment variables in devcontainer.json

If you're combining SSH provider with using environment variables in your .devcontainer.json, please follow the steps below to make sure that your environment variables are properly set on the remote machine.

Steps​

  1. Prepare a .devcontainer.json to include ${localEnv:<VARIABLE_NAME>} directive.
    Example .devcontainer.json:
{
"name": "Node.js",
"image": "mcr.microsoft.com/devcontainers/javascript-node:${localEnv:IMAGE_VERSION}"
}
  1. Prepare an entry in a local .ssh/config to include SetEnv <VARIABLE_NAME>=<VARIABLE_VALUE> directive.
    Example .ssh/config:
Host <REMOTE-SSH-SERVER>
SetEnv IMAGE_VERSION=0-18-bullseye
  1. Log to your remote machine and in the /etc/ssh/sshd_config add an entry AcceptEnv <VARIABLE_NAME>.
    For example:
AcceptEnv IMAGE_VERSION
  1. Restart SSH service on your remote machine.
    For example on Debian Linux:
systemctl restart ssh.service
  1. Run a devpod up command:
devpod up <GITHUB-REPOSITORY-URL> --ide=none

Without changing the ssh configuration​

${localEnv:...} reads the environment of the machine DevPod runs on, so with providers that run DevPod on your computer, such as docker and kubernetes, it needs none of the steps above:

IMAGE_VERSION=0-18-bullseye devpod up <GITHUB-REPOSITORY-URL> --ide none

To give the processes in the workspace variables that devcontainer.json doesn't mention, pass them to devpod up. They are saved with the workspace, so you don't need to pass them on every devpod up:

devpod up my-workspace --workspace-env API_URL=https://api.example.com --workspace-env LOG_LEVEL=debug
devpod up my-workspace --workspace-env-file ./workspace.env

where workspace.env has one NAME=VALUE per line. For one devpod ssh session only, use --set-env, or --send-env to send a variable of your computer:

devpod ssh my-workspace --set-env LOG_LEVEL=trace
devpod ssh my-workspace --send-env AWS_PROFILE

Variables that don't need to come from outside belong in devcontainer.json:

{
"containerEnv": { "LOG_LEVEL": "debug" },
"remoteEnv": { "API_TOKEN": "${localEnv:API_TOKEN}" }
}