Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose-build with rake assets:precompile

Im trying to set up my app to run in production mode and Ive hit a problem with building the assets folder specifically this line in my Dockerfile:

RUN bundle exec rake RAILS_ENV=production DATABASE_URL=postgresql://user:[email protected]/dbname SECRET_TOKEN=dummytoken assets:precompile

The database is just a dummy line. The problem is when it runs the rake it seems to not see the env variables and I get the following errors when it goes to initialize 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 a few more lines to the error but the first line says it all and the ENV variables for the aws_access_key_id and aws_secret_access_key dont seem to load up. If I run this without trying to precompile the assets everything works but I need to precompile the assets to make them visible to nginx

I did find that for a fix I could replace the ENV variables with what they are, this is the code that is having the trouble:

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 isnt a good solution long term to have the keys programmed in.

like image 375
DRing Avatar asked Jan 21 '26 18:01

DRing


1 Answers

Your question has shifted a bit, so I'm going to address your last sentence:

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.

If you're sure you want to handle this during your build, as opposed to adding your rake task to your command entry, you can set build args in your docker-compose.yml config file.

# 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

You can then declare and use the build args in your 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
like image 142
pdoherty926 Avatar answered Jan 23 '26 10:01

pdoherty926



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!