skip to Main Content

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


  1. Chosen as BEST ANSWER

    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..


  2. You can set the environment variable for your docker container in 2 ways

    1. In docker run command use docker run -e VARIABLE=VALUE ...
    2. In docker-compose file you can set in like:

        environment:
          - DEBUG=1
      

      https://docs.docker.com/compose/environment-variables/#set-environment-variables-in-containers

    Login or Signup to reply.
  3. You use .env file, so you certainly use docker-compose. If not use docker-compose, .env will not make effect. And the .env file must be placed in the directory where docker-compose is run from.

    Whole solution could be something like:

    .env

    MY_VARIABLE=abc
    

    docker-compose.yml

    version: '3'
    services:
      my_service:
        environment:
          - MY_VARIABLE="${MY_VARIABLE}"
    

    wp-config.php

    echo getenv('MY_VARIABLE');
    

    I guess you did not get env because you did not do - MY_VARIABLE="${MY_VARIABLE}" in docker-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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search