How Environment works in docker-compose

Hi, I am new to docker in general and I have a question regarding environment in docker-compose.

I have a mariadb image and a java image that has my application.

when setting the environment for mariadb I use

environment:
  - MARIADB_ROOT_PASSWORD=foo

and when setting the database connection for my spring app

environment:
   spring.datasource.url: "jdbc:mariadb://bar:3306"

My question is regarding the naming consistency. For mariadb I use ’ - ’ and ’ = ', and for spring I do not use ’ - ’ and ’ = '.

I tried setting the spring environment the same way I did in mariadb and it did not work

This should work:

   - "spring.datasource.url=jdbc:mariadb://bar:3306"

You can either use the map (key/value) syntax or the array syntax:
https://docs.docker.com/compose/compose-file/05-services/#environment

Personally I prefer the map syntax, as it allows merging of compose files. If you split your compose file in a common compose file and use additional compose files for environment specific overrides, maps will be merged, arrays won’t.

1 Like