2

I have .env file which is used by dockerized PHP application. Aplication loads variables from .env file using dotenv and initialize DB connection. The .env file has basic DB config in it:

DB_ENGINE = "mysql"
DB_HOST = "host"
DB_NAME = "db"
DB_USERNAME = "user"
DB_PASSWORD = "pass"

Obviously, those credentials will be different in the dev environment and in the production environment. My question is, how do deal with those different .env files when building app images for development and production? .env MUST be physically at the root app folder because it's been loaded via Dotenv.

For now I have created .env.dev and .env.prod files. I may use 2 different Dockerfiles for dev and prod and just run COPY command. But I don't think this is a good practice. I'm using GitLab CI for deploying - I build app image and run container in production. I was also thinking about storing .env.prod file in the Gitlab file variable. What is the best practice in my case?

EDIT I temporary solved my problem with multi stage build and targets But I don't think is a good practice:

FROM php:7.4-cli as base
...

FROM base as dev
COPY ./.env.dev /usr/src/myapp/.env

FROM base as prod
COPY ./.env.prod /usr/src/myapp/.env
feyen01
  • 377
  • 5
  • 18
  • 1
    Define them as ENV variable at container's run time. Related: https://stackoverflow.com/questions/41827665/how-to-change-php-dotenv-env-variables-dynamically-in-laravel-or-php – β.εηοιτ.βε Mar 09 '21 at 21:30
  • And another related link is https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file – β.εηοιτ.βε Mar 09 '21 at 21:33
  • This doesn't physically create `.env` file. Therefore I get the following error: `Fatal error: Uncaught Dotenv\Exception\InvalidPathException: Unable to read any of the environment file(s) at /usr/src/myapp/.env` – feyen01 Mar 10 '21 at 06:55

0 Answers0