I’ve created a Docker container with an Ubuntu base image. Setting the environment variables through a .env file. When running the container, I can see the variables being passed through using the shell terminal.
I want to able to get the env varibles in my wp-config. I am using getenv but it is not working..
Any suggestions..
Thanks
3
Answers
Both previous answers, I could already pass the env variables to my apache environment served by docker. I just needed to add Pass env_name to the .htaccess file for each env variable.
I could then get the values via the $SERVER['env_name'] within my php application..
You can set the environment variable for your docker container in 2 ways
docker run -e VARIABLE=VALUE ...
In docker-compose file you can set in like:
https://docs.docker.com/compose/environment-variables/#set-environment-variables-in-containers
You use
.env
file, so you certainly usedocker-compose
. If not usedocker-compose
,.env
will not make effect. And the .env file must be placed in the directory wheredocker-compose
is run from.Whole solution could be something like:
.env
docker-compose.yml
wp-config.php
I guess you did not get env because you did not do
- MY_VARIABLE="${MY_VARIABLE}"
indocker-compose.yml
, the value in.env
will not be automatically act as an environment variable to container, you need to handle it in compose file. FYI.Detail refers to offical guide