Ruby on Rails application slow to load when running in Docker container - ruby-on-rails

I have a Ruby on Rails application that I'm running locally using docker-compose. The container comes up relatively fast. But the Rails applications takes a few minutes to start. It start up quickly but takes a while to get past the preloading state.
Dockefile
FROM ruby:3.1.2-slim-buster
# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpq-dev \
nodejs \
net-tools \
libffi-dev \
git \
curl \
yarn
# Bundle install
WORKDIR /app
COPY Gemfile Gemfile.lock ./
# RUN gem install ffi -v 1.15.5 -- --disable-system-libffi
# RUN gem install bundler:2.3.14
RUN bundle config build.nokogiri --use-system-libraries
RUN bundle check || bundle install
# Yarn install
# COPY package.json yarn.lock ./
# RUN yarn install
# Copy the main application.
COPY . ./
ENTRYPOINT ["./entrypoints/docker-entrypoint.sh"]
docker-compose.yml
version: '3.8'
services:
web:
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- "3000:3000"
volumes:
- .:/app
- gem_cache:/usr/local/bundle/gems
- node_modules:/app/node_modules
- web_logs:/app/log
env_file: .env
environment:
- RAILS_ENV=development
- DUMP_SCHEMA_AFTER_MIGRATION=false
depends_on:
- db
- redis
db:
image: postgres:14-bullseye
restart: always
ports:
- 5432:5432
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: auth_svc_development
POSTGRES_HOST_AUTH_METHOD: trust
redis:
image: redis:6.2
volumes:
postgres_data:
gem_cache:
node_modules:
web_logs:
auth-svc-web-1 | [7] Puma starting in cluster mode...
auth-svc-web-1 | [7] * Puma version: 6.0.0 (ruby 3.1.2-p20) ("Sunflower")
auth-svc-web-1 | [7] * Min threads: 5
auth-svc-web-1 | [7] * Max threads: 5
auth-svc-web-1 | [7] * Environment: development
auth-svc-web-1 | [7] * Master PID: 7
auth-svc-web-1 | [7] * Workers: 4
auth-svc-web-1 | [7] * Restarts: (✔) hot (✖) phased
auth-svc-web-1 | [7] * Preloading application

Related

Rails in docker error when try to start server in Windows WSL2 Docker Desktop Engine(docker compose), but working correctly in a Debian system

This error message appears to 3 images in composed docker container.
exec /usr/bin/entrypoint.sh: no such file or directory
All images related to Ruby execution of services
Sidekiq, Webpack runned by Ruby executable and Web(rails) services
I have tried change every execution to run loading de Gemfile environment using bundle exec, but nothing worked.
Dockerfile
FROM ruby:2.6.6
RUN apt-get update -qq \
&& apt-get install -y curl build-essential libpq-dev postgresql \
nodejs postgresql-client &&\
curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y nodejs yarn
ADD . /app
WORKDIR /app
RUN gem install bundler:2.3.22
RUN bundle install
RUN yarn install --check-files
RUN gem install foreman
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 80
CMD ["bash"]
docker-compose.yml
version: '3.3'
services:
db:
image: postgres
ports:
- 5423:5432
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: *****
redis:
image: redis
ports:
- "6379:6379"
volumes:
- 'redis:/data'
depends_on:
- db
webpack:
build: .
command: sh -c 'rm -rf public/packs/* || true && bin/webpack-dev-server --host 0.0.0.0 --port 3035 -w'
volumes:
- .:/app
- /app/node_modules
ports:
- "3035:3035"
depends_on:
- db
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && rails s -b 0.0.0.0 -p 80"
volumes:
- .:/app
ports:
- "80:80"
depends_on:
- db
- redis
- webpack
- chrome
env_file: .env_docker
environment:
RAILS_ENV: development
RAILS_MAX_THREADS: 5
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
volumes:
- .:/app
depends_on:
- db
- redis
env_file: .env_docker
environment:
RAILS_MAX_THREADS: 5
chrome:
image: selenium/standalone-chrome
ports:
- "4444:4444"
volumes:
- /dev/shm:/dev/shm
depends_on:
- db
- redis
- webpack
- sidekiq
volumes:
redis:
postgres:
Equal to entrypoint.sh exec: #: not found but not resolved
I really want to change my Debian development OS to Windows and work only with containers, not looking to Linux or WSL alternatives

How to access dockerized rails server using "localhost"

I don't play often with Docker so I'm really confused here.
Here is my Dockerfile:
ARG RUBY_VERSION
FROM ruby:${RUBY_VERSION}-slim-buster
ARG PG_MAJOR
ARG NODE_MAJOR
ARG BUNDLER_VERSION
ARG YARN_VERSION
# common dependencies
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
curl \
less \
git \
&& apt-get clean \
&& rm -fr /var/cache/apt/archives/* \
&& rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
# add postgresql to sources list
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list
# add nodejs to sources list
RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -
# add yarn to sources list
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
# application dependencies
# we use an external aptfile for that
COPY Aptfile /tmp/Aptfile
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
libpq-dev \
postgresql-client-$PG_MAJOR \
nodejs \
yarn=$YARN_VERSION-1 \
$(cat /tmp/Aptfile | xargs) && \
apt-get clean && \
rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
truncate -s 0 /var/log/*log
# configure bundler
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
# upgrade rubygems and install required bundler version
RUN gem update --system && \
gem install bundler:${BUNDLER_VERSION}
# create a directory for the app code
RUN mkdir -p /app
WORKDIR /app
And here is my docker-compose.yml:
version: '3.8'
services:
app: &app
build:
context: .dockerdev
dockerfile: Dockerfile
args:
BUNDLER_VERSION: '2.1.4'
NODE_MAJOR: '11'
PG_MAJOR: '13'
RUBY_VERSION: '2.7.2'
YARN_VERSION: '1.22.5'
image: example-dev:1.0.0
tmpfs:
- /tmp
backend: &backend
<<: *app
stdin_open: true
tty: true
volumes:
- .:/app:cached
- rails_cache:/app/tmp/cache
- bundle:/usr/local/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
- .dockerdev/.psqlrc:/root/.psqlrc:ro
environment:
- NODE_ENV=development
- RAILS_ENV=${RAILS_ENV:-development}
- REDIS_URL=redis://redis:6379/
- DATABASE_URL=postgres://postgres:postgres#postgres:5432
- BOOTSNAP_CACHE_DIR=/usr/local/bundle/_bootsnap
- WEBPACKER_DEV_SERVER_HOST=webpacker
- HISTFILE=/app/log/.bash_history
- PSQL_HISTFILE=/app/log/.psql_history
- EDITOR=vi
- MALLOC_ARENA_MAX=2
- WEB_CONCURRENCY=${WEB_CONCURRENCY:-1}
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
runner:
<<: *backend
command: /bin/bash
ports:
- '3000:3000'
- '3002:3002'
rails:
<<: *backend
command: bundle exec rails server -b 0.0.0.0
ports:
- '3000:3000'
sidekiq:
<<: *backend
command: bundle exec sidekiq -C configs/sidekiq.yml
postgres:
image: postgres:13.1
volumes:
- .psqlrc:/root/.psqlrc:ro
- postgres:/var/lib/postgresql/data
- ./log:/root/log:cached
- ./.dockerdev/init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- PSQL_HISTFILE=/root/log/.psql_history
- POSTGRES_PASSWORD=postgres
ports:
- 5432
healthcheck:
test: pg_isready -U postgres -h 127.0.0.1
interval: 5s
redis:
image: redis:5.0-alpine
volumes:
- redis:/data
ports:
- 6379
healthcheck:
test: redis-cli ping
interval: 1s
timeout: 3s
retries: 30
webpacker:
<<: *app
command: ./bin/webpack-dev-server
ports:
- '3035:3035'
volumes:
- .:/app:cached
- bundle:/usr/local/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
environment:
- NODE_ENV=${NODE_ENV:-development}
- RAILS_ENV={RAILS_ENV:-development}
- WEBPACKER_DEV_SERVER_HOST=0.0.0.0
volumes:
postgres:
redis:
bundle:
node_modules:
rails_cache:
packs:
Everything works just fine, I can run my runner, install gems, play with db, run rails, etc.
Until I try to reach http://localhost:3000 which only shows:
This site can’t be reached
localhost refused to connect.
Here is the step I do to run rails server:
$ docker-compose run --rm rails
Creating okamii-saas_rails_run ... done
=> Booting Puma
=> Rails 6.0.3.4 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.6 (ruby 2.7.2-p137), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
And here is the result of docker-compose ps:
Name Command State Ports
------------------------------------------------------------------------------------------------------------
okamii-saas_postgres_1 docker-entrypoint.sh postgres Up (healthy) 0.0.0.0:32789->5432/tcp
okamii-saas_rails_run_6fff67202995 bundle exec rails server - ... Up
okamii-saas_redis_1 docker-entrypoint.sh redis ... Up (healthy) 0.0.0.0:32788->6379/tcp
I have the feeling that the fact that nothing shows in the Ports column for okamii-saas_rails_run_6fff67202995 is a sign something is wrong but I don't know why it is empty and what I am supposed to do here. (cf. EDIT 1)
As a note, I know the title say how to using localhost but I really can't access it at all AFAIK :)
===
EDIT 1:
That's not entirely true. I figured that by adding EXPOSE 3000 in my Dockerfile, docker-compose ps will show something in the column Ports for my container but that did not change things a bit.
Here is an updated view of docker-composer ps when using EXPOSE 3000:
Name Command State Ports
------------------------------------------------------------------------------------------------------------
okamii-paas_postgres_1 docker-entrypoint.sh postgres Up (healthy) 0.0.0.0:32771->5432/tcp
okamii-paas_rails_run_d812907346b4 bundle exec rails server - ... Up 3000/tcp
okamii-paas_redis_1 docker-entrypoint.sh redis ... Up (healthy) 0.0.0.0:32770->6379/tcp
EDIT 2:
From what I can read from the doc about EXPOSE, it is only acting as a documentation. It does not do anything else which explains why using it does not change anything.
EDIT 3:
I just tried running docker-compose up -d rails instead of docker-compose run rails and this message started spawning:
$ dc up -d rails
Creating network "okamii-paas_default" with the default driver
Creating okamii-paas_postgres_1 ... done
Creating okamii-paas_redis_1 ... done
Creating okamii-paas_rails_1 ...
Creating okamii-paas_rails_1 ... error
ERROR: for okamii-paas_rails_1 Cannot start service rails: driver failed programming external connectivity on endpoint okamii-paas_rails_1 (5d07dfedfc5c979133ce61a237327edb149a0a6793a85f61f6ad8218a60a510b): Bind for 0.0.0.0:3000 failed: port is already allocated
ERROR: for rails Cannot start service rails: driver failed programming external connectivity on endpoint okamii-paas_rails_1 (5d07dfedfc5c979133ce61a237327edb149a0a6793a85f61f6ad8218a60a510b): Bind for 0.0.0.0:3000 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
I don't understand where the conflict comes from.
After #dbugger's suggestion, I tweaked the ports publication from "3000:3000" to "3000". And now, stuff shows in the Ports column but of course, the mapping is wrong.
Name Command State Ports
------------------------------------------------------------------------------------------------
okamii-paas_postgres_1 docker-entrypoint.sh postgres Up (healthy) 0.0.0.0:32784->5432/tcp
okamii-paas_rails_1 bundle exec rails server - ... Up 0.0.0.0:32786->3000/tcp
okamii-paas_redis_1 docker-entrypoint.sh redis ... Up (healthy) 0.0.0.0:32785->6379/tcp

Yarn packages out of date when running Rails app in Docker

I start my Rails 6 app as Docker containers, but when it starts the Rails server, it keeps giving the error:
warning Integrity check: System parameters don't match
website_1 | error Integrity check failed
website_1 | error Found 1 errors.
website_1 |
website_1 |
website_1 | ========================================
website_1 | Your Yarn packages are out of date!
website_1 | Please run `yarn install --check-files` to update.
website_1 | ========================================
website_1 |
website_1 |
website_1 | To disable this check, please change `check_yarn_integrity`
website_1 | to `false` in your webpacker config file (config/webpacker.yml).
So why is this not working? I have that command in the Dockerfile and also that check is disabled in webpacker.yml.
I build it with docker-compose up --build and then it doesn't seem to give errors.
When I start it with docker-compose up, it will return the erorr when the Rails server is started. Here are the relevant files:
Dockerfile:
FROM ruby:2.6.5-slim
LABEL maintainer="John van Arkelen <johnvanarkelen#fastmail.com>"
RUN apt-get update -qq && apt-get install -y curl build-essential libpq-dev postgresql postgresql-contrib
RUN mkdir /app
RUN mkdir -p /usr/local/nvm
WORKDIR /app
RUN curl -sL https://deb.nodesource.com/setup_11.x | bash -
RUN apt-get install -y nodejs
RUN node -v
RUN npm -v
ENV BUNDLE_PATH /gems
RUN gem install bundler -v 2.0.2
COPY Gemfile Gemfile.lock package.json yarn.lock ./
RUN bundle install
RUN npm install -g yarn
COPY . /app
RUN yarn install --check-files
webpacker.yml:
default: &default
source_path: app/javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
cache_path: tmp/cache/webpacker
check_yarn_integrity: false
webpack_compile_output: false
docker-compose.yml:
version: '2'
services:
postgres:
image: 'postgres:10.3-alpine'
volumes:
- 'postgres:/var/lib/postgresql/data'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password123
POSTGRES_DB: qd_development
env_file:
- '.env'
redis:
image: 'redis:4.0-alpine'
command: redis-server --requirepass password123
volumes:
- 'redis:/data'
website:
depends_on:
- 'postgres'
- 'redis'
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- '3000:3000'
volumes:
- '.:/app'
- gem_cache:/gems
environment:
DATABASE_HOST: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password123
POSTGRES_DB: qd_development
env_file:
- '.env'
volumes:
redis:
postgres:
gem_cache:
It seems to me like you are building your app correctly in the image in the /app directory. But afterward in your docker-compose you mount a local volume over your built /app directory, thereby losing your built /app folder contents.
Try removing the mounted volume '.:/app'.
In your docker-compose.yml, you can also do this to avoid rebuilding everytime time. Then, you can just do docker-compose up
command: bash -c "bundle install && yarn install --check-files && bundle exec rails s -p 3000 -b '0.0.0.0'"

Docker compose connection issue

I try to run a rails application in docker but I have an issue with docker-compose network, I think...
My Dockerfile looks like this:
FROM ruby:2.3-slim
RUN apt-get update \
&& apt-get install -qq -y --no-install-recommends \
build-essential \
nodejs \
libpq-dev \
git \
tzdata \
libxml2-dev \
libxslt-dev \
ssh \
&& rm -rf /var/lib/apt/lists/*
ENV APP_HOME /var/apps/books-store
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
ENV GEM_HOME /var/apps/books-store/vendor/bundle
ENV PATH $GEM_HOME/bin:$PATH
ENV BUNDLE_PATH $GEM_HOME
ENV BUNDLE_BIN $BUNDLE_PATH/bin
EXPOSE 3000
my docker-compose.yml looks like this:
version: '2'
services:
database:
image: postgres
volumes:
- ./data/pgdata:/pgdata
ports:
- '5555:5432'
env_file:
- '.env'
web:
links:
- database
build: .
volumes:
- .:/var/apps/books-store
ports:
- '3000:3000'
command: [bundle, exec, puma]
env_file:
- '.env'
stdin_open: true
tty: true
When I try docker-compose up, from logs I see rails server starts successfully but when I try to access localhost:3000 from host browser it does not work and I could not understand why. What am I doing wrong?
docker ps:
407b59a2fa99 bookstore_web "bundle exec puma" About a minute ago Up 41 seconds 0.0.0.0:3000->3000/tcp bookstore_web
1837fc3e3f387 postgres "docker-entrypoint..." About a minute ago Up 49 seconds 0.0.0.0:5555->5432/tcp bookstore_database_1
docker-compose logs web:
Attaching to bookstore_web_1
web_1 | Puma starting in single mode...
web_1 | * Version 3.6.2 (ruby 2.3.3-p222), codename: Sleepy Sunday Serenity
web_1 | * Min threads: 0, max threads: 16
web_1 | * Environment: development
web_1 | * Listening on tcp://0.0.0.0:9292
web_1 | Use Ctrl-C to stop

Not able to connect mongodb with Rails container using Docker compose

Getting this error when inserting values in Model through rails console .
"Mongo::Error::NoServerAvailable: No server is available matching
preference: # using server_selection_timeout=30 and local_threshold=
0.015 "
Both containers are running fine, but Rails not able to connect mongodb .
I have only one Dockerfile.
My docker-compose.yml file contents are:
version: '2'
services:
mongo:
image: mongo:3.0
command: mongod --smallfiles --quiet
environment:
- RAILS_ENV=production
- RACK_ENV=production
ports:
- "27017:27017"
app:
depends_on:
- 'mongo'
# - 'redis'
build: .
ports:
- '3000:3000'
volumes:
- '.:/app'
command: rails s -b '0.0.0.0'
env_file:
- '.env'
volumes:
mongo:
My Dockerfile :
FROM ruby:2.3.0
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
ENV APP_HOME /app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
ADD Gemfile* $APP_HOME/
RUN bundle install
ADD . $APP_HOME
Did you use mongo(same as the container name mentioned in docker-compose.yml) as your host in mongoid.yml?

Resources