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

Time:08-09

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?

CodePudding user response:

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
  • Related