简体   繁体   English

使用Docker将Elastic Beanstalk环境变量暴露给Laravel的工匠

[英]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. 我正在尝试使用Elastic Beanstalk首次将我的应用程序部署到生产环境,并且我已经使用我的Elastic beanstalk应用程序创建了一个RDS实例。 Since my application uses PHP5-FPM, I have to expose these environment variables inside Dockerfile like this: 由于我的应用程序使用PHP5-FPM,我必须在Dockerfile公开这些环境变量,如下所示:

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. 这很好用,我的PHP脚本可以访问我的RDS数据库。 However, I also need to migrate my database when I deploy, so I added this line to my Dockerfile : 但是,我在部署时还需要迁移我的数据库,所以我将此行添加到我的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. 这失败了,因为命令行上的PHP不存在RDS环境变量。 I've confirmed this by doing die(var_dump($_SERVER)); 我已经通过die(var_dump($_SERVER));确认了这一点die(var_dump($_SERVER)); at the top of the artisan script, and as I expected the RDS environment variables aren't there. artisan脚本的顶部,正如我所料,RDS环境变量不存在。

To try and get the environment variables to PHP CLI I tried doing this in my Dockerfile : 要尝试将环境变量提供给PHP CLI,我尝试在Dockerfile执行此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? 如何让PHP命令行解释器访问我的RDS环境变量?

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. 我能想到的唯一另一个选择是在我的应用程序配置中硬编码我的RDS凭据,我确信你理解这是我不想做的事情。

You can set environment variables in the config of the beanstalk application. 您可以在beanstalk应用程序的配置中设置环境变量。 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. 我相信您也可以在.elasticbeanstalk / config.yml中执行此操作,但我通常只是从Web界面执行此操作。

Most likely the mod_env doesn't pass those variables to your application. mod_env很可能不会将这些变量传递给您的应用程序。 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: /var/apache2/sites-enabled/000-default.conf或其他负责您的应用程序的文件中,您需要指定应传递的所有环境参数,如下例所示:

<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. 如您所见 - 最重要的是PassEnv命令。

You can create a file called 000-default.conf and put it into a docker folder in your project. 您可以创建一个名为000-default.conf的文件,并将其放入项目中的docker文件夹中。 Then add to the Dockerfile 然后添加到Dockerfile

ADD docker/000-default.conf /etc/apache2/sites-available/000-default.conf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Elastic Beanstalk-Laravel Artisan命令 - AWS Elastic Beanstalk - Laravel Artisan Command Elastic Beanstalk从API部署docker环境 - Elastic Beanstalk deploy a docker environment from the API 使用Docker的开发环境并部署到AWS Elastic Beanstalk - Development environment with Docker and deploying to AWS Elastic Beanstalk 来自 Amazon Linux 2(Elastic Beanstalk)的 Laravel 工匠修补匠 - Laravel artisan tinker from Amazon Linux 2 (Elastic Beanstalk) 如何在 AWS Elastic Beanstalk 上执行 Laravel Artisan 迁移? - How do I perform Laravel Artisan migrations on AWS Elastic Beanstalk? Github repo 如何在没有.env 文件的情况下使用 AWS laravel 弹性 beanstalk 环境变量 - How Github repo can use AWS laravel elastic beanstalk environment variables without .env file Laravel Artisan - Cli 不读取环境变量 - Laravel Artisan - Cli doesnt reads environment variables 如何在Elastic Beanstalk Multicontainer Docker Environment上安装composer依赖项 - How to install composer dependencies on Elastic Beanstalk Multicontainer Docker Environment Symfony3不读取Elastic Beanstalk上的环境变量 - Symfony3 does not read the environment variables on Elastic Beanstalk 将Elastic Beanstalk PHP App环境变量传递给Javascript - Passing Elastic Beanstalk PHP App Environment Variables to Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM