How can I pass heroku config vars to docker build?
Asked Answered
D

1

6

I'm trying to deploy my rails app to the Heroku using heroku.yml. This is my yml setup

setup:
  addons:
    - plan: cleardb-mysql
      as: DATABASE
build:
  docker:
    web: Dockerfile
  config:
    RAILS_ENV: development
    DATABASE_URL: mysql2://abcdef:[email protected]/heroku_abcdefg123456
run:
  web: bin/rails server -p $PORT -b 0.0.0.0

and I'm using MySQL as a database. And here's my Dockerfile that the Heroku is using to build the image.

FROM ruby:2.6.5-alpine

ARG DATABASE_URL
ARG RAILS_ENV
# Adding the required dependencies
# Installing Required Gems
# Other Configs...
# Copying Gem Files
COPY Gemfile Gemfile.lock ./
# Installing Gems
RUN bundle install --jobs=4 --retry=9
# Copying package and package.lock
COPY package.json yarn.lock ./
# Installing node_modules
RUN yarn
# Copy everything to the from current dir to container_root
COPY . ./
#Compiling assets
RUN bundle exec rake assets:precompile # this precompilation step need DATABASE_URL
CMD ["rails", "server", "-b", "0.0.0.0"]

This is working as expected but the problem is I've to pass the database string in the heroku.yml file. Is there a way I can reference the config vars that are declared in the Heroku?

I tried this but it's not working as the docs also saying that the config-vars declared in Heroku are not available at the build time.

setup:
  addons:
    - plan: cleardb-mysql
      as: DATABASE
build:
  docker:
    web: Dockerfile
  config:
    RAILS_ENV: $RAILS_ENV
    DATABASE_URL: $DATABASE_URL
run:
  web: bin/rails server -p $PORT -b 0.0.0.0

What could be the possible workaround for this issue?

Davinadavine answered 2/7, 2020 at 11:39 Comment(0)
W
1

Without using the heroku.yml a possible solution is to include the env variable in the Dockerfile.
Example with Java code:

CMD java -Dserver.port=$PORT -DmongoDbUrl=$MONGODB_SRV  $JAVA_OPTS -jar /software/myjar.jar

You need to build the image locally, push and release it into the Heroku Registry: when it runs the Config Vars (ie MONGODB_SRV) are injected

Web answered 2/7, 2020 at 13:13 Comment(5)
Thank you but I want the Heroku to handle everything as soon as I push my code to the git.Davinadavine
I see. It looks like (according to doc) that you can config vars at provisioning time, using the setup.config section of the yml, something to try maybeWeb
hey @Davinadavine any luck with this? I am struggling with the same thing here.. I need to access a config variable in my Dockerfile. ThanksGregoriogregorius
@Gregoriogregorius hi! have you solved this? same issueMagical
no @AdilAkhmetov no luck with this...Gregoriogregorius

© 2022 - 2024 — McMap. All rights reserved.