1

I'm trying to follow Docker docs and SO suggestions, but still getting "variable not set" errors on just trying to "docker-compose up db". Can someone please check my .env and yaml file? Both are in the same directory but it doesn't seem yml is reading .env???

.env:

FS_NAME=MY_BLOG_NAME
FS_REGION=us-east-2
DB_HOST=localhost
DB_USER=postgres
DB_PASS=$$323MY_PASS$&84
DB_NAME=postgres

docker-compose.yml:

version: '3.7'
volumes:
    db_data:

x-shared_environment: &shared_environment
   LOG_LEVEL: ${LOG_LEVEL:-debug}
   DATABASE_HOST: ${DB_HOST}
   DATABASE_NAME: ${DB_NAME}
   DATABASE_USERNAME: ${DB_USERNAME}
   DATABASE_PASSWORD: ${DB_PASSWORD}

services:
  app:
    image: MY_BLOG_NAME:latest
    build:
      context: .
    environment:
      <<: *shared_environment
      FS_NAME: ${FS_NAME}
      FS_REGION: ${FS_REGION}
    depends_on:
    - db
    ports:
      - '8080:8080'
      
db:
    image: postgres:12.2-apline
    volumes:
        -db_data/var/lib/postrgresql/data/pgdata
    environment:
        PGDATA: /var/lib/postgresql/data/pgdata
        POSTGRES_USER: {DB_USERNAME}
        POSTGRESS_PASSWORD: {DB_PASSWORD}
        POSTGRES_DB: {DB_NAME}
        POSTGRESS_PASSWORD: {DB_PASSWORD}
    ports:
        -'5432:5432'

1 Answer 1

1

You need to specify the directory of your .env file, add env_file configuration for each service.

https://docs.docker.com/compose/environment-variables/

For exapmle:

db:
    image: postgres:12.2-apline
    volumes:
        -db_data/var/lib/postrgresql/data/pgdata
    env_file: 
        - ./.env
    environment:
        PGDATA: /var/lib/postgresql/data/pgdata
        POSTGRES_USER: {DB_USERNAME}
        POSTGRESS_PASSWORD: {DB_PASSWORD}
        POSTGRES_DB: {DB_NAME}
        POSTGRESS_PASSWORD: {DB_PASSWORD}
    ports:
        -'5432:5432'
Sign up to request clarification or add additional context in comments.

4 Comments

I actually got the env variables right (but also added ur suggestion). Once env variables fixed... my final error on running docker-compose up db is: ``` ERROR: The Compose file './docker-compose.yml' is invalid because: Invalid top-level property "db". Valid top-level sections for this Compose file are: version, services etc.... NOTE: that to get the env variables working correctly I changed my original version from 3.7 to 3.8 (to support my latest docker v install). Any idea what the top property db error is?
note: I did not add a separate env_file for the services because my .env file has all variables for FS, DB and AWS. So " - ./.env" should suffice? any thoughts on the top level db error?
You can't define the env_file at the top level of the file, it should be configured for each service seperatly stackoverflow.com/questions/48154093/…
I figured it out... "db" needed to be indented... thx.for the help along the way. 😊

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.