How can I get the environment variables from docker compose?
services:
service:
environment:
SOME_USER: ${USERNAME}
I'm currently using the environment variables from my .env
file but I want to switch to the docker compose variables.
Environment variables are environment variables, it doesn't matter how they were set. Are you using a library to set environment variables from the .env file, perhaps?
There are multiple ways to get the environment variables, depending on your use case.
The simplest approach is to use the env
module from the standard library. Here's an example for getting a single environment variable:
use std::env;
let key = "MY_ENV_VARIABLE";
match env::var(key) {
Ok(val) => println!("{key}: {val:?}"),
Err(e) => println!("couldn't interpret {key}: {e}"),
}
If you have multiple environment variables, you can create an iterator and iterate through them like this:
use std::env;
for (key, value) in env::vars() {
println!("{key}: {value}");
}
If your use case is more specific, I would recommend to use a specialized library instead.
2 Likes
system
Closed
June 9, 2024, 9:01am
5
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.