简体   繁体   中英

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 :

# 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM