When I use docker-compose up
the container exits with code 10 and says
Could not locate Gemfile or .bundle/ directory
but if I do docker run web entrypoint.sh
The rails app seems to start without an issue.
What could be the cause of this inconsistent behavior?
Entrypoint.sh
#!/bin/bash
set -e
if [ -f tmp/pids/server.pid ]; then
rm tmp/pids/server.pid
fi
bundle exec rails s -b 0.0.0.0 -p 8080
Relevant part from the docker-compose file.
docker-compose.yml
...
web:
build:
context: "./api/"
args:
RUBY_VERSION: '2.7.2'
BUNDLER_VERSION: '2.2.29'
entrypoint: entrypoint.sh
volumes:
- .:/app
tty: true
stdin_open: true
ports:
- "8080:8080"
environment:
- RAILS_ENV=development
depends_on:
- mongodb
...
CodePudding user response:
When you docker run web ...
, you're running exactly what's in the image, no more and no less. On the other hand, the volumes:
directive in the docker-compose.yml
file replaces the container's /app
directory with arbitrary content from the host. If your Dockerfile RUN bundle install
expecting to put content in /app/vendor
in the image, the volumes:
hide that.
You can frequently resolve problems like this by deleting volumes:
from the Compose setup. Since you're running the code that's built into your image, this also means you're running the exact same image and environment you'll eventually run in production, which is a big benefit of using Docker here.
(You should also be able to delete the tty:
and stdin_open:
options, which aren't usually necessary, and the entrypoint:
and those specific build: { args: }
, which replicate settings that should be in the Dockerfile.)
(The Compose file suggests you're building a Docker image out of the api
subdirectory, but then bind-mounting the current directory .
-- api
's parent directory -- over the image contents. That's probably the immediate cause of the inconsistency you see.)