I'm trying to create a docker file for Rails that will allow the container to handle all requirements such as the Ruby version. I have a Rails project that has the Gemfile and Gemfile.lock where only Rails is defined (5.0.0.1). My Dockerfile and Compose file are below. In addition to having a standard method to start new projects I want the gems to be cached to speed up development. To this end it appears I need to move the bundle install to the Compose file. But now if I try and generate the Rails app I get the error rails executable file not found in $PATH:
docker-compose run web rails new . --force --database-postgresl --skip-bundle
What do I need to do to resolve this issue? When I was working on caching gems with an existing project scenario adding the binaries to the PATH seemed to resolve the issue in that case but not sure to handle things from a from scratch perspective.
#ENV HOME=/usr/src/app PATH=/usr/src/app/bin:$PATH
Dockerfile
FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev 
nodejs
RUN mkdir /docknewdev22C
WORKDIR /docknewdev22C
ADD Gemfile /docknewdev22C
ADD Gemfile.lock /docknewdev22C
ENV BUNDLE_PATH /gems
#add the app's binaries path to $PATH:
ENV HOME=/docknewdev22C PATH=/docknewdev22C/bin:$PATH
#RUN bundle install (NOW IN COMPOSE FILE)
ADD . /docknewdev22C
DOCKER-COMPOSE.yml
version: '2.1'
services:
   db:
   image: postgres
   volumes:
     - postgres-data:/var/lib/postgresql/data
web:
  build: .
  #added bash command as server.pid was persisting so multiple runs would 
  complain about existing server.
  command: bash -c "bundle install && rm -f tmp/pids/server.pid && bundle 
  exec rails s -p 3000 -b '0.0.0.0'"
  #mount the folder of the project from the local workstation.
  volumes:
    - .:/docknewdev22C
    - gem_cache:/gems
  ports:
    - "3000:3000"
  depends_on:
    - db
volumes:
  postgres-data:
    driver: local
  gem_cache:
Please using bundle exec. It is work for me
Try this up:
Dockerfile
FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev 
nodejs
RUN gem install bundler -v 1.11.2 --no-ri --no-rdoc
RUN mkdir /src
WORKDIR /src
COPY Gemfile Gemfile.lock ./
RUN bundle install
# Copy the main application.
COPY . ./
CMD ./start-dev.sh
start-dev.sh
rm /src/tmp/pids/server.pid
bundle exec rails s -p 3000 -b '0.0.0.0'
This way, you wont need the field "command" in your docker-compose only if you want to override. Each line of your dockerfile will create a layer, one of them is about installing gems RUN bundle install so if you dont update lines ahead of it, you will build your image from cache each time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With