Upfrontbeats
Upfrontbeats

Reputation: 101

Docker Set environment variables in ubuntu cant access through php getenv

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

Upvotes: 2

Views: 4796

Answers (3)

Upfrontbeats
Upfrontbeats

Reputation: 101

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

Upvotes: 1

atline
atline

Reputation: 31644

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

Upvotes: 1

Hemant Singh
Hemant Singh

Reputation: 1608

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

Upvotes: 1

Related Questions