0

I try to set variables in my `docker-compose.yml` file in a separate `PORTAL_ENVIRONMENT.env` file
I don't know, what I am doing wrong.
I have this output:
WARNING: The POSTGRES_DB variable is not set. Defaulting to a blank string.
WARNING: The POSTGRES_USER variable is not set. Defaulting to a blank string.
WARNING: The POSTGRES_PASSWORD variable is not set. Defaulting to a blank string.
WARNING: The HOST variable is not set. Defaulting to a blank string.
WARNING: The PORT2 variable is not set. Defaulting to a blank string.
WARNING: The PORT3 variable is not set. Defaulting to a blank string.
WARNING: The PORT1 variable is not set. Defaulting to a blank string.

docker-compose.yml looks like this:

  client_app:
    env_file:
      - PORTAL_ENVIRONMENT.env
    image: 'client:latest'
    build:
      context: ./
    container_name: client
    depends_on:
      - db
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/${POSTGRES_DB}
      - SPRING_DATASOURCE_USERNAME=${POSTGRES_USER}
      - SPRING_DATASOURCE_PASSWORD=${POSTGRES_PASSWORD}
      - SPRING_JPA_HIBERNATE_DDL_AUTO=update
      - HOST_NAME=${HOST}
      - CREDIT_POST=${PORT1}
      - PRODUCT_POST=${PORT3}
    ports:
      - ${PORT2}:8080

(there are 3 separate modules,every looks almost the same)

PORTAL_ENVIRONMENT.env looks like this:

HOST=localhost

PORT1=8089
PORT2=8090
PORT3=8091

POSTGRES_DB=create_credit
POSTGRES_USER=user
POSTGRES_PASSWORD=pass123

I run it with:

sudo docker-compose up --force-recreate --build

2 Answers 2

4

When specifying your service's environment, you want those environment variables (HOST, POSTGRES_DB, etc) to be accessible to docker-compose at the time that it parses your docker-compose file. To do that, you should put them in a file called just .env. (Alternatively, if they are set in your shell environment at the time you run docker-compose, that is okay too, but you probably want to be keeping them in a .env file.)

Instead, you're trying to use env_file: in the docker-compose file. That specifies that the service that uses the env_file should look in that file and then update its own environment with that information. env_file is like environment:, but it looks at a file. It's just for the container to use, and docker-compose can't use it to set up how to run the container.

If you'd like to also pass variables from a .env file into a container, you can do something like one of these:

environment:
 - MY_VARIABLE=${VARIABLE_IN_MY_ENV_FILE}
 - MY_VARIABLE_SAFER=${VARIABLE_IN_MY_ENV_FILE:?err}

(the ?err will cause the startup to fail if the environment variable is not set, which is a nice trick.)

If you want the .env file used by docker-compose to not be named .env (maybe you are fond of the name you already have for some reason), docker-compose also has its own --env-file command-line option which you can use to specify the path of the .env file to use.

If you're still curious or confused you could also check out the docker-compose Environment Variables documentation page - that one talks about both environment-setting for docker-compose and for containers, all in the same webpage.

Sign up to request clarification or add additional context in comments.

2 Comments

That has solved my problem, thanks for the full explanation! :)
Having the environment variables in environment configuration with env_file setting didn't solve the problem and it still can't read it from env file.
0

Also make sure you are running docker-compose up in the correct folder where your YML File is, and that you have your .env file in the correct path in relation to your YML file

Comments

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.