Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.env variable in the mounts section of devcontainer.json

setting up a .devcontainer configuration ...

I have a mount that normally lives in one spot, but when working remotely, can live in a different path. I want to set up a devcontainer so that I can change an environment variable locally, without making a commit change to the repos. I can't change the target because it is a convention that spans many tools and systems.

"mounts": [
  "type=bind,source=${localEnv:MY_DIR},target=/var/local/my_dir,readonly"
]

For example, in a .env file in the project root:

MY_DIR=~/workspace/my_local_dir

When I try this, no matter what combinations, this environment variable always ends up blank, and then Docker is sad when it gets a --mount configuration that is missing a source:

[2022-02-12T19:11:17.633Z] Start: Run: docker run --sig-proxy=false -a STDOUT -a STDERR --mount type=bind,source=,target=/var/local/my_dir,readonly --entrypoint /bin/sh vsc-sdmx-9c04deed4ad9f1e53addf97c69727933 -c echo Container started
[2022-02-12T19:11:17.808Z] docker: Error response from daemon: invalid mount config for type "bind": field Source must not be empty.
See 'docker run --help'.

The reference shows that this type of syntax is allowed for a mount, and that a .env will be picked up by VSCode. But I guess it only does that for the running container and not VSC itself.

How do I set up a developer-specific configuration change?

like image 209
Ken Mayer Avatar asked Oct 25 '25 15:10

Ken Mayer


1 Answers

The error message

invalid mount config for type "bind": field Source must not be empty

indicates that your source config appears empty to docker, which means that ${localEnv:MY_DIR} resolves to an empty string, which in turn means that MY_DIR isn't set on your host, because "Unset variables are left blank.".

If you set MY_DIR on your host then its path should be mounted into the devcontainer with the mount command you've used:

"mounts": [
  "type=bind,source=${localEnv:MY_DIR},target=/var/local/my_dir,readonly"
]

Defining a variable in an .env file in the project root, however, means that this variable gets set as an environment variable in the devcontainer, not locally.

like image 109
dpprdan Avatar answered Oct 28 '25 04:10

dpprdan