Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: rails executable file not found in $PATH

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:
like image 580
Aaron Lind Avatar asked Oct 27 '25 19:10

Aaron Lind


2 Answers

Please using bundle exec. It is work for me

like image 82
TuanHV Avatar answered Oct 30 '25 10:10

TuanHV


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.

like image 37
dzof31 Avatar answered Oct 30 '25 09:10

dzof31