Using environment variables in docker-compose.yml file

  docker, docker-compose, gitlab, gitlab-ci

I am trying to configure GitLab CI to continuously build and deploy my app with Docker and Docker Compose.

When running the CI pipeline, I get the following error messages:

The Compose file './docker-compose.ci.yml' is invalid because:
services.dashboard.ports contains an invalid type, it should be a number, or an object
services.mosquitto.ports contains an invalid type, it should be a number, or an object
services.mosquitto.ports contains an invalid type, it should be a number, or an object
services.mosquitto.ports contains an invalid type, it should be a number, or an object
services.mosquitto.ports value [':', ':', ':'] has non-unique elements

I would like to use environment variables to keep my configuration hidden.

Following is a snippet of my docker-compose.ci.yml:

version: "3.9"

services:
  dashboard:
    build:
      context: ./dashboard
      dockerfile: Dockerfile.prod
      cache_from:
        - "${BACKEND_IMAGE}"
    image: "${BACKEND_IMAGE}"
    command: gunicorn dashboard.wsgi:application --bind ${DJANGO_HOST}:${DJANGO_PORT}
    volumes:
      - static_volume:/home/app/web/static
    ports:
      - "${DJANGO_PORT}:${DJANGO_PORT}"
    env_file:
      - .env
    depends_on:
      - postgres

...

  mosquitto:
    build: 
      context: ./mosquitto
      cache_from:
        - "${MOSQUITTO_IMAGE}"
    image: "${MOSQUITTO_IMAGE}"
    volumes:
      - ./mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - ./mosquitto/data:/mosquitto/data
      - ./mosquitto/log:/mosquitto/log
      - broker_certs:/mosquitto/config/certs
    ports:
      - "${MQTT_DEFAULT_PORT}:${MQTT_DEFAULT_PORT}"
      - "${MQTT_SECURE_PORT}:${MQTT_SECURE_PORT}"
      - "${MQTT_WEBSOCKETS_PORT}:${MQTT_WEBSOCKETS_PORT}"
    env_file:
      - .env
...

In my build stage, I set up the environment variables using a bash script:

.gitlab-ci.yml:

image:
  name: docker/compose:1.28.5
  entrypoint: [""]

services:
  - docker:dind

stages:
  - build

variables:
  DOCKER_HOST: tcp://docker:2375
  DOCKER_DRIVER: overlay2

build:
  stage: build
  before_script:
    ...
    - chmod +x ./setup_env.sh
    - bash ./setup_env.sh
    ...
...

setup_env.sh:

...

# mosquitto config
echo MQTT_DEFAULT_PORT=$MQTT_DEFAULT_PORT >> .env
echo MQTT_SECURE_PORT=$MQTT_SECURE_PORT >> .env
echo MQTT_WEBSOCKETS_PORT=$MQTT_WEBSOCKETS_PORT >> .env
echo MOSQUITTO_COMMON_NAME=$MOSQUITTO_COMMON_NAME >> .env

...

All my variables are well set up on Gitlab.

Running this with docker-compose locally on my machine doesn’t generate any errors.

What am I doing wrong?

Source: Docker Questions

LEAVE A COMMENT