Docker与rake资源组合 – 构build – 预编译

我试图设置我的应用程序在生产模式下运行,我遇到了一个问题,build立资产文件夹,特别是我的Dockerfile中的这一行:

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

数据库只是一个虚拟的行。 问题是,当它运行耙子似乎没有看到envvariables,并得到以下错误时,它初始化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)>' 

Theres多了几条线,但第一行说这一切和ENVvariables的aws_access_key_id和aws_secret_access_key似乎不会加载。 如果我运行这个没有试图预编译资产一切正常,但我需要预编译的资产,使其可见的nginx

我确实发现,为了解决这个问题,我可以用它们replaceENVvariables,这是有问题的代码:

 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 

所以,我只是在键入键入它的工作…但显然,这是一个很好的解决scheme,长期来把钥匙编程。

你的问题已经转移了一点,所以我要解释你的最后一句话:

所以,我只是在键入键入工作…但显然这是不是一个好的解决scheme,长期来说,按键编程。

如果您确定要在构build过程中处理这个问题,而不是将您的rake任务添加到您的command条目中,则可以在docker-compose.ymlconfiguration文件中设置构build参数 。

 # 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中声明和使用构build参数:

 # 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