Docker & Rails | bundler: command not found: rails

14,455

Solution 1

I am not sure if this is a proper solution, but I tried adding

RUN bundler install

to my Dockerfile. I deleted all of the images, ran docker-compose up and worked like a charm.

Solution 2

Another possible solution that worked for me is to create a docker-entrypoint.sh file in the root of your project similar to this:

#!/bin/bash
bundle install --jobs 20 --retry 5

and add this steps in your Dockerfile

# allows a shell script to run before any relative containers execute a command.
COPY ./docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

This way you have the possibility to run this shellscript file around the initialization of your container. You can read more about this here: https://success.docker.com/article/use-a-script-to-initialize-stateful-container-data

Share:
14,455
Fares K. A.
Author by

Fares K. A.

A computer science student with a passion for programming, hackathons and pizza. http://faresalaboud.me

Updated on June 25, 2022

Comments

  • Fares K. A.
    Fares K. A. almost 2 years

    I'm brand new to Docker and fairly new to Rails.

    When I run docker-compose up, I get this error:

    web_1  | bundler: command not found: rails
    web_1  | Install missing gem executables with `bundle install`
    app_web_1 exited with code 127
    

    Initially I had trouble getting it to find any gem when running bundle install. I was getting a "Could not find gem in any of the sources" error for every single Gem in my Gemfile. I then attempted to run bundle (without install) and it returned no errors.

    Rails is definitely installed, as typing rails gives me all of it's usage options.

    Also, I can see the gems in vendor/cache. I tried removing all of them and running bundle install and they all start appearing one by one as the bundler installs them.

    EDIT: Please feel free to correct my understanding: since running bundle install is meant to install the gems on the Docker instance, it is not being installed "locally". While attempting to troubleshoot this issue, I deleted all my gems locally and re-installed bundler. So, when I run gems list, I only see bundler. I ran bundler install and it claims that all the gems are being installed. Where are they being installed? It says it's going to vendor/cache, and that's true. I can see them there, yet it still fails to find any gems: it won't even find rails.

    EDIT 2: I should also mention: bin/rails s starts the server just fine. It won't load anything due to the database instance not running, but that makes sense I guess.

    EDIT 3: I ran gem install rails locally, still no change but now gems list returns rails and its dependencies.

    Below are some files and logs that may be relevant below and could help determine the cause. Please note I changed the actual application name to app.

    Running gems list returns this:

    *** LOCAL GEMS ***
    
    actionmailer (5.1.1)
    actionpack (5.1.1)
    actionview (5.1.1)
    activejob (5.1.1)
    activemodel (5.1.1)
    activerecord (5.1.1)
    activesupport (5.1.1)
    arel (8.0.0)
    builder (3.2.3)
    bundler (1.15.0)
    concurrent-ruby (1.0.5)
    erubi (1.6.0)
    globalid (0.4.0)
    i18n (0.8.1)
    loofah (2.0.3)
    mail (2.6.5)
    method_source (0.8.2)
    mime-types (3.1)
    mime-types-data (3.2016.0521)
    mini_portile2 (2.1.0)
    minitest (5.10.2)
    nio4r (2.0.0)
    nokogiri (1.7.2)
    rack (2.0.3)
    rack-test (0.6.3)
    rails (5.1.1)
    rails-dom-testing (2.0.3)
    rails-html-sanitizer (1.0.3)
    railties (5.1.1)
    rake (12.0.0)
    sprockets (3.7.1)
    sprockets-rails (3.2.0)
    thor (0.19.4)
    thread_safe (0.3.6)
    tzinfo (1.2.3)
    websocket-driver (0.6.5)
    websocket-extensions (0.1.2)
    

    My Gemfile looks like this:

    source 'https://rubygems.org'
    
    git_source(:github) do |repo_name|
      repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
      "https://github.com/#{repo_name}.git"
    end
    
    gem 'gmail'
    
    gem 'oauth', '0.5.2'
    
    gem 'twilio'
    
    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
    gem 'rails', '~> 5.0.1'
    # Use postgres as the database for Active Record
    gem 'pg'
    # Use Puma as the app server
    gem 'puma', '~> 3.0'
    # Use SCSS for stylesheets
    gem 'sass-rails', '~> 5.0'
    # Use Uglifier as compressor for JavaScript assets
    gem 'uglifier', '>= 1.3.0'
    # Use CoffeeScript for .coffee assets and views
    gem 'coffee-rails', '~> 4.2'
    # See https://github.com/rails/execjs#readme for more supported runtimes
    # gem 'therubyracer', platforms: :ruby
    
    # Use jquery as the JavaScript library
    gem 'jquery-rails'
    # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
    gem 'turbolinks', '~> 5'
    # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
    gem 'jbuilder', '~> 2.5'
    # Use Redis adapter to run Action Cable in production
    # gem 'redis', '~> 3.0'
    # Use ActiveModel has_secure_password
    # gem 'bcrypt', '~> 3.1.7'
    
    # Use Capistrano for deployment
    # gem 'capistrano-rails', group: :development
    
    group :development, :test do
      # Call 'byebug' anywhere in the code to stop execution and get a debugger console
      gem 'byebug', platform: :mri
    end
    
    group :development do
      # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
      gem 'web-console', '>= 3.3.0'
      gem 'listen', '~> 3.0.5'
      # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
      gem 'spring'
      gem 'spring-watcher-listen', '~> 2.0.0'
    end
    
    # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
    

    My docker-compose.yml file looks like this:

    version: '2'
    services:
      db:
        image: postgres
        volumes:
          - ./postgres-data:/var/lib/postgresql/data
      web:
        build: .
        command: bundle exec rails s -p 3000 -b '0.0.0.0'
        volumes:
          - .:/app
        ports:
          - "3000:3000"
        depends_on:
          - db
    

    My Dockerfile looks like this:

    FROM ruby:2.4.0
    RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
    RUN mkdir /app
    WORKDIR /app
    ADD Gemfile /app/Gemfile
    ADD Gemfile.lock /app/Gemfile.lock
    ADD . /app
    

    Running bundle install returns this:

    Using rake 12.0.0
    Using oauth 0.5.2
    Using mime-types-data 3.2016.0521
    Using builder 3.2.3
    Using multi_xml 0.6.0
    Using concurrent-ruby 1.0.5
    Using i18n 0.8.1
    Using minitest 5.10.2
    Using thread_safe 0.3.6
    Using erubis 2.7.0
    Using mini_portile2 2.1.0
    Using rack 2.0.3
    Using nio4r 2.0.0
    Using websocket-extensions 0.1.2
    Using arel 7.1.4
    Using method_source 0.8.2
    Using thor 0.19.4
    Using bundler 1.15.0
    Using pg 0.20.0
    Using puma 3.8.2
    Using sass 3.4.24
    Using tilt 2.0.7
    Using execjs 2.7.0
    Using coffee-script-source 1.12.2
    Using turbolinks-source 5.0.3
    Using multi_json 1.12.1
    Using byebug 9.0.6
    Using bindex 0.5.0
    Using rb-fsevent 0.9.8
    Using ffi 1.9.18
    Using gmail_xoauth 0.4.2
    Using mime-types 3.1
    Using httparty 0.15.5
    Using tzinfo 1.2.3
    Using nokogiri 1.7.2
    Using rack-test 0.6.3
    Using sprockets 3.7.1
    Using websocket-driver 0.6.5
    Using uglifier 3.2.0
    Using coffee-script 2.4.1
    Using turbolinks 5.0.1
    Using rb-inotify 0.9.8
    Using mail 2.6.5
    Using twilio 3.1.1
    Using activesupport 5.0.3
    Using loofah 2.0.3
    Using listen 3.0.8
    Using gmail 0.6.0
    Using rails-dom-testing 2.0.3
    Using globalid 0.4.0
    Using activemodel 5.0.3
    Using jbuilder 2.6.4
    Using spring 2.0.1
    Using rails-html-sanitizer 1.0.3
    Using activejob 5.0.3
    Using activerecord 5.0.3
    Using spring-watcher-listen 2.0.1
    Using actionview 5.0.3
    Using actionpack 5.0.3
    Using actioncable 5.0.3
    Using actionmailer 5.0.3
    Using railties 5.0.3
    Using sprockets-rails 3.2.0
    Using coffee-rails 4.2.1
    Using jquery-rails 4.3.1
    Using web-console 3.5.1
    Using rails 5.0.3
    Using sass-rails 5.0.6
    Updating files in vendor/cache
    Bundle complete! 18 Gemfile dependencies, 68 gems now installed.
    Bundled gems are installed into ./vendor/cache.
    
  • Mano Marks
    Mano Marks almost 7 years
    This is the proper solution. Basically bundler isn't installed by default on the ruby image.