CodeIgniter Forums
.env and Docker Container Environment - potential problems - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: .env and Docker Container Environment - potential problems (/showthread.php?tid=78375)



.env and Docker Container Environment - potential problems - tgix - 01-11-2021

Just a sanity-check to see if I am missing something.

In a recent project I packaged a CI4 application into a Docker (php:7.3.26-apache-stretch image) and uploaded it to AWS FARGATE. In the application I use .env to configure stuff like database settings:
PHP Code:
database.default.hostname localhost
database
.default.database ci4
database
.default.username root
database
.default.password root 
Since I use CloudFormation to manage my hosting projects, I wanted to set the hostname dynamically to the RDS created by CloudFront. I tried doing this with Docker ENV, but this did not work.
After googling around I came to the conclusion that it is a combination of issues here but the main culprit is the bash-shell (which Ubuntu is using, which is the base of the Docker image) defining variable names as strictly alphanumeric and underscore - dot is not allowed!
The work-around I have now is to use legal variable names and then in app/Config/Database.php::__construct() do this:
PHP Code:
if (getenv('DATABASE_HOSTNAME')) {
    
$this->default['hostname'] = getenv('DATABASE_HOSTNAME');
 }
 if (getenv('DATABASE_USERNAME')) {
     $this->default['username'] = getenv('DATABASE_USERNAME');
 }
 if (getenv('DATABASE_PASSWORD')) {
     $this->default['password'] = getenv('DATABASE_PASSWORD');
 
Ugly, but gets the job done.

My question now - is there a better way?
/Mattias

Ah, and by the way: I also ran into another problem for some reason, variables return the wrong value using getenv() - https://github.com/codeigniter4/CodeIgniter4/issues/3992


RE: .env and Docker Container Environment - potential problems - kenjis - 01-19-2021

Unfortunately it seems there is no better way.