Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference another environment variable inside an env file used by .devcontainer running inside a Visual Studio Code docker container?

Summary

I am using Visual Studio Code to run a docker container and passing my environment variables as a file.

Problem

I am trying to format a string dynamically from other environmental variables and having trouble resolving the string.

I am able to build the container and debug terminal tab shows no problems.

I am currently not using docker-compose.yml, but rather the Visual Studio .devcontainer settings.

Code

devcontainer.json

...

"runArgs": [
        "--env-file", "${localWorkspaceFolder}/.env.dev"
, 

...

.env.dev

DATABASE_DATABASE=database
DATABASE_USER=user
DATABASE_PASSWORD=password
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_DRIVER=postgresql
CONNECTION_STRING=${DATABASE_DRIVER}://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_DATABASE}

Output

When I type env into bash it displays:

DATABASE_DATABASE=database
DATABASE_USER=user
DATABASE_PASSWORD=password
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_DRIVER=postgresql
CONNECTION_STRING=${DATABASE_DRIVER}://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_DATABASE}

Where I would expect it to display:

DATABASE_DATABASE=database
DATABASE_USER=user
DATABASE_PASSWORD=password
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_DRIVER=postgresql
CONNECTION_STRING=postgresql://user:password@$localhost:5432/database

Question

Is ${VARIABLE_NAME} not the correct syntax?

like image 294
fnet Avatar asked Dec 16 '19 17:12

fnet


People also ask

How do I create an .ENV file in Visual Studio code?

Once you have opened the folder, click on the Explorer icon on the top left corner of the VSCode (or press Ctrl+Shift+E) to open the explorer panel. In the explorer panel, click on the New File button as shown in the following screenshot: Then simply type in the new file name . env ...


1 Answers

Setting env variables from files works differently than setting them in a terminal.

Each line is just a string that gets split on "=" with the right hand side of that split being assigned as the value, and the left hand side being assigned as the key.

If you look at the source code for the run command:

https://github.com/docker/cli/blob/master/cli/command/container/run.go

you'll see that environment variables aren't set set through bash commands. It's just a series of string assignments.

This is pretty much always the case with env files.They're strings, and are set as such. The syntax you're trying to use only works on platforms that make an effort to run env files as a series of bash commands, but docker doesn't do that. This has nothing do with with VS code, it's just how env files ususally work.

One potential workaround would be to move this assignment from your env file to your Dockerfile or docker-compose. Those instructions are run as bash commands, so your variable call will work.

Examples

Dockerfile

ENV FIRST=ONE
ENV SECOND=${FIRST}

Here, inside your container echo ${SECOND} will print out ONE.

docker-compose

Docker compose environment variables work a bit differently. It doesn't set the variables in your compose file sequentially, so referencing a variable that you set in the same service definition doesn't work. However, If your variables are set in a .env file in the same directory as your compose file, then you can access those values by doing:

services:
  myapp:
    environment:
      - VAR_IN_MY_CONTAINER=${VAR_ON_MY_HOST}

This behavior happens because compose considers any .env file in its directory to be part of the host environment.

like image 103
Charles Desbiens Avatar answered Sep 30 '22 13:09

Charles Desbiens