我试图将我的应用设置为在生产模式下运行,并且我在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变量似乎没有加载。如果我在没有尝试预编译资产的情况下运行它,一切正常,但我需要预先编译资产以使它们对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
所以我只需输入键即可使用......但很明显,长期使用这些键不是很好的解决方案。
答案 0 :(得分:0)
你的问题有所改变,所以我要解决你的最后一句话:
所以我只需输入键即可使用......但很明显,长期使用这些键不是很好的解决方案。
如果您确定要在构建期间处理此问题,而不是将rake任务添加到command
条目,则可以在您的# 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
条目中设置build args docker-compose.yml配置文件。
# 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
然后,您可以在Dockerfile中声明并使用build args:
from sqlite3 import *
from webbrowser import *
#retrieve a text file
while inc != len(categories):
file_open = open(categories[inc]+'.txt','U')
#assign the category
category = categories[inc]
#run the loop to populate the table popularity
for each_ln in file_open:
#assign the variables.
tab_index = each_ln.find('\t')
PersonNumber = each_ln[0:tab_index]
value = each_ln[tab_index + 1:len(each_ln)]
#populate the database.
TH2_db.execute("INSERT INTO popularity VALUES (?,?,?)" \
, (PersonNumber, category, value))
#increment to move onto the next textfile
inc = inc + 1