在Rails 6上部署Ruby-AWS Elastic Beanstalk-Docker:ArgumentError:缺少`secret_key_base`

问题描述 投票:0回答:1

我似乎无法找出为Ruby on Rails 6设置secret_key_base的正确方法-AWS Elastic Beanstalk-Docker部署。因此,部署不断失败。我一直在尝试遵循本教程:https://dev.to/fdoxyz/elastic-beanstalk-apps-using-docker-containers-56l8

系统:

  • Ubuntu 18.04
  • 红宝石2.6.5p114(2019-10-01修订版67812)[x86_64-linux]
  • 捆绑器版本2.1.4
  • Rails 6.0.2.1
  • Docker版本18.09.7,内部版本2d0083d
  • 节点v12.16.1

这是我从空目录执行的步骤:

mkdir new_project && cd new_project
eb init
     2) us-west-1 : US West (N. California)
     2) [ Create new Application ]
     (default is "new_project")
     8) Docker
     Do you want to set up SSH for your instances? Y
     Select a keypair.

eb create
     Enter Environment Name (default is new-project-dev)
     Enter DNS CNAME prefix (default is new-project-dev)
     Select a load balancer type: 2) application
     enable Spot Fleet? n
     download the sample application into the current directory? n

eb setenv SECRET_KEY_BASE=$(ruby -e "require 'securerandom';puts SecureRandom.hex(64)")
eb setenv RAILS_ENV=production

cat .gitignore
rails new .
vim .gitignore (paste old contents of gitignore)
touch Dockerfile
vim Dockerfile

===============
FROM ruby:2.6.5

# Install NodeJS & Yarn
RUN apt-get update && \
    apt-get install apt-transport-https && \
    curl -sL https://deb.nodesource.com/setup_12.x | bash - && \
    apt-get purge nodejs && \
    apt-get update && \
    apt-get install nodejs -y && \
    npm install yarn -g  && \
    gem install bundler -v 2.1.4

# Workdir and add dependencies
WORKDIR /app/
ADD Gemfile Gemfile.lock /app/

# Throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1

# Install dependencies
ARG RAILS_MASTER_KEY
ENV RAILS_ENV=production NODE_ENV=production RAILS_SERVE_STATIC_FILES=1
RUN bundle install --without development test

# Add the app code, precompile assets and use non-root user
ADD . /app/
RUN rake assets:precompile DISABLE_SPRING=1 && \
    chown -R nobody:nogroup /app
USER nobody
ENV HOME /app

# Make sure to explicitly bind to port & interface
CMD ["bundle", "exec", "rails s -p 3000 -b 0.0.0.0"]
===============

vim config/environments/production.rb
insert at the top of the file:
    config.secret_key_base = ENV["SECRET_KEY_BASE"]

git add . && git commit -m "Initial commit"
eb use new_project-dev
eb deploy

这里是从ssh-ing到'/var/log/eb-activity.log'的实例的完整日志:

https://raw.githubusercontent.com/maxtocarev/eb-log/master/eb-activity.log

ruby-on-rails docker amazon-elastic-beanstalk
1个回答
0
投票

secret_key_base是存储在Rails encrypted credentials中的值。似乎在构建Docker映像时,来自Dockerfile内部的命令rake assets:precompile失败,因为它需要secret_key_base值。我相信这是因为您的本地项目中没有config/master.key。我建议使用类似以下的方法将其传递给docker build

docker build  --build-arg RAILS_MASTER_KEY=${RAILS_MASTER_KEY} ...

出于安全原因,我绝对不建议在项目本身中包含config/master.key,这就是为什么我要使用ENV变量的原因。

在这种情况下,您似乎正在使用Elastic Beanstalk的“自动构建”(这意味着Docker映像是在源代码的每次部署中构建的),因此您无需手动构建映像。可以通过使用RAILS_MASTER_KEY或从AWS Web控制台添加eb setenv RAILS_MASTER_KEY=XXXXXXXX env变量来解决此问题。

© www.soinside.com 2019 - 2024. All rights reserved.