Search code examples
ruby-on-railsrubydockerrubygemsbundler

Dockerizing Existing Rails Application


I've been trying to move my rails app into a docker container, as it used to be ansible managed in a virtualbox for dev env. I've looked at a few docker tutorials and a couple specific to migrating rails applications but I am running into the following error:

 => ERROR [10/10] RUN rails c                                                                                          0.9s
------
 > [10/10] RUN rails c:
#15 0.878 /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/spec_set.rb:91:in `block in materialize': Could not find rake-13.0.1 in any of the sources (Bundler::GemNotFound)
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `map!'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/spec_set.rb:85:in `materialize'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/definition.rb:170:in `specs'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/definition.rb:237:in `specs_for'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/definition.rb:226:in `requested_specs'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/runtime.rb:108:in `block in definition_method'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/runtime.rb:20:in `setup'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler.rb:107:in `setup'
#15 0.879   from /usr/local/bundle/gems/bundler-1.17.3/lib/bundler/setup.rb:20:in `<top (required)>'
#15 0.879   from /usr/local/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
#15 0.879   from /usr/local/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
#15 0.879   from /usr/local/bundle/bin/rails:27:in `<main>'

Here is the simplified dockerfile I am trying to run via docker-compose. I originally had it connected to my MySQL image but have removed that to help debug, however the error still persists.

Dockerfile

FROM ruby:2.4.0


RUN apt-get update -yqq \
    && apt-get install -yqq --no-install-recommends \
    build-essential \
    mysql-client \
    nodejs \
    supervisor \
    && apt-get clean -q clean \
    && rm -rf /var/lib/apt/lists


RUN mkdir -p /my_api
ENV RAILS_ROOT /my_api
WORKDIR $RAILS_ROOT

COPY ../../Gemfile ./
RUN gem install bundler --no-ri --no-rdoc --version 1.17.3
RUN bundle install 
COPY . ./

RUN gem list
RUN rails c

DockerCompose

version: "3.7"

services:
  api:
    image: my_api
    build:
      context: ./
      dockerfile: ./docker/app/dockerfile
    ports: 
      - 3000:3000
    working_dir: /api

I've tried a few solutions in the following SO threads but nothing seems to remove the error: Specifying path to vendor/cache and installing rake before running bundle install


Solution

  • Putting my solution here just in case someone comes across this:

    First I updated my dockerfile to do the following

    FROM ruby:2.4.0
    
    
    RUN apt-get update -yqq \
        && apt-get install -yqq --no-install-recommends \
        build-essential \
        mysql-client \
        nodejs \
        supervisor \
        vim \
        lsof \
        htop \
        && apt-get clean -q clean \
        && rm -rf /var/lib/apt/lists
    
    
    RUN mkdir -p /my_api
    ENV RAILS_ROOT /my_api
    WORKDIR $RAILS_ROOT
    
    COPY Gemfile ./
    COPY . ./
    COPY ./railsbox/ansible/group_vars/all/.env ./.env
    RUN gem install bundler  --no-ri --no-rdoc --version 1.17.3
    RUN gem install rake --version 13.0.1 && bundle install --path vendor/cache --no-deployment
    
    RUN gem list
    CMD rm -f ./tmp/pids/server.pid && bundle exec rails s -b 0.0.0.0
    

    note the change of COPY . ./. before running bundle install. This will avoid overwriting /vendor/cache after installs are run (I believe).

    I also updated my docker-compose setup as follows

    api:
        image: my_api
        build:
          context: .
          dockerfile: ./docker/app/dockerfile
        depends_on:
          - mysql
          - mysql_test
        ports: 
          - "3000:3000"
        working_dir: /my_api
        volumes:
          - ./:/my_api
          - /my_api/vendor/cache
        links: 
          - mysql
          - mysql_test
    

    Note the blank volume mapping for /vendor/cache as suggested in this post which explains this with respect to node_modules. I believe with this change you do not need to do the above suggestion of moving COPY . ./ before the bundle install, but I left it as is since I don't think it really matters.