How to use secrets as interpolation variables in Docker-Compose?

471 Views Asked by At

This is part of my docker-compose.yaml:

  gql:
    build: 
      context: ./graphql
    container_name: cyp-gql
    restart: unless-stopped
    env_file: 
      - ./.env
    depends_on: 
      - db
    ports: 
      - 5000:5000
    command: [
      "--owner-connection", "postgres://${NAME_ADMIN}:${PWD_ADMIN}@172.27.0.2:5432/${DB_NAME}", 
      .....

.env file contains all variables and this service is running OK.

I wanna use secrets for this service as interpolation variables, like this:

  gql:
    build: 
      context: ./graphql
    container_name: cyp-gql
    restart: unless-stopped
    secrets:
      - db_pwd
      - db_dbname
    env_file: 
      - ./.env
    environment:
      - DB_NAME=/run/secrets/db_dbname
      - PWD_ADMIN=/run/secrets/db_pwd
    depends_on: 
      - db
    ports: 
      - 5000:5000
    command: [
      "--owner-connection", "postgres://${NAME_ADMIN}:${PWD_ADMIN}@172.27.0.2:5432/${DB_NAME}", 
      .....

Where .env file contains NAME_ADMIN variable only.

But I'm getting these warning messages:

WARN[0000] The "DB_NAME" variable is not set. Defaulting to a blank string. 
WARN[0000] The "PWD_ADMIN" variable is not set. Defaulting to a blank string. 

How to resolve this problem?

0

There are 0 best solutions below