I´m using a docker-compose.yml at /var/sota/storage/docker-compose/ to start automatically (according to this link, and my docker-compose.yml needs an enviroment variable:
environment:
- HOST_ID=${HOST_ID}
How can I set the given variable before the docker-compose.yml is read? I´ve tried /etc/environment with no success.
Currently I´m getting an empty variable inside the container.
Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!
Did you like my answer?
Hello!
To ensure that the environment variable HOST_ID is correctly set before your docker-compose.yml file is read on Torizon OS, you should define the variable in a way that it is available to the Docker service at startup.
One effective method is to create a systemd service override for the Docker service to export the environment variable. Here’s how you can do it:
Create a systemd override file for Docker:
Run the following command to create an override file:
sudo systemctl edit docker.service
Add the environment variable to the override file:
In the editor that opens, add the following lines:
[Service]
Environment="HOST_ID=your_value_here"
Replace your_value_here with the actual value you want to set for HOST_ID.
Reload the systemd daemon and restart Docker:
After saving and closing the editor, run the following commands to apply the changes:
This method ensures that the HOST_ID environment variable is set in the environment of the Docker service, making it available to any containers started by docker-compose that reference this variable.
If you need the variable to be dynamic or set at runtime, consider using a script to export the variable before starting the Docker service, or modify the docker-compose.yml to use a .env file located in the same directory as the docker-compose.yml. This file should contain the line HOST_ID=your_value_here.
By following these steps, the HOST_ID variable should be correctly passed to your container when it starts.
Best regards,
Toradex AI Assistant.
Please, also check if these links can help you solve your problem:
Here you can find the explanation about environment variables in compose files. The variable value in the way you did will be the same as the environment in the shell in which the compose file is run.
The docker-compose.yaml file is started by a systemd service called docker-compose.service, which basically uses the command docker-compose up. Since it is a different environment from yours and no variable was set, this variable value will be empty.
You can find other ways to set environment variables in the link I sent before. For example, using a .env file.
Besides that, you can use a systemd service to start a container by running docker run, as explained in the article you sent.