Search code examples
ruby-on-railsdockerherokudockerfile

Deploy Rails app into Heroku with Docker using heroku.yml


After following the tutorial at: https://devcenter.heroku.com/articles/build-docker-images-heroku-yml it was not clear which contents we should put in the Dockerfile.

For a Rails app what do we need in our Dockerfile?


Solution

  • There is a Rails 5 example that you can base your solution: https://github.com/jahangiranwari/rails5-docker-heroku/

    For a Rails 6 project I've used the following Dockerfile:

    FROM ruby:2.6.6
    
    # Install node & yarn
    RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
    RUN apt-get install -y nodejs
    RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
    RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
    RUN apt-get update && apt-get install -y yarn
    
    # Install base deps or additional (e.g. tesseract)
    ARG INSTALL_DEPENDENCIES
    RUN apt-get update -qq \
      && apt-get install -y --no-install-recommends ${INSTALL_DEPENDENCIES} \
        build-essential libpq-dev git \
      && apt-get clean autoclean \
      && apt-get autoremove -y \
      && rm -rf \
        /var/lib/apt \
        /var/lib/dpkg \
        /var/lib/cache \
        /var/lib/log
    
    # Install deps with bundler
    RUN mkdir /app
    WORKDIR /app
    COPY Gemfile* /app/
    ARG BUNDLE_INSTALL_ARGS
    RUN gem install bundler:2.1.4
    RUN bundle config set without 'development test'
    RUN bundle install ${BUNDLE_INSTALL_ARGS} \
      && rm -rf /usr/local/bundle/cache/* \
      && find /usr/local/bundle/gems/ -name "*.c" -delete \
      && find /usr/local/bundle/gems/ -name "*.o" -delete
    COPY . /app/
    
    # Compile assets
    ARG RAILS_ENV=development
    RUN if [ "$RAILS_ENV" = "production" ]; then SECRET_KEY_BASE=$(rake secret) bundle exec rake assets:precompile; fi
    
    

    And this heroku.yml file:

    build:
      docker:
        web: Dockerfile
      config:
        BUNDLE_INSTALL_ARGS: --jobs 10 --retry=3
        RAILS_ENV: production
        # Put extra deps here
        INSTALL_DEPENDENCIES: curl openssh-server python
    run:
      web: bundle exec puma -C config/puma.rb
    

    Update 20-1-22: It works for Rails 7