3

I Have watched approximately 23.74 docker-compose tutorials for laravel and mysql containers!

Please can someone explain to me???

When I create my docker-compose file I create an mysql container from a mysql image. THEN I have to enter variables that look like this:

environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql

What is the difference between these variables and the variables that I enter into the .env file of my laravel app. THESE ONES:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laradb
DB_USERNAME=root
DB_PASSWORD=secret

WHAT IS THE DIFFERENCE????

What is the docker-compose variables doing. And what are these .env variables doing. And Why am I setting these up twice?

The reason I am asking is because whenever I follow docker tutorials to set up mysql I can only get it to work if I use the variety of variables provided to me by the teacher?? This makes no sense. What about if I want to use my own variable values??! As soon as I try use my own variables the db breaks and I can't connect to it. Is homestead some special db instance that is for laravel? This was not an issue when I do it all locally without docker.

For example. The above docker-compose variables were used to create a mysql container and then when i connect to it with SQL workbench I see a schema called 'homestead'. Now what do I do if I don't want that Schema to be called homestead, or what if I want to add another Schema?? It doesn't let me??(permission denied).

I have now spent 3 days trying to create an empty laravel app that connects to a db in a separate container that uses mysql that I can connect to via SQL workbench to see the actual db. I want to be able to create the schema name I want to use in SQL workbench and then be able to set that schema as the db name in my laravel .env file.

Please HELP! You don't have to solve this problem for me but can you point me towards some helpful material that explains this stuff!! For docker-compose specifically mysql. No looking to use std docker commands in the terminal if possible.

1
  • you can use in compose file MYSQL_DATABASE: '${.env_vairable_name}' like this you need to only put in .env Commented Dec 14, 2020 at 3:49

1 Answer 1

1

.env file vs docker-compose.yml environment::

They have different scopes / precedence.

Passing environment variables in for the benefit of MySQL:

The MySQL container expects those environment variables to exist and have values.

Likewise for the Laravel container needs to be able to talk to the MySQL container, hence it needs the values to match and that is why there is overlap.

The bash command printenv might help tighten this up, as you can see what environment variables are exposed to which containers (docker run msql_container_name bash -c 'printenv' vs docker run laravel_container_name bash -c 'printenv').

You've mentioned you don't mind being sent to references, so I've primarily done that - but I'm happy to elaborate in here or in comments if it still isn't making sense / I'm not addressing the main issue.

Sign up to request clarification or add additional context in comments.

Comments

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.