How to set PHP $_SERVER variable with Docker? How to set PHP $_SERVER variable with Docker? apache apache

How to set PHP $_SERVER variable with Docker?


Consider the below Dockerfile

FROM php:5.6-apacheRUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-devRUN docker-php-ext-install pdo pdo_mysql gd curlRUN a2enmod rewriteRUN service apache2 restartRUN echo 'PassEnv FIRST_NAME' > /etc/apache2/conf-enabled/expose-env.conf RUN echo '<?php echo $_SERVER["FIRST_NAME"];' > /var/www/html/first.php && echo '<?php echo $_SERVER["LAST_NAME"];' > /var/www/html/last.php

Now run the container using

docker run -it -e FIRST_NAME=TARUN -e LAST_NAME=LALWANI -p 80:80 4ba2aa50347b

Testing

$ curl localhost/first.phpTARUN$ curl localhost/last.php$

As you can see the only FIRST_NAME can be accessed, because we exposed the same using PassEnv directive in our apache config insider the container


I know this is a old thread, however, another solution could also be that inside your docker compose, you need to remove the "-" in front of ENV and then instead of "=" it needs to be ":".

It will end up looking like this then:

version: '2'services:  webserver:    build: ./docker/webserver    image: dev_web    ports:      - "80:80"      - "443:443"    volumes:      - /pathtodev/www:/var/www/html    links:      - db    environment:      ENV: developement  db:    image: mysql:5.7    ports:       - "3306:3306"    volumes:      - ./db:/var/lib/mysql    environment:      - MYSQL_ROOT_PASSWORD=******      - MYSQL_DATABASE=db_dev

At least it's working like that in version 3.