1

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?

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Actually It didn't work. I was probably just a coincidence. The .env and the compose file were in the same directory but I was running docker-compose up from a child dir. That was the problem.

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.