0

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

Matt Matero
  • 119
  • 1
  • 10
  • `COPY` can only copy files from within the current directory tree (the _build context_); `COPY ../../Gemfile` will actually bring in the `Gemfile` from the _current_ directory and not the ancestor directory (if you have nested Ruby projects somehow). – David Maze Jul 22 '21 at 23:31
  • (It doesn't really make sense to `RUN rails console`, since this would happen non-interactively as part of the build process, but you'd probably have the same error if you set `CMD rails server` and actually launched the container.) – David Maze Jul 22 '21 at 23:32
  • I have my Dockerfile in a main_app/docker/app directory and the Gemfile is in main_app/Gemfile. Are you suggesting to move a copy of the Gemfile into docker/app ? Is there no standard way to point it towards the one in the app's root directory? – Matt Matero Jul 23 '21 at 00:30
  • The left-hand side of the `COPY` statement is relative to the `build: { context: }` directory, but can't go above that directory; if you `COPY Gemfile Gemfile.lock .` with the `context:` set to the `main_app` directory it should have the effect you want. – David Maze Jul 23 '21 at 00:56
  • Also: do you need to `bundle exec rails ...`? – David Maze Jul 23 '21 at 00:57
  • Thanks, that is helpful to know. I have updated my Dockerfile to just COPY Gemfile ./ This still leads to to the same rake not found error, when I try to run CMD bundle exec rake routes though. It looks like when I run docker-compose build that the bundle install is working but when i run any rails/rake commands this error occurs – Matt Matero Jul 23 '21 at 02:43

1 Answers1

0

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.

Matt Matero
  • 119
  • 1
  • 10