Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use different .env file for Laravel application?

Laravel comes by default with a .env file that comes under the root folder when you create a new project. I am having a fresh Laravel installation with this folder structure

root/
├── docker/
│   ├── php/
│   │   ├── Dockerfile
│   └── nginx/
│   |   ├── Dockerfile
│   |   └── default.conf
|   |__ webapp.env  
├── Laravel/
│   ├── app/
│   ├── bootstrap/
│   ├── config/
│   ├── database/
│   ├── public/
│   ├── resources/
│   ├── routes/
│   ├── storage/
│   ├── tests/
│   ├── vendor/
│   ├── artisan
│   ├── composer.json
│   ├── composer.lock
│   └── README.md
|   |__ .env
└── docker-compose.yml

And this is how I am referencing both at the moment in the docker-compose.yml

...
env_file:
      - "docker/webapp.env"
      - "laravel/.env"

As you can see I have a webapp.env inside the docker folder, in which I am keeping any environment variables required for the Docker images I am using. At the moment it is a bit hard to manage the environment variables as I need to jump from one file or another etc, so I was wondering whether it is possible to copy all the env variables from the .env file to my webapp.env and point Laravel to use this file instead of the default one?

like image 903
ltdev Avatar asked Aug 31 '25 20:08

ltdev


1 Answers

I'm not sure this approach works,( without testing). We used this to map other files/folders into the project. It is the same theory you can use, too

services:
  app:
    env_file:
      - "docker/webapp.env"
    volumes:
      - ./Laravel:/var/www/html # this to map your app folder
      - ./docker/webapp.env:/var/www/html/.env # this to map your .env file

docker-compose down
docker-compose up --build

This will map all the items in webapp.env to .env on the docker build.

like image 51
Abdulla Nilam Avatar answered Sep 03 '25 11:09

Abdulla Nilam