Spelling of environmental variables

Hi all,

i’m quite new to docker and compose and still trying to set up my homeserver based on several docker images.
So far this works very good and is suprisingly easy.

But there’s one thing, im not sure about, how to handle it.
When passing enviromental variables i’ve encountered two different ways of spelling.

environment:
  -MY_ENV=value
  -MY_2ND_ENV=value

environment:
  my_env: value
  my_2nd_env: 'value'

So far i’ve stuck to the first method, but when setting up ghost, only the second worked for me.

What’s the difference between those two and when to use which one?

Thanks in advance!

Philipp

Of course the environment variables are case sensitive. Check the description on Docker Hub. It should mention all variables and how they need to be used. If the description is missing, i would strongy advice to stay away from the image. At this point your should think about switching to an alternative image for the same service.

Worst case: check the entrypoint script declared in the image to see, which variables are actualy used.

Hey,
thanks for your reply, but i think, you got me wrong.
Sure, environment variables are case sensitive. My question war more related to the type of declaration.
Is that leading “-” needed?
Whats the difference between “=” und “:”?

Best regards!

I realy got you wrong, sorry for that :blush:

The 1st version is a yaml list:

environment:
- item1=value1
- item2=value2

It allows an array of scalar values. Each item is marked by a leading dash and followed by a string. Docker-Compose known how to parse the list and that the left hand side of the equal sign is the variable name and the right hand side its value.

The 2nd version is a yaml key/value structure:

environment:
  key1: value1
  key2: value2

This is typical key/value map, where a key can have a scalar value or one or more keys. In general these values can be again one or more keys and their values - though, not for environment entries.

As of now, both work fine. Though, the key/value structure was introduced later and is most likely to stick. I would use the 2nd variation.

Both versions are plain yaml, it is docker-compose that makes sense of both versions and how to translate them to environment variables.

1 Like

Thank you very mich for your explanations! :slight_smile: