Background
I'm trying to use Wercker to run my test for a PHP application. Wercker uses Docker containers to setup a test environment to execute tests in. It uses Environment Variables to expose the connection params for any connected services like MySQL and Elasticsearch. Example MYSQL_PORT_3306_TCP_ADDR = 127.0.1.1
My core Docker containers is running Ubuntu 14.04 with PHP and Apache already installed on the container.
Problem
I can't seem to access the Environment Variables via php $_SERVER or $_ENV when running via Apache. It works fine if I run the script via CLI php ./db_connect.php
or if I run PHP using its build in server php -S localhost:8000
. However If I try and access a page via the Apache virtual host, the Environment Variables are not available.
Progress
I have setup Apache with the mod used to allow environmental variables "I think"
sudo a2enmod env
sudo service apache2 restart
I'm trying to access the Environment Variables in my script.
$database_host = $_SERVER["MYSQL_PORT_3306_TCP_ADDR"];
$database_username = $_SERVER["MYSQL_ENV_MYSQL_USER"];
$database_password = $_SERVER["MYSQL_ENV_MYSQL_PASSWORD"];
$database_name = $_SERVER["MYSQL_ENV_MYSQL_DATABASE"];
$elasticsearch_host = $_SERVER["ELASTICSEARCH_PORT_9300_TCP_ADDR"];
I can add new variables in my .htaccess, I just don't get all the system environmental variables.
SetEnv TEST_VAR test
I have read this question How to get system environment variables into PHP while running CLI & Apache2Handler? but i'm not sure what its suggesting to do.
Question
How do I expose System Environment Variables to Apache and PHP?
$_ENV['MYSQL_PORT_3306_TCP_ADDR']
? Note that the environment variables for links are deprecated, and no longer set for the new custom networks in docker 1.9 and above. A better way is to connect using the name of the container that's linked, e.g.http://mysql
to connect to the "mysql" container. The environment variables didn't add much, because you need both the name and the port number to obtain the ip-address, so using the name of the container is easier anyway.