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:

  • How does Docker use ports 2375 and 4243?
  • OpenWisk Docker Support install fails
  • docker not responding when using different data directory
  • Using Kubernetes with an Artifactory/Nginx proxy
  • Docker api: push an image to docker hub private repository
  • Optimizations for ng build at runtime
  • # 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.

  • Kubernetes pod fails while making call to google cloud pub/sub with unknown certificate autority
  • mysql in docker container hangs
  • How to use CloudBees Docker Custom Build Environment Plugin?
  • Dockerfile: Permission denied when trying to install ruby-build
  • Deleting folders in docker conatiner using python
  • Running JUnit Tests in parallel with Docker
  • 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
        </Directory>
    
        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
    </VirtualHost>
    

    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.