I've set up a devcontainer in a Visual Studio Code Ruby project, and the launch configurations don't seem to be able to override environment variables from the devcontainer. I set up the devcontainer to launch from Docker Compose, with one environment variable set in the .env file.
EXAMPLE_VALUE=from-env-file
Another environment variable is set in compose.yml.
services:
active_sinatra:
restart: unless-stopped
image: donkirkby/active-sinatra-devcontainer:v0.2.2
environment:
- EXAMPLE_VALUE
- EXAMPLE_VALUE2=from-compose-file
volumes:
- .:/opt/active_sinatra
I wrote example_script.rb to display the environment variables, plus a third that isn't set yet.
value = ENV.fetch('EXAMPLE_VALUE', 'not set')
puts "example value is #{value.inspect}."
value2 = ENV.fetch('EXAMPLE_VALUE2', 'not set')
puts "example value 2 is #{value2.inspect}."
value3 = ENV.fetch('EXAMPLE_VALUE3', 'not set')
puts "example value 3 is #{value3.inspect}."
I set up a launch configuration that overrides all three environment variables.
{
"type": "ruby_lsp",
"name": "Debug example",
"request": "launch",
"program": "ruby /opt/active_sinatra/example_script.rb",
"env": {
"EXAMPLE_VALUE": "1 from launch",
"EXAMPLE_VALUE2": "2 from launch",
"EXAMPLE_VALUE3": "3 from launch"
}
},
Unfortunately, my Ruby script only sees the third environment variable when I debug it with the launch configuration.
Ruby REPL: You can run any Ruby expression here.
Note that output to the STDOUT/ERR printed on the TERMINAL.
[experimental]
`,COMMAND` runs `COMMAND` debug command (ex: `,info`).
`,help` to list all debug commands.
DEBUGGER: Disconnected.
example value is "from-env-file".
example value 2 is "from-compose-file".
example value 3 is "3 from launch".
Is there a way to change one of the existing environment variables using the launch configuration, or do I have to change the Docker Compose configuration, relaunch the container, and then launch my debugger?
If you want to play around with my example code, you can see it on GitHub, then open a codespace on the docker-compose branch.