1

My folder structure looks like this

- root-dir
-- docker
-- src //contains laravel application
---.env
-- docker-compose.yml

As you might know in both laravel .env and docker-compose.yml files you need to specify the connection settings to db

// .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

// docker-compose.yml
environment:
   - MYSQL_ROOT_PASSWORD=secret
   - MYSQL_DATABASE=homestead
   - MYSQL_USER=homestead
   - MYSQL_PASSWORD=secret

Is there a way where I can make docker-compose to "read" the settings from the .env file, since the last one is not tracked by git? so basically if I have to change settings I have to do it only in one file and also to not track the credentials on git for docker-compose.yml

1 Answer 1

2

You can do it like(From docker documentation https://docs.docker.com/compose/environment-variables/#the-env-file):

The “.env” file You can set default values for any environment variables referenced in the Compose file, or used to configure Compose, in an environment file named .env:

$ cat .env
TAG=v1.5

$ cat docker-compose.yml
version: '3'
services:
  web:
    image: "webapp:${TAG}"

You can also use:

The “env_file” configuration option You can pass multiple environment variables from an external file through to a service’s containers with the ‘env_file’ option, just like with docker run --env-file=FILE ...:

web:
  env_file:
    - web-variables.env
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.