Could not locate Gemfile or .bundle/ directory - ruby-on-rails

I am trying to use docker on with a rails apps. This setup works on other developers machines, but not mine. I can get the mysql to "up" but not the rails app. I've been at it for 2 days now.
I'm on windows 10 home using the Docker Quickstart Terminal. I was able to run this about a year ago on the same machine.
Here is my Dockerfile:
FROM ruby:2.5.3
ARG workdir=/bundle-api
RUN apt-get update -qq && apt-get install -y build-essential netcat graphviz
RUN mkdir -p ${workdir}
WORKDIR ${workdir}
ADD Gemfile ${workdir}/Gemfile
ADD Gemfile.lock ${workdir}/Gemfile.lock
RUN bundle install
ADD . ${workdir}
Here is my docker-compose file:
version: '2'
services:
db:
image: mysql:5.7
container_name: bundle-mysql
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: bundle_api_pwd
MYSQL_DATABASE: bundle_api_dev
MYSQL_USER: bundle_api
MYSQL_PASSWORD: bundle_api_pwd
ports:
- "3306:3306"
web:
build: .
container_name: bundle-rails
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/bundle-api:cached
- bundle_cache:/bundle_cache
ports:
- "3100:3000"
environment:
DB_HOST: db
DB_USERNAME: root
DB_PASSWORD: bundle_api_pwd
BUNDLE_PATH: /bundle_cache
GEM_HOME: /bundle_cache
GEM_PATH: /bundle_cache
depends_on:
- db
stdin_open: true
tty: true
bundle_cache:
image: busybox
volumes:
- bundle_cache:/bundle_cache
volumes:
db_data:
bundle_cache:
To start fresh I run this:
docker-compose build --no-cache
Everything seems to work fine. I can see all the gems install.
Once it runs, I run: docker-compose up and get this error.
bundle-rails | Could not locate Gemfile or .bundle/ directory
bundle-rails exited with code 10
Name Command State Ports
------------------------------------------------------------------------------------------------------------
bundle-backend_bundle_cache_1 sh Exit 0
bundle-mysql docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
bundle-rails bundle exec rails s -p 300 ... Exit 10

Looks like you are setting BUNDLE_PATH in the docker-compose.yml but you don't set it in the Dockerfile. That means you gems will be installed to /usr/local/bundle but then when you start the container ruby looks in /bundle-cache for the gems.

Related

permission denied while trying to start rails server in docker

I'm trying to run a rails server in a docker image along with a mysql and vue frontend image. I'm using ruby 3 and rails 6. The mysql and frontend image both start without problems. However the rails images doesn't start.
I'm on a Macbook Pro with MacOS Monterey and Docker Desktop 4.5.0
this is my docker-compose.yml:
version: "3"
services:
mysql:
image: mysql:8.0.21
command:
- --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=nauza_backend_development
ports:
- "3307:3306"
volumes:
- mysql:/var/lib/mysql
backend:
build:
context: nauza-backend
args:
UID: ${UID:-1001}
tty: true
stdin_open: true
command:
bundle exec rails s -p 8080 -b '0.0.0.0'
volumes:
- ./nauza-backend:/usr/src/app
# attach a volume at /bundle to cache gems
- bundle:/bundle
# attach a volume at ./node_modules to cache node modules
- node-modules:/usr/src/app/node_modules
# attach a volume at ./tmp to cache asset compilation files
- tmp:/usr/src/app/tmp
environment:
- RAILS_ENV=development
ports:
- "8080:8080"
depends_on:
- mysql
user: rails
environment:
- RAILS_ENV=development
- MYSQL_HOST=mysql
- MYSQL_USER=root
- MYSQL_PASSWORD=root
frontend:
build:
context: nauza-frontend
args:
UID: ${UID:-1001}
volumes:
- ./nauza-frontend:/usr/src/app
ports:
- "3000:3000"
user: frontend
volumes:
bundle:
driver: local
mysql:
driver: local
tmp:
driver: local
node-modules:
driver: local
and this is my Dockerfile:
FROM ruby:3.0.2
ARG UID
RUN adduser rails --uid $UID --disabled-password --gecos ""
ENV APP /usr/src/app
RUN mkdir $APP
WORKDIR $APP
ENV EDITOR=vim
RUN apt-get update \
&& apt-get install -y \
nmap \
vim
COPY Gemfile* $APP/
RUN bundle install -j3 --path vendor/bundle
COPY . $APP/
CMD ["rails", "server", "-p", "8080", "-b", "0.0.0.0"]
when I try to start this with docker-compose up on my Mac I get the following error:
/usr/local/lib/ruby/3.0.0/fileutils.rb:253:in `mkdir': Permission denied # dir_s_mkdir - /usr/src/app/tmp/cache (Errno::EACCES)
Any ideas on how to fix this?
Remove the line - tmp:/usr/src/app/tmp on your Dockerfile.
You don't need to access temp files of your container I would say. 🙂

docker-compose volume is not mounting properly on Windows 10

I am setting up docker-compose for an existing Ruby on Rails project. I am using docker-compose version 1.23.1, build b02f1306 and Docker version 18.09.0, build 4d60db4
When I am trying to start my containers for development using docker-compose up --build my web and worker containers are exiting with code 10. When I /bin/bash into them the /web_gen folder only contains a /tmp/db inside of that and postgres files inside of that.
I can get the containers working by changing the volumes to - /web_gen but then the volumes will not hot reload.
My docker-compose.yml
version: '3'
services:
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/web_gen
ports:
- "3000:3000"
depends_on:
- db
- redis
db:
image: 'postgres:9.4.5'
volumes:
- ./tmp/db:/var/lib/postgresql/data
redis:
image: 'bitnami/redis:latest'
environment:
- ALLOW_EMPTY_PASSWORD=yes
worker:
build: .
command: bundle exec sidekiq -c 1
volumes:
- .:/web_gen
depends_on:
- redis
Dockerfile
FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /web_gen
WORKDIR /web_gen
COPY Gemfile /web_gen/Gemfile
COPY Gemfile.lock /web_gen/Gemfile.lock
RUN bundle install
COPY . /web_gen

Docker Postgres Ruby on Rails unable to connect

I am following this tutorial from docker Docker Rails and I have created a folder and added this code below in my docker file.
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
And my docker compose file code is:
version: '3'
services:
db:
image: postgres
volumes:
- .data:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
I am following tutorial when I am running docker compose up I can just see this error:
Could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
What is wrong here I don't know how to inspect and detect error how to fix this.
You need environment variables within your web container so that it knows how to connect to the db container.
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=
volumes:
- ./data:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
- PGHOST=db
- PGUSER=postgres
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
Please go to your database.yml and add host set to db , then username and password and run the command again.

Docker-compose rails image won't start after reboot

It ran just before I rebooted my machine, and suddenly I get Could not locate Gemfile or .bundle/ directory when starting the container.
During build, which completes without issue, I can see the contents of /app are correct, but on startup, /app only contains a .bundle directory and nothing else.
UPDATE: Turns out the volume ./documents_api:/app is what isn't working. Environment is docker for windows 17.09.1 running as administrator
Here is my folder structure:
./
.env
docker-compose.yml
documents_api/
<typical rails directory contents>
Dockerfile
.env just contains RAILS_ENV=development
the dockerfile contains:
FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /app
WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install
ADD . /app
docker-compose.yml contains:
version: '3'
services:
database:
image: mongo
volumes:
- mongo:/var/lib/mongo
env_file:
- .env
ports:
- "27017:27017"
documents:
build: ./documents_api
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- ./documents_api:/app
env_file:
- .env
expose:
- "3000"
depends_on:
- database
frontend:
image: nginx
build: ./web
depends_on:
- documents
ports:
- "80:80"
- "144:144"
# Persistence
volumes:
mongo:
Turns out I needed to remap my shared drives in the settings. It had lost the credentials after the reboot and was silently failing to map the volume.

How to make MySQL container run in the background when configured in docker-compose

I've created the docker-compose.yml file below to create a container based on Ruby image and a container based on MySQL image. When I execute docker-compose up, the MySQL container seems to be created correctly, however it is not run in the background. How can I configure it to do so using the docker-compose.yml file?
version: '2'
services:
web:
build:
context: .
dockerfile: .docker/rails.dockerfile
volumes:
- .:/var/www
ports:
- "3000:3000"
depends_on:
- 'mysql'
networks:
- ddoc-network
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: 'SOMETHING'
networks:
- ddoc-network
networks:
ddoc-network:
driver: bridge
rails.dockerfile
FROM ruby:2.3.1
MAINTAINER Juliano Nunes
RUN apt-get update -qq && apt-get install -y build-essential mysql-client libmysqlclient-dev nodejs
RUN mkdir /var/www
WORKDIR /var/www
ADD Gemfile /var/www/Gemfile
ADD Gemfile.lock /var/www/Gemfile.lock
RUN bundle install
ADD . /var/www
CMD ['bundle', 'exec', 'rails', 'server', '-b', '0.0.0.0']
You can always use docker-compose up -d to run your containers in detached mode.
Check docker-compose up --help for more info.

Resources