Search code examples
postgresqldockerdocker-composeconfigstring-interpolation

Docker-Compose + Environment variables interpolation problem


i have a problem. I have two files. First one is config.dev.yaml:

app:
    port: 8080
    host: localhost
    read_timeout:   20
    read_header_timeout: 5
    write_timeout: 30
    idle_timeout: 120

certificate:
    cert: "certificate/cert.pem"
    key: "certificate/key.pem"

postgres:
    host: "localhost"
    port: 5432
    username: "sadasdaw"
    password: "sup3erRestrictedSafe" 
    name: "myDB"
    p8s_path_src: "/app/postgresql"
    p8s_path_dest: "/var/lib/postgresql/data"

As you can see, i have nested variables in here. Another one is my docker-compose.yaml file:

version: "3"

services:
  myService:
    build: .
    ports:
      - "${app.port}:${app.port}"
    depends_on:
      - postgresql

  postgresql:
    restart: unless-stopped
    image: postgres:15.3
    environment:
      - POSTGRES_USERNAME=${postgres.username}
      - POSTGRES_PASSWORD=${postgres.password}
      - POSTGRES_DB=${postgres.name}
      - POSTGRES_DATA=${postgres.p8s_path_dest}
    ports:
      - "${postgres.port}:${postgres.port}"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${postgres.username} -d $${postgres.name}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    volumes:
      - "${postgres.p8s_path_src}:${postgres.p8s_path_dest}"
      - ./internal/sql:/docker-entrypoint-initdb.d

The problem is i cannot read variables from my config.dev.yaml using dot notation I run this command: docker-compose --env-file ./config.dev.yaml -p my_service up and i get an error message:

invalid interpolation format for services.myService.ports.[].
You may need to escape any $ with another $.
${app.port}:${app.port}

I've tried to google, asked chatGpt, tried to escape with 2 dollar signs but it just didn't help, Is there a way to use config.yaml file with nested variables and pass it to docker-compose?

I expected docker-compose up command to work with configuration file, but it cannot parse nested variables. I did the same stuff with config.env file before and it worked but can i use .yaml file for config


Solution

  • By default you cannot parse yaml file as env_file in your docker-compose configuration as it accepts key value pairs (just like we have in a .env file). To achieve what you are trying to do, you need to use a script or tool (like jq or yq) to extract the values from the YAML file and export them as environment variables which you can pass as env_file while running docker-compose up. Here you can find some elegant ways on how to parse the YAML file as environment variables.