MariaDB / mariadb-docker

Docker Official Image packaging for MariaDB
https://mariadb.org
GNU General Public License v2.0
770 stars 438 forks source link

Can we use .env file to pass environment variables? #337

Closed SnowNooDLe closed 3 years ago

SnowNooDLe commented 3 years ago

Hey guys,

Recently joined dev world, a fresh developer here. Please correct me if I am doing something wrong :)

So I am currently working on a web app project with React + Flask and we decided to use MariaDB with SQLAlchemy. We also decided to use with docker as our React and Flask are also running on docker as well. The current docker-compose.yml is like the following code.

Working fine

# docker-compose.yml

version: "3.8"

services:
  db:
    image: mariadb:10.5.8
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: test
      MYSQL_USER: user
      MYSQL_PASSWORD: user
      MYSQL_ROOT_PASSWORD: root_test

    volumes:
      - ./init:/docker-entrypoint-initdb.d

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

With the code above, it works as we expected, we can put user and user via adminer to see what's happening. However,

Not working

# docker-compose.yml

version: "3.8"

services:
  db:
    image: mariadb:10.5.8
    restart: always
    ports:
      - 3306:3306
     environment:
       - MYSQL_DATABASE=${DATABASE}
       - MYSQL_USER=${USERNAME}
       - MYSQL_PASSWORD=${PASSWORD}
       - MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}

    volumes:
      - ./init:/docker-entrypoint-initdb.d

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

with .env

DATABASE=test
USERNAME=user
PASSWORD=user
ROOT_PASSWORD=root_test

This is not working as we expected, it gives us the access denied error message.

I also tried parsing env_file: .env in the docker-compose.yml and still didn't work :(

Is this a related issue to this PR? Add support for MARIADB_ versions of env vars or is there a different way of using/parsing .env variables to docker-compose.yml? (I am like 99% confident that the syntax of parsing .env variables is fine as this is how we set up the docker-compose for React and Flask.)

As I could only see the usage of MariaDB docker-compose.yml with specified environment variables instead of using .env and I am not sure whether it is a good practice to make those information visible (e.g, password and root_password)

Thanks for reading a long post and happy to get any feedback or answers! :+1:

tianon commented 3 years ago

When switching from the working config to the one using a .env file, did you clear out your volume? (docker-compose down --volumes)

SnowNooDLe commented 3 years ago

When switching from the working config to the one using a .env file, did you clear out your volume? (docker-compose down --volumes)

Thanks for the reply! I have never done that command docker-compose down -v so just tried that way but still didn't work :(

I even tried to change the name of service from db to mydb as I noticed that this refers to the 'server' section in aminer. (Assuming change it to the different server name, creating a server with a fresh config...?) image

But still not working :(

SnowNooDLe commented 3 years ago

In case someone is having the same issue, I solved this problem by adding one extra parameter in the command line to run docker-compose.yml.

Do (Assuming your .env file name is also .env)

docker-compose --env-file .env up -d

Instead of doing

docker-compose up -d

By doing the one with --env-file .env, we can also confirm that variables are passed properly by typing docker-compose config as well.

I shall leave this opens in case someone wants to see regarding this problem, but please feel free to close this issue :+1:

tianon commented 3 years ago

Nice, glad you got it figured out! :+1:

(In the future, these sorts of questions/requests would be more appropriately posted to a dedicated support forum, such as the Docker Community Forums, the Docker Community Slack, or Stack Overflow.)

SnowNooDLe commented 3 years ago

Nice, glad you got it figured out!

(In the future, these sorts of questions/requests would be more appropriately posted to a dedicated support forum, such as the Docker Community Forums, the Docker Community Slack, or Stack Overflow.)

Will do! Thanks for the feedback/recommendation :)