MojoJojo
MojoJojo

Reputation: 4242

Laravel not picking up environment variables

I'm trying to run a PHP application in a docker container with Apache and Laravel. My dockerfile looks like below:

#Create a minimalistic docker container for deploying PHP/Laravel applications
FROM php:7-apache
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
RUN a2enmod rewrite 
RUN a2enmod setenvif
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!AllowOverride None!AllowOverride All!g' /etc/apache2/apache2.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
RUN sed -i '10i \
        SetEnvIf X-Forwarded-Proto "https" HTTPS=on \
        RewriteEngine on \
        RewriteCond %{HTTPS} !=on \
        RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC] \
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] \n' /etc/apache2/sites-available/*.conf
RUN mv $PHP_INI_DIR/php.ini-production $PHP_INI_DIR/php.ini
RUN apt-get update && apt-get install -y \
        zip \
        unzip \
        wget \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libpq-dev \
    && docker-php-ext-install -j$(nproc) iconv \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pdo pdo_pgsql pgsql

COPY install-composer.sh /usr/local/bin
RUN /usr/local/bin/install-composer.sh
COPY . /var/www/html
WORKDIR /var/www/html
RUN composer install --no-plugins --no-scripts --optimize-autoloader --no-dev
RUN composer require nunomaduro/collision --dev
RUN php artisan config:cache
RUN chown -R www-data:www-data /var/www/html
#RUN php artisan route:cache

I deploy the container on AWS ECS and in my launch configuration, I setup environment variables. However, for some reason, Laravel is not picking up any environment variables. When I examine the running container:

docker exec -it <<mycontainerid>> /bin/bash
root@ed472a71a444:/var/www/html#env

I can see all the environment variables setup properly:

root@0c591ce9ef7c:/var/www/html# env
DB_PORT=5432
MAIL_DRIVER=smtp
DB_HOST=xxxxxxxxxxxxxxxxxxxxxxxxxxx
CAPTCHA_SITE_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxx
HOSTNAME=xxxxxxxxx
MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APACHE_CONFDIR=/etc/apache2
PHPIZE_DEPS=autoconf        dpkg-dev        file        g++         gcc         libc-dev        make        pkg-config      re2c
GPG_KEYS=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
APP_DEBUG=false
PHP_EXTRA_CONFIGURE_ARGS=--with-apxs2 --disable-cgi
MAIL_HOST=xxxxxxxxxxxxxxxxxxxxxxxxxxx
PHP_ASC_URL=https://secure.php.net/get/php-7.2.11.tar.xz.asc/from/this/mirror
PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2
APACHE_DOCUMENT_ROOT=/var/www/html/public
PHP_EXTRA_BUILD_DEPS=apache2-dev
APP_ENV=production
PWD=/var/www/html
APP_KEY=base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

but when I do this:

root@ed472a71a444:/var/www/html# php artisan tinker
Psy Shell v0.9.8 (PHP 7.2.11 — cli) by Justin Hileman
>>> print_r($_ENV)
Array
(
    [SHELL_VERBOSITY] => 0
)
=> true

However, running the following returns all variables:

root@ac526d5b9184:/var/www/html# php artisan tinker
Psy Shell v0.9.8 (PHP 7.2.11 — cli) by Justin Hileman
>>> getenv()
=> [
     "DB_PORT" => "5432",
     "MAIL_DRIVER" => "smtp",
     "DB_HOST" => "xxxxxxxxxxxxx",
     "CAPTCHA_SITE_KEY" => "xxxxxxxxxxxxxxxxxxxxxxxx",
     "APP_DEBUG" => "false",
     "PHP_EXTRA_CONFIGURE_ARGS" => "--with-apxs2 --disable-cgi",
     "PHP_ASC_URL" => "https://secure.php.net/get/php-7.2.11.tar.xz.asc/from/this/mirror",
     ...........
  ]

What am I doing wrong? How do I fix it?

Update 1: I realised that my PHP.ini had this:

variables_order = "GPCS"

Adding an "E" at the beginning caused the environment variables to appear in $_ENV but Laravel still can't ready them:

[2018-11-04 23:20:14] production.ERROR: No application encryption key has been specified. {"exception":"[object] (RuntimeException(code: 0): No application encryption key has been specified. at /var/www/html/vendor/laravel/framework/src/Illuminate/Encryption/EncryptionServiceProvider.php:42)
[stacktrace]

Update 2: The following also works, but not sure why Laravel can't see it:

root@b377f6cf15f0:/var/www/html# php artisan tinker
Psy Shell v0.9.8 (PHP 7.2.11 — cli) by Justin Hileman
>>> env('APP_KEY')
=> "base64:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
>>>

Upvotes: 4

Views: 7282

Answers (1)

MojoJojo
MojoJojo

Reputation: 4242

Okay, thanks to @Travis Britz, I figured it out. Since artisan config:cache was running as part of docker build and the environment variables weren't set at build time, this resulted in empty values for all environment variables. I removed the config:cache command and replaced it with config:clear and it seems to have worked!

Upvotes: 7

Related Questions