Docker compose environment variables

I’m trying to containerize a demo nodejs + reactjs + postgresql application and the directory structure looks something like this:

demo:
  -client
    -.env
    - Dockerfile.client
    - package.json
  -server
    - .env
    - Dockerfile.server
    - package.json
  .env
  docker-compose.yml

docker-compose.yml

version: '3.8'

services:
  client:
    container_name: ${APP_NAME}_fe
    build:
      dockerfile: ./client/Dockerfile.client
    environment:
      CHOKIDAR_USEPOLLING: "true"
    image: ${APP_NAME}_img_fe
    volumes:
      - ./client:/app
      - /app/node_modules
    ports:
      - 3000:3000

  server:
    container_name: ${APP_NAME}_be
    build:
      # context: .
      dockerfile: ./server/Dockerfile.server
    image: ${APP_NAME}_img_be
    volumes:
      - ./server:/app
      - /app/node_modules
    ports:
      - 5000:5000

  db:
    container_name: ${APP_NAME}_db
    image: postgres
    build:
      # context: .
      dockerfile: ./server/Dockerfile.db
    env_file: ./server/.env
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_DB: ${APP_NAME}
    volumes:
    - db-data:/var/lib/postgresql/data
    ports:
      - 5432:5432
      
volumes:
  db-data:
    name: ${APP_NAME}_db

I’m trying to keep all the sensitive data(like passwords and keys), api urls or ports inside .env files so i have all the infos in just one place, something like this:

.env from server

PORT=5000
DB_PASSWORD=postgres
DB_USER=postgres
DB_NAME=demo

.env on root level

APP_NAME=demo

The problem is for the db service (postgres container) the credentials are not visible at the creation time, so i need to hardcode them like:

environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: demo

Is there a way to fetch these values from inside the .env files ?

LE: I think one way is to keep only one .env at the root of the application with:
-db password
-db user
-port(both server and client)
-api url

In this case i need to acces .env from client and server folder.

Not sure this is a common practice or not.

Thank you.

Source: Docker Questions