gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:51:32 28 4
gpt4 key购买 nike

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

系统:

  • Ubuntu 18.04
  • ruby​​ 2.6.5p114(2019-10-01修订版67812)[x86_64-linux]
  • Bundler版本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

    最佳答案

    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变量来解决此问题。

    关于ruby-on-rails - 在Rails 6上部署Ruby-AWS Elastic Beanstalk-Docker:ArgumentError:缺少 `secret_key_base`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60327928/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com