# How should the docker compose variables work?

**URL:** <https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628>\
**Category:** Product support\
**Created:** [September 17, 2019, 5:00pm UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628 "2019-09-17T17:00:51Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![krasi-georgiev](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@krasi-georgiev](https://forums.balena.io/u/krasi-georgiev)\
**Post date:** [September 17, 2019, 5:00pm UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/1 "2019-09-17T17:00:51Z")

</div>

It seems that the env substitution doesn’t work as expected using local mode.

```auto
version: "2.1"

services:
  env-config:
    image: arribada/env-config
    environment:
      CONFIG: "${PROMETHEUS_CONFIG}"

```

I tried few different variation, but {PROMETHEUS\_CONFIG} never gets substituted and inside the container it is just as CONFIG="{PROMETHEUS\_CONFIG}".

I don’t want to put the text directly as this is checked out on github so it will expose secrets and configs.

The second question is for when I switch to non local mode in the balena portal should I set PROMETHEUS\_CONFIG or the CONFIG env variables to get these substituted?

---

<div class="post-metadata">

**Author:** ![wrboyce](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/wrboyce/32/374_2.png) [@wrboyce](https://forums.balena.io/u/wrboyce)\
**Post date:** [September 17, 2019, 7:45pm UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/2 "2019-09-17T19:45:43Z")

</div>

To pass a `CONFIG` var to your devices you can just set that variable in the dashboard, and when operating in local mode you can pass a `--env CONFIG="..."` argument to `balena push`. There is no need for the `config:` line in your compose file.

---

<div class="post-metadata">

**Author:** ![krasi-georgiev](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@krasi-georgiev](https://forums.balena.io/u/krasi-georgiev)\
**Post date:** [September 18, 2019, 8:55pm UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/4 "2019-09-18T20:55:57Z")

</div>

Thanks,  
Is there a workaround if 2 services use the same config name but expect different content?

for example  
service1 expects $CONFIG=“param=1”  
service2 expects $CONFIG=“target=localhost”

the question is mainly for the portal

---

<div class="post-metadata">

**Author:** ![krasi-georgiev](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@krasi-georgiev](https://forums.balena.io/u/krasi-georgiev)\
**Post date:** [September 18, 2019, 9:12pm UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/5 "2019-09-18T21:12:11Z")

</div>

btw I just tried the suggestion with a multiline env variable and it couldn’t parse it:

```auto
export PROMETHEUS_CONFIG="
global:
  scrape_interval: 5s 
scrape_configs:
  - job_name: 'lora-connect'
    static_configs:
    - targets: ['192.168.0.113:8070']
alerting:
  alertmanagers:
  - static_configs:
    - targets:
        - localhost:9093
rule_files:
    - alert_rules
"

```

```auto
balena push 192.168.0.113 --env PROMETHEUS_CONFIG="$PROMETHEUS_CONFIG"

```

the error is :

```auto
Unable to parse environment variable: PROMETHEUS_CONFIG=".......

```

---

<div class="post-metadata">

**Author:** ![imrehg](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/imrehg/32/36_2.png) [@imrehg](https://forums.balena.io/u/imrehg)\
**Post date:** [September 18, 2019, 11:45pm UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/6 "2019-09-18T23:45:08Z")

</div>

How about using multiline env vars in the `docker-compose.yml`? I’ve tried it like this, and could successfully deploy, and seems it’s a multiline env var on the other side.

```auto
...
    environment:
      - |
        PROMETHEUS_CONFIG=
        global:
          scrape_interval: 5s
        scrape_configs:
          - job_name: 'lora-connect'
            static_configs:
            - targets: ['192.168.0.113:8070']
        alerting:
          alertmanagers:
          - static_configs:
            - targets:
                - localhost:9093
        rule_files:
            - alert_rules

```

then in the container I can go:

```auto
root@67a57cfa4657:/usr/src/app# printf %s "$PROMETHEUS_CONFIG"
global:
  scrape_interval: 5s
scrape_configs:
  - job_name: 'lora-connect'
    static_configs:
    - targets: ['192.168.0.113:8070']
alerting:
  alertmanagers:
  - static_configs:
    - targets:
        - localhost:9093
rule_files:
    - alert_rules

```

---

<div class="post-metadata">

**Author:** ![krasi-georgiev](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@krasi-georgiev](https://forums.balena.io/u/krasi-georgiev)\
**Post date:** [September 19, 2019, 12:21am UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/9 "2019-09-19T00:21:36Z")

</div>

Yes that might be even better with one problem some devices deployed in the same application need a different config.  
If I set the same env var in the GUI will that override the one set in the compose file?

I can’t test it now as the `balena cli` doesn’t work in production mode with pre built multi arch images, that is why I am using local mode for now.

Also the compose file is checked in on github so need to look through all configs to make sure not to expose passwords and sensitive information.

---

<div class="post-metadata">

**Author:** ![afitzek](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/afitzek/32/359_2.png) [@afitzek](https://forums.balena.io/u/afitzek)\
**Post date:** [September 19, 2019, 7:16am UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/10 "2019-09-19T07:16:01Z")

</div>

Hi,  
Yes the env vars that are set in the dashboard are considered runtime level and therefore overwrite values defined in the compose file.

---

<div class="post-metadata">

**Author:** ![krasi-georgiev](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@krasi-georgiev](https://forums.balena.io/u/krasi-georgiev)\
**Post date:** [September 21, 2019, 4:26pm UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/12 "2019-09-21T16:26:42Z")

</div>

Thanks, the only other problem that remains is that some of these config env vars contain passwords so adding those directly in the compose file and checking it on github will expose those on the internet.

---

<div class="post-metadata">

**Author:** ![krasi-georgiev](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@krasi-georgiev](https://forums.balena.io/u/krasi-georgiev)\
**Post date:** [September 22, 2019, 8:04am UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/13 "2019-09-22T08:04:45Z")

</div>

opened an issue for the multi line support

> <https://github.com/balena-io/balena-cli/issues/1442>

---

<div class="post-metadata">

**Author:** ![telphan](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@telphan](https://forums.balena.io/u/telphan)\
**Post date:** [September 23, 2019, 8:15am UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/14 "2019-09-23T08:15:13Z")

</div>

I think what my colleague is saying is to use env vars for the passwords and pass the values at runtime, form the dashboard. This way they do not have to be committed to github.

---

<div class="post-metadata">

**Author:** ![krasi-georgiev](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@krasi-georgiev](https://forums.balena.io/u/krasi-georgiev)\
**Post date:** [September 23, 2019, 10:47am UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/16 "2019-09-23T10:47:59Z")

</div>

some passwords are inside the config files.

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/janvda/32/1271_2.png) [@janvda](https://forums.balena.io/u/janvda)\
**Post date:** [September 23, 2019, 11:32am UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/18 "2019-09-23T11:32:57Z")

</div>

so far I have always set passwords and API keys by means of device service variables in the balenaCloud dashboard.

If passwords/API keys should be set in config file(s) then in start script of your docker service you must first update the config file(s) based on the passwords/API keys set by the device service variables before launching the application/service that is reading the configuration from these config file(s).

---

<div class="post-metadata">

**Author:** ![krasi-georgiev](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@krasi-georgiev](https://forums.balena.io/u/krasi-georgiev)\
**Post date:** [September 23, 2019, 11:58am UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/20 "2019-09-23T11:58:08Z")

</div>

@janvda thanks for the suggestion. Sounde like the movie inception 🙂 replacing env variable inside an env variable. I will try it and who knows it might work out well.

All that said I still think the `balena push --env=..` should support multiline env variables.

just for reference the config file that includes password is the routing\_key in the Alert Manager  
[https://prometheus.io/docs/alerting/configuration/#pagerduty\_config](https://prometheus.io/docs/alerting/configuration/#pagerduty_config)

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/janvda/32/1271_2.png) [@janvda](https://forums.balena.io/u/janvda)\
**Post date:** [September 23, 2019, 12:43pm UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/22 "2019-09-23T12:43:28Z")

</div>

apparently prometheus doesn’t support environment variables in their configuration files ([https://github.com/prometheus/prometheus/issues/2357](https://github.com/prometheus/prometheus/issues/2357)).

If there is no other way than using a configuration file for this then in your docker service startup script you must do a substitute of the FAKE routing\_key you have specified in the configuration file by the actual routing\_key that is stored in the environment variable.

Not clear to me how multiline env variables might also solve this issue.

---

<div class="post-metadata">

**Author:** ![krasi-georgiev](https://avatars.discourse-cdn.com/v4/letter/k/ccd318/32.png) [@krasi-georgiev](https://forums.balena.io/u/krasi-georgiev)\
**Post date:** [September 23, 2019, 1:23pm UTC](https://forums.balena.io/t/how-should-the-docker-compose-variables-work/32628/23 "2019-09-23T13:23:08Z")

</div>

> [@janvda](#):
>
> Not clear to me how multiline env variables might also solve this issue.

I have developped a [simple tool that converts an env variable into a file](https://github.com/arribada/EnvToFile) which is than used in the AlertManager container. Allowing multiline env variables would make it possible to NOT add the env variable directly in the compose file and pass it vie the cli

```auto
balena push --env ALERTMANAGER_CONFIG="$ALERTMANAGER_CONFIG" 192.168.0.113

```

or set it via the balena portal GUI.  
I currently can’t use the gui due to this bug:

> [@Use pre build multiarch image from docker hub](https://forums.balena.io/t/use-pre-build-multiarch-image-from-docker-hub/26110):
>
> Trying to use my own multi-arch image that I have built with the new docker buildx command and pushed to docker hub. From the docs so far I understand that I need to use belena push, but it seems that doesn’t use the existing image, but still somehow builds a new image as when running the logs show standard\_init\_linux.go:207: exec user process caused "exec format error". I can run this image fine on another Rpi with Hypriot OS docker-compose file version: "2.1" services: lora-connect: …

P.S I have now added suport to the envToFile tool to expand the env vars so will test shortly and will let you know if it worked.
