Anthony Ma
Anthony Ma

Reputation: 686

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?

Upvotes: 2

Views: 1553

Answers (1)

larsks
larsks

Reputation: 311870

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

Upvotes: 2

Related Questions