ruby-on-rails - Rails 6 rake Assets :precompile failing with Docker on AWS due to missing Master Key Env variable

标签 ruby-on-rails docker amazon-elastic-beanstalk ruby-on-rails-6

我有一个 Rails 6 应用程序,其凭据在本地 Docker 上运行。
为了在 AWS ElasticBeanstalk 上启动并运行它,我想将 Assets 预编译为 Docker 构建的一部分。然而,正如 this issue in the rails repo 中所述,我在 rake assets:precompile 上收到以下错误:
Missing encryption key to decrypt file with. Ask your team for your master key and write it to config/master.key or put it in the ENV['RAILS_MASTER_KEY'].
我添加了 RAILS_MASTER_KEY作为 EB 环境中的环境变量(配置 > 软件 > 修改),但在 Docker 构建期间,它不会从那里读取 Env 变量。

允许 Docker 构建访问环境变量的最佳和最安全的方法是什么?我应该运行我的assets:precompile在部署之前?

在本地,我可以通过将 key 作为构建参数传递来在生产模式下预编译 Assets (请参阅 Dockerfile 中我解析 RAILS_MASTER_KEY 参数并设置它的部分),但是,这似乎不是生产的正确选择?或者如果是,我应该怎么做?

我的 Dockerfile:

FROM ruby:2.6.3-slim
ARG RAILS_MASTER_KEY

bla bla install yarn etc

RUN gem install bundler
#Install gems
RUN mkdir /gems
WORKDIR /gems
COPY Gemfile .
COPY Gemfile.lock .
RUN bundle install

ARG INSTALL_PATH=/opt/my_app
ENV INSTALL_PATH $INSTALL_PATH
ENV RAILS_MASTER_KEY $RAILS_MASTER_KEY
WORKDIR $INSTALL_PATH
COPY . .

RUN RAILS_ENV=production rake assets:precompile

最佳答案

您添加的主 key 可能不正确。检查 config/master.key 中的主 key (在本地)。本地和 aws 中的主 key 必须相同。

此外,请确保在 AWS 中添加主 key 。进入控制台打印主 key ENV['RAILS_MASTER_KEY'] .

关于ruby-on-rails - Rails 6 rake Assets :precompile failing with Docker on AWS due to missing Master Key Env variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58202309/

相关文章:

docker - 创建运行IBM Containers Extension(ICE)的容器

docker - 发送代理在 ssl-passthrough haproxy 上失败

postgresql - Drupal 与 Docker 中的 Postgres : throws exception after install

amazon-web-services - Docker 容器无法在使用 AWS ECR 的 AWS ECS 中工作

amazon-web-services - Docker nfs4 安装在 Elastic Beanstalk 上

ruby-on-rails - 将数据从哈希保存到 YAML 文件

ruby-on-rails - 当 :allow_destroy => true 时删除关联而不是销毁对象

ruby-on-rails - 使用 bundler 安装 gem 时传递安装标志

ruby-on-rails - AWS Elastic Beanstalk 无法担任角色

ruby-on-rails - 为什么我不需要使用 Rails binstub 来运行正确的版本?