dockerdocker-composemariadbenvironment-variablesgitlab-ci

Mixing ENV variables with and without assignments in docker compose yaml for Gitlab CI


In trying to set up a database container via compose, I ran into issues with credential management. I wanted to be able to use Gitlab secrets/variables to set the db password, etc. and stumpled upon ENV variables without assignments, ie.

gitlab-ci:

MARIADB_ROOT_PASSWORD=$MARIA_ROOT_PASS MARIADB_USER='root' docker compose up --build

with a compose file like this:

environment:
      MARIADB_ROOT_PASSWORD
      MARIADB_USER
      ...

This worked if the only ENV vars are those without assignment, but it apparently breaks as soon as you combine it with things like

MARIADB_ROOT_PASSWORD
MARIADB_USER
CUDA_DEVICE_ORDER: "PCI_BUS_ID"
CUDA_VISIBLE_DEVICES: ${CUDA_DEVICES:-'0, 1, 2, 3'}

Is there a way to have both types of ENVs in the same compose file, or do you recommend another way of encorporating CI vars into the build pipeline? Note that i'd like to avoid a .env file on the host server.


Solution

  • There are two syntaxes for environment:, and both support mixing values defined in the YAML file and values passed through from the host.

    environment: can be a YAML map, with a series of KEY: value. If you omit a value half then Compose will pass through a value from the host.

    environment:
      MARIADB_ROOT_PASSWORD:  # <-- note colon at the end, no value
      MARIADB_USER:
      CUDA_DEVICE_ORDER: "PCI_BUS_ID"
      CUDA_VISIBLE_DEVICES: ${CUDA_DEVICES:-0, 1, 2, 3}
    

    You can also set environment: to a YAML list (typically one value to a line with - at the start of the line) of KEY=value strings. In this syntax, just setting KEY without an =value half passes through the value.

    environment:
      - MARIADB_ROOT_PASSWORD  # <-- just the variable name
      - MARIADB_USER
      - CUDA_DEVICE_ORDER=PCI_BUS_ID  # quotes will not be removed mid-string
      - CUDA_VISIBLE_DEVICES=${CUDA_DEVICES:-0, 1, 2, 3}