Having issues running bundle install with docker container

I have an existing Rails application that I am just trying to turn into a docker instance. I’ve went through a few tutorials on dockerizing an existing rails app, but I keep getting stuck somewhere.

Here’s my Dockerfile:

FROM ruby:2.5.1
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

I confirmed that my Gemfile.lock file in the local directory is empty.

Finally, here’s my docker-compose.yml file:

version: '3'
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - ./:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

After running docker-compose build, it goes through the whole process of pulling down Ruby, postgresql, doing bundle install, etc… and then finally when I try to run docker-compose up, I get the following error:

web_1  | Bundler::GitError: The git source https://github.com/zdennis/activerecord-import is not yet checked out. Please run `bundle install` before trying to start your application
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git/git_proxy.rb:235:in `allowed_in_path'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git/git_proxy.rb:192:in `find_local_revision'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git/git_proxy.rb:64:in `revision'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git.rb:225:in `revision'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git.rb:93:in `install_path'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/path.rb:126:in `expanded_path'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/path.rb:163:in `load_spec_files'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git.rb:200:in `load_spec_files'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/path.rb:100:in `local_specs'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git.rb:167:in `specs'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:759:in `block in converge_locked_specs'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:745:in `each'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:745:in `converge_locked_specs'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:248:in `resolve'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:171:in `specs'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:238:in `specs_for'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:227:in `requested_specs'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/runtime.rb:108:in `block in definition_method'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/runtime.rb:20:in `setup'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler.rb:107:in `setup'
web_1  |   /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/setup.rb:20:in `<top (required)>'
web_1  |   /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
web_1  |   /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
web_1  | bundler: failed to load command: rails (/usr/local/bundle/bin/rails)

What am I missing here? I would like to use an existing volume as myapp (not sure if there’s any reason I shouldn’t want to do this — perhaps it runs faster if it’s within the container itself?)

The only way I can get this to work properly is if I run docker-compose build, followed by docker-compose run web bundle install, followed by docker-compose up

Am I doing something wrong that’s requiring me to have to run docker-compose run web bundle install? I saw it run when I ran docker-compose build, so not sure why it’s required again.

Source: StackOverflow