Exposing Elastic Beanstalk environment variables to Laravel's artisan using Docker

I’m attempting to deploy my application to production for the first time using Elastic Beanstalk, and I’ve created an RDS instance along with my Elastic beanstalk application. Since my application uses PHP5-FPM, I have to expose these environment variables inside Dockerfile like this:

RUN echo 'env[RDS_HOSTNAME] = $RDS_HOSTNAME' >> /etc/php5/fpm/pool.d/www.conf
RUN echo 'env[RDS_PORT] = $RDS_PORT' >> /etc/php5/fpm/pool.d/www.conf
RUN echo 'env[RDS_DB_NAME] = $RDS_DB_NAME' >> /etc/php5/fpm/pool.d/www.conf
RUN echo 'env[RDS_USERNAME] = $RDS_USERNAME' >> /etc/php5/fpm/pool.d/www.conf
RUN echo 'env[RDS_PASSWORD] = $RDS_PASSWORD' >> /etc/php5/fpm/pool.d/www.conf

This works fine, my PHP scripts can access my RDS database. However, I also need to migrate my database when I deploy, so I added this line to my Dockerfile:

  • Accessing kubernetes running inside docker in a guest OS from the host OS
  • convert Dockerfile to Bash script
  • WordPress on docker-compose no run
  • What is root cgroup process behavior on Debian?
  • Limit Network access but allow a specific IP for a Running Docker Container
  • Run old Linux release in a Docker container?
  • # Run artisan migrations
    RUN php /var/www/artisan migrate --force

    This fails, as the RDS environment variables don’t exist for PHP on the command line. I’ve confirmed this by doing die(var_dump($_SERVER)); at the top of the artisan script, and as I expected the RDS environment variables aren’t there.

    To try and get the environment variables to PHP CLI I tried doing this in my Dockerfile:

    RUN echo 'RDS_HOSTNAME=$RDS_HOSTNAME' >> /etc/environment
    RUN echo 'RDS_PORT=$RDS_PORT' >> /etc/environment
    RUN echo 'RDS_DB_NAME=$RDS_DB_NAME' >> /etc/environment
    RUN echo 'RDS_USERNAME=$RDS_USERNAME' >> /etc/environment
    RUN echo 'RDS_PASSWORD=$RDS_PASSWORD' >> /etc/environment
    RUN source /etc/environment

    However, again, the environment variables don’t exist.

    How can I give the PHP command line interpreter access to my RDS environment variables?

    The only other option I can think of is to hard-code my RDS credentials inside my application config, which as I’m sure you understand is something I don’t want to do.

  • AngularJS NOT WORKING in LARAVEL docker continer
  • How Docker and Ansible fit together to implement Continuos Delivery/Continuous Deployment
  • How to ignore certificate errors in Boot2Docker on windows
  • Docker image wrapping for binary blobs
  • Restrict Docker exposed port from only specific IP adresses
  • How To run from host machine bash script inside docker container and remain in bash in container [duplicate]
  • 2 Solutions collect form web for “Exposing Elastic Beanstalk environment variables to Laravel's artisan using Docker”

    You can set environment variables in the config of the beanstalk application. Go to your application, click Configuration on the left menu, then look for Software Configuration section and click the edit icon, a gear looking thing. On this page you can then add all the environment variables you want and change them at any time. I believe you can also do this in your .elasticbeanstalk/config.yml however I just normally do it from the web interface.

    Most likely the mod_env doesn’t pass those variables to your application. In your /var/apache2/sites-enabled/000-default.conf, or other file which is responsible for your app, you need to specify all environment parameters which should be passed like in the example below:

    <VirtualHost *:80>
        DocumentRoot "${APP_DOCUMENT_ROOT}"
        <Directory "${APP_DOCUMENT_ROOT}">
            AllowOverride AuthConfig FileInfo Indexes Limit Options=All,MultiViews
            Options FollowSymLinks MultiViews
            Require all granted
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ErrorLog ${APACHE_LOG_DIR}/error.log
        #here the magic begins:
        PassEnv APP_RUN_MODE
        PassEnv RDS_HOSTNAME
        PassEnv RDS_PORT
        PassEnv RDS_DB_NAME
        PassEnv RDS_USERNAME
        PassEnv RDS_PASSWORD

    As you can see – the most important is PassEnv command.

    You can create a file called 000-default.conf and put it into a docker folder in your project. Then add to the Dockerfile

    ADD docker/000-default.conf /etc/apache2/sites-available/000-default.conf
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.