gpt4 book ai didi

ruby-on-rails - Docker compose-build 与 rake Assets :precompile

转载 作者:行者123 更新时间:2023-12-04 03:44:09 35 4
gpt4 key购买 nike

我试图将我的应用程序设置为在生产模式下运行,但我在构建 Assets 文件夹时遇到了问题,特别是在我的 Dockerfile 中的这一行:

RUN bundle exec rake RAILS_ENV=production DATABASE_URL=postgresql://user:pass@127.0.0.1/dbname SECRET_TOKEN=dummytoken assets:precompile

数据库只是一个虚拟线。问题是当它运行 rake 时,它​​似乎没有看到 env 变量,并且在它初始化carrierwave.rb 时出现以下错误
rake aborted!
ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/core/service.rb:244:in `validate_options'
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/core/service.rb:268:in `handle_settings'
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/core/service.rb:98:in `new'
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/core/services_mixin.rb:16:in `new'
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/storage.rb:27:in `new'
/usr/local/bundle/gems/carrierwave-0.11.2/lib/carrierwave/uploader/configuration.rb:83:in `eager_load_fog'
/usr/local/bundle/gems/carrierwave-0.11.2/lib/carrierwave/uploader/configuration.rb:96:in `fog_credentials='
/mnt/hgfs/Projects/livingrecipe/config/initializers/carrierwave.rb:3:in `block in <top (required)>'
/usr/local/bundle/gems/carrierwave-0.11.2/lib/carrierwave/uploader/configuration.rb:118:in `configure'
/usr/local/bundle/gems/carrierwave-0.11.2/lib/carrierwave.rb:14:in `configure'
/mnt/hgfs/Projects/livingrecipe/config/initializers/carrierwave.rb:1:in `<top (required)>'

错误还有几行,但第一行说明了一切,aws_access_key_id 和 aws_secret_access_key 的 ENV 变量似乎没有加载。如果我在不尝试预编译 Assets 的情况下运行它,一切正常,但我需要预编译 Assets 以使它们对 nginx 可见

我确实发现为了修复我可以用它们替换 ENV 变量,这是有问题的代码:
CarrierWave.configure do |config|

config.fog_credentials = {
provider: 'AWS', # required
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], # required
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], # required
region: 'us-west-2' # optional, defaults to 'us-east-1'
}
config.fog_directory = ENV['S3_BUCKET_NAME'] # required
#config.fog_host = 'https://assets.example.com' # optional, defaults to nil
#config.fog_public = false # optional, defaults to true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end

所以我只需要输入键就可以使用它......但显然这不是一个很好的长期解决方案,让键被编程。

最佳答案

你的问题有点变化,所以我要解决你的最后一句话:

So I got it to work with just typing in the keys...but obviously that isn't a good solution long term to have the keys programmed in.



如果您确定要在构建期间处理此问题,而不是将您的 rake 任务添加到您的 command条目,您可以设置 build args在您的 docker-compose.yml 配置文件中。
# compose.yml
version: '2'
services:
app:
# ...
build:
context: .
args:
# This will make your variables available during the
# "build" phase.
# You can hardcode these values here, or better,
# add them to a .env file, whose contents
# Docker/Compose will make available during the build.
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- DATABASE_URL
- SECRET_TOKEN
environment:
# You should also add these values to your application's
# environment.
# You can hardcode these values here, or better,
# add them to a .env file, whose contents
# Docker/Compose will make available to your running container.
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- DATABASE_URL
- SECRET_TOKEN

然后,您可以在 Dockerfile 中声明和使用构建参数:
# Dockerfile
# ...
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY
ARG DATABASE_URL
ARG SECRET_TOKEN
# these values will now be available to your rake task
# in ENV['AWS_ACCESS_KEY_ID'], etc.
RUN bundle exec rake RAILS_ENV=production assets:precompile

关于ruby-on-rails - Docker compose-build 与 rake Assets :precompile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441674/

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