简体   繁体   中英

Why does bundler exec fail to load the rails command in my Docker entrypoint?

I created a Docker image and container via docker-compose that consists of a Rails app backed by PostgreSQL. Rails is meant to start via a docker-entrypoint.sh file which looks like this:

#!/bin/sh

# Exit early if there are any errors.
set -e

# Check that there won't be any server conflicts.
if [ -f tmp/pids/server.pid ]; then
  rm tmp/pids/server.pid
fi

# -b binds the server to all IP addresses, rather than to localhost.
bundle exec rails s -b 0.0.0.0

However, the container stops immediately after startup with this error:

bundler: failed to load command: rails (/usr/local/bundle/bin/rails)
LoadError: cannot load such file -- /usr/local/bundle/specifications/exe/rails
  /usr/local/bundle/bin/rails:23:in `load'
  /usr/local/bundle/bin/rails:23:in `<top (required)>'

Since to my knowledge you can't directly explore the filesystem of a stopped container, I exported the entire filesystem to a .tar file:

docker export a2410e604db9 > container.tar

Looking inside of container.tar , I found that there is a /usr/local/bundle/bin/rails executable file, so I don't know why bundle wouldn't be able to load that command.

The other directory that is mentioned in the error, /usr/local/bundle/specifications/exe/rails , does not exist. There is only /usr/local/bundle/specifications , which contains a bunch of .gemspec files for the Rails project and nothing else.

What is causing this bundler / LoadError problem?

What docker image are you using for your rails app? If it's the public library/ruby one then I presume all the required env variables will be set correctly for your script to work.

I suggest sharing your Dockerfile if it isn't the public ruby one.

First see if your PATH (and script) are using the intended bundle and rails:

docker run -it <YOUR_DOCKER_IMAGE> bash
which bundle
which rails

If not, try running it manually first before scripting to diagnose issues:

docker run -it <YOUR_DOCKER_IMAGE> bash
cd /path/to/app
bundle exec rails s -b 0.0.0.0

Lastly, ensure your docker-compose.yml file exposes the port you're expecting. If it's 4000 :

ports:
      - "4000:4000"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM