skip to Main Content

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

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?

2

Answers


  1. Try this syntax

    env_file:
     - config.env
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search