What is the best practice to deal with application .env files in development and production in Docker?

Viewed 116

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
0 Answers
Related