error running db migrate with docker for rails - ruby-on-rails

I'm trying to set up an dev environment for Vue.js and rails API following a tutorial.
Eventually I hit a hurdle when trying to run the following command:
docker-compose run backend rails db:create
Here is the error:
$ docker-compose run backend rails db:create
Starting am-full-stack_db_1_b7f6ee37d2e4 ... done
Error response from daemon: OCI runtime create failed:
container_linux.go:348: starting container process caused "exec:
\"rails\": executable file not found in $PATH": unknown
Here is my file tree
App
autheg-backend
autheg-frontend
docker-compose.yml
Here is my docker-compose.yml
version: '3'
services:
db:
image: postgres
ports:
- "5432"
backend:
build:
context: autheg-backend
args:
UID: ${UID:-1001}
volumes:
- ./autheg-backend:/usr/src/app
ports:
- "8080:8080"
depends_on:
- db
user: rails
frontend:
build:
context: autheg-frontend
args:
UID: ${UID:-1001}
volumes:
- ./autheg-frontend:/usr/src/app
ports:
- "3000:3000"
user: frontend
Result of 'docker-compose run backend env'
PATH=/usr/local/bundle/bin:/usr/local/bundle/gems/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=c88e0c72c584
TERM=xterm
RUBY_MAJOR=2.5
RUBY_VERSION=2.5.3
RUBY_DOWNLOAD_SHA256=1cc9d0359a8ea35fc6111ec830d12e60168f3b9b305a3c2578357d360fcf306f
RUBYGEMS_VERSION=2.7.8
BUNDLER_VERSION=1.17.1
GEM_HOME=/usr/local/bundle
BUNDLE_PATH=/usr/local/bundle
BUNDLE_SILENCE_ROOT_WARNING=1
BUNDLE_APP_CONFIG=/usr/local/bundle
APP=/usr/src/app
HOME=/home/rails
Thanks!

to do db migration use:
docker-compose run backend bin/rails db:create
# or
docker-compose run backend bundle exec rails db:create
/backend/Dockerfile
FROM ruby:2.5
ARG UID
RUN adduser rails --uid $UID --disabled-password --gecos ""
ENV APP /usr/src/app
RUN mkdir $APP
WORKDIR $APP
COPY Gemfile* $APP/
RUN bundle install -j3 --path vendor/bundle
COPY . $APP/
# Setting env up
ENV RAILS_ENV='development'
ENV RACK_ENV='development'
CMD [ "bundle", "exec", "rails", "server", "-p", "8080", "-b", "0.0.0.0"]
./docker-compose.yml
version: '3'
services:
db:
image: postgres
ports:
- "5432"
backend:
build:
context: autheg-backend
args:
UID: ${UID:-1001}
command: bundle exec rails s -p 8080 -b '0.0.0.0'
volumes:
- ./autheg-backend:/usr/src/app
ports:
- "8080:8080"
depends_on:
- db
user: rails
frontend:
build:
context: autheg-frontend
args:
UID: ${UID:-1001}
volumes:
- ./autheg-frontend:/usr/src/app
ports:
- "3000:3000"
user: frontend
Then
docker-compose up
Now you should be able to see the rails site using:
localhost:8080 (instead of 3000)
Hope that helps you get started

Where is your backened dockerfile for rails development like this example:-
# Base image:
FROM ruby:2.5
# Install dependencies
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
# create application directory
RUN mkdir /myapp
# Set our working directory inside the image
WORKDIR /myapp
# Setting env up
ENV RAILS_ENV='development'
ENV RACK_ENV='development'
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp
EXPOSE 3000
CMD [ "bundle", "exec", "puma", "-C", "config/puma.rb" ]

This is caused because the path is not set in the Docker container, it's set on your development/local machine. Use the following command:
docker-compose run backend bundle exec rails db:create

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. 🙂

ERROR: container_linux.go:367: starting container process caused: exec: "entrypoint.sh": executable file not found in $PATH: unknown

I am following this tutorial but when I ran docker-compose run --no-deps app rails new . --force --database=mysql I got the error mentioned in the question title. Below are the contents of some of my files:
Dockerfile:
FROM ruby:2.6
RUN apt-get update -qq && apt-get install -y nodejs
WORKDIR /essence
COPY Gemfile /essence/Gemfile
COPY Gemfile.lock /essence/Gemfile.lock
RUN bundle install
COPY . /essence
# Add a script to be executed every time the container starts
COPY ./entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
EXPOSE 3000
# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml:
version: '2'
services:
db:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: *****
MYSQL_DATABASE: *****
MYSQL_USER: *****
MYSQL_PASSWORD: *****
app:
build: .
command: bash -c "rm -f /essence/tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ".:/app"
ports:
- "3000:3000"
depends_on:
- db
links:
- db
environment:
DB_USER: *****
DB_NAME: *****
DB_PASSWORD: *****
DB_HOST: *****
I tried about 20 different things but none of them resolved this error. Does anyone know on a first glance what could be wrong with my setup? Thank you!

Docker: ERROR: Service 'app' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder818844007/entrypoint.sh: no such file or directory

I'm having problems when I build the container with MongoDB, when using the docker-compose up I get the following error
ERROR: Service 'app' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder367230859/entrypoint.sh: no such file or directory
I tried to change the mongo to PostgreSQL, but continue.
my files are below, thanks in advance
that Dockerfile
version: '3'
services:
web:
image: nginx
restart: always
# volumes:
# - ${APPLICATION}:/var/www/html
# - ${NGINX_HOST_LOG_PATH}:/var/log/nginx
# - ${NGINX_SITES_PATH}:/etc/nginx/conf.d
ports:
- "80:80"
- "443:443"
networks:
- web
mongo:
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
ports:
- "27017:27017"
# volumes:
# - data:/data/db
networks:
- mongo
app:
build: .
volumes:
- .:/mm_api
ports:
- 3000:3000
depends_on:
- mongo
networks:
web:
driver: bridge
mongo:
driver: bridge´´
that docker-compose
FROM ruby:2.7.0
RUN apt-get update -qq && apt-get install -y nodejs
RUN mkdir /mm_api
WORKDIR /mm_api
COPY Gemfile /mm_api/Gemfile
COPY Gemfile.lock /mm_api/Gemfile.lock
RUN bundle install
COPY . /mm_api
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma,rb"]
#CMD ["rails", "server", "-b", "0.0.0.0"]
that entry point
#!/bin/bash
set -e
rm -f /mm_api/tmp/pids/server.pid
exec "$#"
I had a similar issue when working on a Rails 6 application using Docker.
When I run docker-compose build, I get the error:
Step 10/16 : COPY Gemfile Gemfile.lock ./
ERROR: Service 'app' failed to build : COPY failed: stat /var/lib/docker/tmp/docker-builder408411426/Gemfile.lock: no such file or directory
Here's how I fixed it:
The issue was that the Gemfile.lock was missing in my project directory. I had deleted when I was having some issues with my gem dependencies.
All I had to do was to run the command below to install the necessary gems and then re-create the Gemfile.lock:
bundle install
And then this time when I ran the command docker-compose build everything worked fine again.
So whenever you encounter this issue endeavour to check if the file is present in your directory and most importantly if the path you specified to the file is the correct one.
That's all.
I hope this helps

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.

Bundle with docker-compose installs gems from scratch

I'm trying to cache bundle install on Docker build.
I've tried recommendations from several blog posts and I ended up in something like this:
Dockerfile:
FROM ruby:2.5.0-alpine
RUN apk update && apk add build-base postgresql-dev nodejs git tzdata
ENV APP_HOME /panabus-api
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
COPY Gemfile Gemfile.lock ./
ENV BUNDLE_PATH /gems
ENV GEM_PATH /gems
ENV GEM_HOME /gems
RUN bundle check || bundle install
COPY . $APP_HOME
ENV RAILS_ENV=production
RUN bundle exec rake RAILS_ENV=production assets:precompile apipie:cache
LABEL maintainer="Hari Carreras Pérez <hc#abtion.com>"
CMD bundle exec puma -C config/puma.rb
docker-compose.yml
version: '3'
services:
box:
image: busybox
volumes:
- gem_cache:/gems
postgres:
image: postgres:10.3-alpine
ports:
- 5432:5432
volumes:
- postgres:/var/lib/postgresql/data
restart: always
web:
build: .
image: panabus-api-web
command: bundle exec puma -C config/puma.rb -p 3000
depends_on:
- postgres
- box
volumes:
- .:/panabus-api
- gem_cache:/gems
ports:
- "3000:3000"
restart: always
environment:
- RAILS_ENV=development
tty: true
stdin_open: true
setup:
build: .
depends_on:
- postgres
- box
environment:
- RAILS_ENV=development
volumes:
- .:/panabus-api
- gem_cache:/gems
command: "bin/rails db:create db:migrate"
volumes:
postgres:
gem_cache:
This doesn't update the gems if the Gemfile hasn't changed. Unfortunately, if it has changed, it install all gems from scratch, not using the cache.
Is there any way to cache the gems on build?
I'd like to keep bundle in the Dockerfile since Heroku uses the same Dockerfile.
You need to add a tag to your bundle check where you have cached the installed gems
bundle check || bundle install --binstubs="$BUNDLE_BIN"
and cache the bundle with a specific path like so
ENV BUNDLE_PATH=/bundle \
BUNDLE_BIN=/bundle/bin \
GEM_HOME=/bundle
ENV PATH="${BUNDLE_BIN}:${PATH}"

Resources