marcelo
marcelo

Reputation: 413

Variables of .env not being passed to the MYSQL container: Defaulting to a blank string

Friends,

I don't know why but my Docker-Compose is not passing the variables of my .env file to my container. I am getting the following warnings before the container exits:

WARNING: The MYSQL_DATABASE variable is not set. Defaulting to a blank string.
WARNING: The MYSQL_PASSWORD variable is not set. Defaulting to a blank string.
WARNING: The MYSQL_ROOT_PASSWORD variable is not set. Defaulting to a blank string.

Here is my yaml-file:

version: "3.3"
services:
  backend:
    container_name: backend
    build:
      context: back
      dockerfile: Dockerfile
    volumes:
      - ./back/:/app
    ports:
      - "3000:3000"
    networks:
      - quotes
  data:
    container_name: data
    build:
      context: data
      dockerfile: Dockerfile
    # volumes:
    #   - ./data/quotes-database.sql:/tmp/quotes-database.sql
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    networks:
      - quotes
networks:
  quotes:

And here my .env:

MYSQL_DATABASE=quotes-database
MYSQL_PASSWORD=root
MYSQL_ROOT_PASSWORD=root

The .env file and the docker-compose file are in the same directory. What am I doing wrong? Any idea?

Upvotes: 1

Views: 2771

Answers (2)

marcelo
marcelo

Reputation: 413

The docker-compose file and the .env file were in the same directory but I was running docker-compose up from a child directory. Thats why It wasn't working.

Upvotes: 2

Julien B.
Julien B.

Reputation: 3324

The docker-compose is supposed to look for a .env in the current directory. However, should you want to use a .env file with another name or in a different location you can specify the .env file to use when running docker-compose.

docker-compose --env-file ../somepath/myfile.env up 

Upvotes: 2

Related Questions