Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Docker compose won't use .env variables [NodeJS, Docker]

I want to use my env variable inside the docker compose file. When I try to run it I get this error for every variable in there like they weren’t defined:

The password variable is not set. Defaulting to a blank string.

Some context:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Docker-compose:

version: '3.3'

services:
  mysqldb:
    image: mysql:8
    restart: always
    env_file: config.env
    environment:
      - MYSQL_ROOT_PASSWORD=$password
      - MYSQL_DATABASE=$database
    ports:
      - $db_port:$db_docker_port
    volumes:
      - db:/var/lib/mysql
  app:
    depends_on:
      - mysqldb
    build: ./
    restart: unless-stopped
    env_file: config.env
    ports:
      - $port:$port_docker
    environment:
      - DB_HOST=mysqldb
      - DB_USER=$user
      - DB_PASSWORD=$password
      - DB_NAME=$database
      - DB_PORT=$db_docker_port
    stdin_open: true
    tty: true
volumes: 
  db:

config.env:

database=dbname
host=localhost
user=user
password=password
port=5005
port_docker=5004
db_port=3306
db_docker_port=3307

Did I import the config.env file incorrectly?

>Solution :

Your environment variable file should be called .env and you use the variables using the syntax ${password}.

The way you’ve done it – with the env_file statement – is used when you want the variables set inside the container. You can’t use them in the docker-compose file, when you do it that way.

You can remove the env_file statements in the docker-compose file unless you need the variables in there as well. But it looks like you pass them all in using the docker-compose file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading