I am using Visual Studio Code to run a docker container and passing my environment variables as a file.
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.
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}
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
Is ${VARIABLE_NAME}
not the correct syntax?
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 ...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With