Welcome Guest, Not a member yet? Register   Sign In
.env and Docker Container Environment - potential problems

(This post was last modified: 01-11-2021, 12:47 AM by tgix.)

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
.default.database ci4
.default.username root
.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?

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/CodeIgni...ssues/3992

Unfortunately it seems there is no better way.

Theme © iAndrew 2016 - Forum software by © MyBB