skip to Main Content

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?

2

Answers


  1. You can move all the env variables with

    cat Laravel/.env >> docker/webapp.env
    

    and then create symlink

    ln -s ../docker/webapp.env Laravel/.env
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search