Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set PHP $_SERVER variable with Docker?

I've created a PHP/Apache/MySQL development environment with Docker and would like to set variable that I can use with $_SERVER in PHP.

Usually I will configure something like that in my virtual host

SetEnv ENV "developement"

Is there a way to do it with my docker_compose.yml file ? I'll try by using environment: - ENV=developement in my docker-compose file but it doesn't work.

Here is my Dockerfile

FROM php:5.6-apache

RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev

RUN docker-php-ext-install pdo pdo_mysql gd curl

RUN a2enmod rewrite

RUN service apache2 restart

and my docker-compose.yml

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
like image 654
user2993925 Avatar asked Sep 14 '17 19:09

user2993925


1 Answers

Consider the below Dockerfile

FROM php:5.6-apache

RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev

RUN docker-php-ext-install pdo pdo_mysql gd curl

RUN a2enmod rewrite

RUN service apache2 restart

RUN 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.php
TARUN

$ 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

like image 96
Tarun Lalwani Avatar answered Oct 02 '22 16:10

Tarun Lalwani