Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set docker-compose.yaml environment variable from a shell output?

I would like to set an env to the CPU serial of a Raspberry Pi. With CLI it’s easy:

docker run -e DEVICE_ID=$( cat /proc/cpuinfo | grep Serial | cut -d ":" -f2 | xargs ) ...

How can I accomplish the same thing in a docker-compose.yaml file?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A docker-compose.yaml file doesn’t provide any mechanism for setting environment variables from the output of commands. However, it does allow you to substitute environment variables from your environment, so if you write your docker-compose.yaml like this:

version: "3"

services:
  myservice:
    environment:
      DEVICE_ID: $DEVICE_ID
    ...

Then you can start your stack like this:

DEVICE_ID=$(cat /proc/cpuinfo | grep Serial | cut -d ":" -f2 | xargs) docker-compose up
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading