Rails fails to start on docker - ruby-on-rails

I'm trying to setup my rails application to run using docker. It keeps crashing with Could not find rake-12.3.2 in any of the sources (Bundler::GemNotFound)
Dockerfile
FROM ruby:2.6.1
RUN apt-get update -yqq && \
apt-get install -yqq --no-install-recommends \
nodejs \
nano
COPY Gemfile* /usr/src/app/
WORKDIR /usr/src/app
RUN bundle install
RUN gem install foreman
RUN gem install rake -v 12.3.2
COPY . /usr/src/app/
CMD [ "bin/rails", "s", "-b", "0.0.0.0" ]
.docker-compose.yml
version: '3'
services:
postgres:
image: 'postgres:10.3-alpine'
volumes:
- 'postgres:/var/lib/postgresql/data'
env_file:
- '.env'
redis:
image: 'redis:4.0-alpine'
command: redis-server --requirepass yourpassword
volumes:
- 'redis:/data'
rails:
depends_on:
- 'postgres'
- 'redis'
build: .
ports:
- '3000:3000'
volumes:
- '.:/usr/src/app'
env_file:
- '.env'
volumes:
redis:
postgres:
config/boot.rb
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' # Set up gems listed in the Gemfile.
require 'bootsnap/setup' if RUBY_PLATFORM =~ /darwin/
log
rails_1 | /usr/local/lib/ruby/site_ruby/2.6.0/bundler/spec_set.rb:91:in `block in materialize': Could not find rake-12.3.2 in any of the sources (Bundler::GemNotFound)
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/bundler/spec_set.rb:85:in `map!'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/bundler/spec_set.rb:85:in `materialize'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/bundler/definition.rb:170:in `specs'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/bundler/definition.rb:237:in `specs_for'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/bundler/definition.rb:226:in `requested_specs'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/bundler/runtime.rb:108:in `block in definition_method'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/bundler/runtime.rb:20:in `setup'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/bundler.rb:107:in `setup'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/bundler/setup.rb:20:in `<top (required)>'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
rails_1 | from /usr/local/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
rails_1 | from /usr/src/app/config/boot.rb:3:in `<top (required)>'
rails_1 | from bin/rails:3:in `require_relative'
rails_1 | from bin/rails:3:in `<main>'

It looks like you are trying to pull down the current latest release of rake. You can remove the version declaration and let it auto resolve in this case.
Any reason this dependency is not in your Gemfile? Also, the rails app you are starting will include a dependency to rake. You should not need to install rake.

Related

Having issues running bundle install with docker container

I have an existing Rails application that I am just trying to turn into a docker instance. I've went through a few tutorials on dockerizing an existing rails app, but I keep getting stuck somewhere.
Here's my Dockerfile:
FROM ruby:2.5.1
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
I confirmed that my Gemfile.lock file in the local directory is empty.
Finally, here's my docker-compose.yml file:
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_HOST_AUTH_METHOD: trust
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ./:/myapp
ports:
- "3000:3000"
depends_on:
- db
After running docker-compose build, it goes through the whole process of pulling down Ruby, postgresql, doing bundle install, etc... and then finally when I try to run docker-compose up, I get the following error:
web_1 | Bundler::GitError: The git source https://github.com/zdennis/activerecord-import is not yet checked out. Please run `bundle install` before trying to start your application
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git/git_proxy.rb:235:in `allowed_in_path'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git/git_proxy.rb:192:in `find_local_revision'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git/git_proxy.rb:64:in `revision'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git.rb:225:in `revision'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git.rb:93:in `install_path'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/path.rb:126:in `expanded_path'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/path.rb:163:in `load_spec_files'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git.rb:200:in `load_spec_files'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/path.rb:100:in `local_specs'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/source/git.rb:167:in `specs'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:759:in `block in converge_locked_specs'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:745:in `each'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:745:in `converge_locked_specs'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:248:in `resolve'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:171:in `specs'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:238:in `specs_for'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/definition.rb:227:in `requested_specs'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/runtime.rb:108:in `block in definition_method'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/runtime.rb:20:in `setup'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler.rb:107:in `setup'
web_1 | /usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/setup.rb:20:in `<top (required)>'
web_1 | /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
web_1 | /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
web_1 | bundler: failed to load command: rails (/usr/local/bundle/bin/rails)
What am I missing here? I would like to use an existing volume as myapp (not sure if there's any reason I shouldn't want to do this -- perhaps it runs faster if it's within the container itself?)
The only way I can get this to work properly is if I run docker-compose build, followed by docker-compose run web bundle install, followed by docker-compose up
Am I doing something wrong that's requiring me to have to run docker-compose run web bundle install? I saw it run when I ran docker-compose build, so not sure why it's required again.

How do I set up rails app in docker thats already been developed

Im new to using docker and I cant seem to get the app to build and make correctly. I'm sure its some easy DB setup or PG gem config.
Here's the output after trying to
=> Booting Puma
=> Rails 4.2.10 application starting in development on http://0.0.0.0:3000
=> Run rails server -h for more startup options
=> Ctrl-C to shutdown server
Exiting
/usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapters/connection
_specification.rb:177:in `rescue in spec': Specified 'postgresql' for database adapter, but
the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the mi
nimum required by ActiveRecord). (Gem::LoadError)
from /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapte
rs/connection_specification.
rb:174:in `spec'
from /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_handli
ng.
rb:50:in `establish_connection'
from /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/railtie.rb:122:in
`block (2 levels) in <class:Railtie>'
from /usr/local/bundle/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks
.rb:38:in `instance_eval'
from /usr/local/bundle/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks
.rb:38:in `execute_hook'
from /usr/local/bundle/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks
.rb:28:in `block in on_load'
from /usr/local/bundle/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks
.rb:27:in `each'
from /usr/local/bundle/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks
.rb:27:in `on_load'
from /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/railtie.
rb:118:in
`block in <class:Railtie>'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/initializable.rb:30:in `insta
nce_exec'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/initializable.rb:30:in `run'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/initializable.rb:55:in `block
in run_initializers'
from /usr/local/lib/ruby/2.4.0/tsort.
rb:228:in `block in tsort_each'
from /usr/local/lib/ruby/2.4.0/tsort.
rb:350:in `block (2 levels) in each_strongly_connected_component'
from /usr/local/lib/ruby/2.4.0/tsort.
rb:431:in `each_strongly_connected_component_from'
from /usr/local/lib/ruby/2.4.0/tsort.
rb:349:in `block in each_strongly_connected_component'
from /usr/local/lib/ruby/2.4.0/tsort.
rb:347:in `each'
from /usr/local/lib/ruby/2.4.0/tsort.
rb:347:in `call'
from /usr/local/lib/ruby/2.4.0/tsort.
rb:347:in `each_strongly_connected_component'
from /usr/local/lib/ruby/2.4.0/tsort.
rb:226:in `tsort_each'
from /usr/local/lib/ruby/2.4.0/tsort.
rb:205:in `tsort_each'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/initializable.
rb:54:in `run_initializers'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/application.
rb:352:in `initia
lize!'
from /app/config/environment.rb:7:in `<top (required)>'
from /usr/local/bundle/gems/activesupport-4.2.10/lib/active_support/dependencies.
rb:274:in `require'
from /usr/local/bundle/gems/activesupport-4.2.10/lib/active_support/dependencies.
rb:274:in `block in require'
from /usr/local/bundle/gems/activesupport-4.2.10/lib/active_support/dependencies.
rb:240:in `load_dependency'
from /usr/local/bundle/gems/activesupport-4.2.10/lib/active_support/dependencies.rb
:274:in `require'
from /app/config.ru:3:in `block in <main>'
from /usr/local/bundle/gems/rack-1.6.12/lib/rack/builder.rb:55:in `instance_eval'
from /usr/local/bundle/gems/rack-1.6.12/lib/rack/builder.rb:55:in `initialize'
from /app/config.ru:in `new'
from /app/config.ru:in `<main>'
from /usr/local/bundle/gems/rack-1.6.12/lib/rack/builder.
rb:49:in `eval'
from /usr/local/bundle/gems/rack-1.6.12/lib/rack/builder.
rb:49:in `new_from_string'
from /usr/local/bundle/gems/rack-1.6.12/lib/rack/builder.
rb:40:in `parse_file'
from /usr/local/bundle/gems/rack-1.6.12/lib/rack/server.
rb:300:in `build_app_and_op
tions_from_config'
from /usr/local/bundle/gems/rack-1.6.12/lib/rack/server.
rb:209:in `app'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/commands/server.
rb:61:in `app
'
from /usr/local/bundle/gems/rack-1.6.12/lib/rack/server.rb:337:in `wrapped_app'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/commands/server.
rb:139:in `lo
g_to_stdout'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/commands/server.
rb:78:in `start'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/commands/commands_tasks.
rb:80
:in `block in server'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/commands/commands_tasks.
rb:75:in `tap'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/commands/commands_tasks.
rb:75
:in `server'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/commands/commands_tasks.
rb:39
:in `run_command!'
from /usr/local/bundle/gems/railties-4.2.10/lib/rails/commands.
rb:17:in `<top (requ
ired)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Dockerfile
FROM ruby:2.4.2-alpine
# Open port 3000 to access the Rails applications
# This does not belong in production as a reverse proxy should route to the application internally
EXPOSE 3000
# Starting command line arguments wrapped in `bundle exec`
ENTRYPOINT ["bundle", "exec"]
# Required pager to using irb or rails console within container
ENV PAGER="busybox less"
# Start Rails server by default bound to 0.0.0.0:3000 of the container
CMD ["rails", "server", "-p", "3000", "-b", "0.0.0.0"]
# Create main application directory
RUN mkdir /app
# Copy all contents of the current directory to the main application directory
COPY . /app
# Set the working directory to the main application directory
WORKDIR /app
# Install required system libraries, latest version of bundle and fetch all dependencies
RUN rm -f /app/tmp/pids/server.pid \
&& apk add --no-cache --verbose \
bash \
build-base \
ca-certificates \
gcc \
git \
libffi-dev \
libxml2 \
libxml2-dev \
libxslt \
libxslt-dev \
nodejs \
patch \
postgresql-dev \
ruby \
ruby-dev \
tzdata \
&& gem install bundler --pre \
&& gem install tzinfo:1.2.2 tzinfo-data \
&& gem install nokogiri --version 1.8.1 -- --use-system-libraries --with-xml2-config=/usr/local/bin/xml2-config --with-xslt-config=/usr/local/bin/xslt-config \
&& bundle install
# Once the container is built, run it using the following:
# > docker run --detach <image-name-or-hash>
# This will run the container and start Rails as follows from the application directory:
# > bundle exec rails server -p 3000 -b 0.0.0.0
# If you want to run something different like the Rails console:
# > docker run --interactive -tty <image-name-or-hash> rails console
# This will run the conatiner and start the Rails console as follows from the application directory:
# > bundle exec rails console
The error says you're missing the pg gem while you've defined the database adapter to postgresql in the config/database.yml file.
The question title says "already been developed" so I guess your Gemfile has the gem 'pg' line, and your Gemfile.lock has also a line referring to the version to install ... but can you check? :)
Then you're not sharing your Dockerfile so it's harder to guess what's your issue, but I would bet on a missing bundle install command in the Dockerfile.
Be sure to have a line like that in your Dockerfile:
RUN bundle install
In the case this doesn't help, please update your question with your Dockerfile.
I think is easy to use docker to avoid to install anything in your development environment.
Usually, I use docker to do that, I use a container exposing the ports of the service like I have the service installed in my machine. Doing that, I am avoiding to install any additional services.
If you want to use a few services in your environment, you can find containers for anything, but if you need to build your own image you need to learn more about docker.
And for example, to use postgres I use a docker-compose.yml like that:
version: '3'
services:
postgres:
image: postgres:alpine
ports:
- "5432:5432"
volumes:
- pg-data:/var/lib/postgresql/data
volumes:
pg-data: {}

Unable to deploy Rails app into Google Cloud Run

I have a Rails app running locally with Docker and now I'm trying to deploy it to Google Cloud Platform using Cloud Run, following this great tutorial by Laurent Julliard.
I'm using following GCP services:
Cloud Build to build the container image.
Cloud Run for the deploy.
Cloud Storage for Rails Active Storage.
Cloud SQL for my Postgres DB.
Cloud Key Management Service for my secrets.
Last 3 services are not relevant for this issue (I think 🤷🏻‍♂️).
My deploy process has 2 steps:
Build the container image using Cloud Build
Deploy the image using Cloud Run
Build the container image using Cloud Build
cloudbuild.yaml file describe the steps Cloud Build has to go through to build your container.
steps:
# Decrypt Rails Master key file
- name: gcr.io/cloud-builders/gcloud
args: ["kms", "decrypt", "--ciphertext-file=./config/master.key.enc",
"--plaintext-file=./config/master.key",
"--location=us-central1","--keyring=whale-on-rails",
"--key=rails_master_key"]
# Decrypt Whale on Rails service account credentials
- name: gcr.io/cloud-builders/gcloud
args: ["kms", "decrypt", "--ciphertext-file=./config/whale_on_rails.key.enc",
"--plaintext-file=./config/whale_on_rails.key",
"--location=us-central1","--keyring=whale-on-rails",
"--key=whale_on_rails_key"]
# Build image with tag 'latest' and pass decrypted Rails DB password as argument
- name: 'gcr.io/cloud-builders/docker'
args: [ 'build',
'--tag', 'gcr.io/$PROJECT_ID/whale_on_rails:latest',
'--build-arg', 'DB_PWD',
'--build-arg', 'RUBY_VERSION=${_RUBY_VERSION}',
'--build-arg', 'PG_MAJOR=${_PG_MAJOR}',
'--build-arg', 'NODE_MAJOR=${_NODE_MAJOR}',
'--build-arg', 'YARN_VERSION=${_YARN_VERSION}',
'--build-arg', 'BUNDLER_VERSION=${_BUNDLER_VERSION}',
'--build-arg', 'RAILS_ENV=${_RAILS_ENV}',
'--build-arg', 'REDIS_URL=${_REDIS_URL}',
'--build-arg', 'DATABASE_HOST=${_DATABASE_HOST}',
'--build-arg', 'DATABASE_USER=${_DATABASE_USER}',
'--build-arg', 'DATABASE_NAME=${_DATABASE_NAME}',
'.'
]
secretEnv: ['DB_PWD']
env:
- RAILS_ENV=${_RAILS_ENV}
- REDIS_URL=redis://redis:6379/
- DATABASE_HOST=/cloudsql/whale-on-rails:us-central1:whale-on-rails-production
- DATABASE_USER=postgres
- DATABASE_NAME=whale-on-rails
# Push new image to Google Cloud Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/whale_on_rails:latest']
secrets:
- kmsKeyName: projects/whale-on-rails/locations/us-central1/keyRings/whale-on-rails/cryptoKeys/db_pwd_key
secretEnv:
DB_PWD: "CiQArHLfBqlON9MNzM+eKp/Un/HJucmgUftgl5LkYBzvLjsXsaQSOQCBzWJBAh061i7dJrNhEQeWqJHgSkaJpRka9w9nxmbiFzHZ1fpXOm0d7FWAi/8v36EDXmWnxkYXsw=="
substitutions:
_RUBY_VERSION: '2.6.5'
_PG_MAJOR: '11'
_NODE_MAJOR: '12'
_YARN_VERSION: '1.13.0'
_BUNDLER_VERSION: '2.0.2'
_RAILS_ENV: production
_REDIS_URL: redis://redis:6379/
_DATABASE_HOST: /cloudsql/whale-on-rails:us-central1:whale-on-rails-production
_DATABASE_USER: postgres
_DATABASE_NAME: whale-on-rails
Dockerfile: this file is invoked by the ‘Build image’ step in Cloud Build.
ARG RUBY_VERSION
FROM ruby:$RUBY_VERSION
ARG PG_MAJOR
ARG NODE_MAJOR
ARG BUNDLER_VERSION
ARG YARN_VERSION
ARG RAILS_ENV
# 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/ stretch-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 the 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
# Install dependencies
COPY .docker/dev/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 \
build-essential \
postgresql-client-$PG_MAJOR \
nodejs \
yarn=$YARN_VERSION-1 \
$(cat /tmp/Aptfile | xargs) && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
truncate -s 0 /var/log/*log
# Create a directory for the app code
ENV APP_HOME=/usr/src/app
# RUN mkdir -p ${APP_HOME}
WORKDIR ${APP_HOME}
# Install production dependencies (Gems installation in
# local vendor directory)
COPY Gemfile Gemfile.lock ./
ENV BUNDLE_FROZEN=true
# Upgrade RubyGems and install required Bundler version
RUN gem update --system && \
gem install bundler:$BUNDLER_VERSION
RUN bundle install
ENV RAILS_ENV=$RAILS_ENV
ENV RAILS_SERVE_STATIC_FILES=true
ENV RAILS_LOG_TO_STDOUT=true
COPY . .
# Pre-compile Rails assets (master key needed)
RUN yarn install
RUN RAILS_ENV=production bundle exec rails assets:precompile
# Set Google App Credentials environment variable with Service Account
ENV GOOGLE_APPLICATION_CREDENTIALS=${APP_HOME}/config/whale_on_rails.key
# Database environment variables
ARG DATABASE_NAME
ARG DATABASE_HOST
ARG DATABASE_USER
ARG DB_PWD
ENV DATABASE_PASSWORD=$DB_PWD
ENV DATABASE_NAME=$DATABASE_NAME
ENV DATABASE_HOST=$DATABASE_HOST
ENV DATABASE_USER=$DATABASE_USER
RUN chmod +x ${APP_HOME}/.docker/script/entry
ENTRYPOINT ["/usr/src/app/.docker/script/entry"]
entrypoint
#!/usr/bin/env bash
cd /usr/src/app
# Create, migrate and seed the Rails production DB
bundle exec rails db:prepare
# Do some protective cleanup
> log/production.log
rm -f tmp/pids/server.pid
# Run the web service on container startup
bundle exec rails server -e production -b 0.0.0.0 -p $PORT
Finally, I'm using the following command to build the container.
$ gcloud builds submit --config cloudbuild.yaml
This process works great. The problem comes in the next step...
Deploy the image using Cloud Run
$ gcloud beta run deploy whale-on-rails --image gcr.io/$PROJECT_ID/whale_on_rails \ 4m 58s Ruby 2.6.5
--set-cloudsql-instances whale-on-rails-production \
--region us-central1 --allow-unauthenticated --platform managed
In this step, I get this error in terminal:
ERROR: (gcloud.beta.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.
And checking out Cloud Run logs, I get this:
2019-10-24 13:03:35.448 CEST<main>: warning: timer_create failed: Success, signals racy
2019-10-24 13:03:37.756 CEST=> Booting Puma
2019-10-24 13:03:37.756 CEST=> Rails 6.0.0 application starting in production
2019-10-24 13:03:37.756 CEST=> Run `rails server --help` for more startup options
2019-10-24 13:03:40.687 CESTExiting
2019-10-24 13:03:40.687 CEST(erb):21:in `<main>': undefined local variable or method `“config' for main:Object (NameError)
2019-10-24 13:03:40.687 CEST from /usr/local/lib/ruby/2.6.0/erb.rb:901:in `eval'
2019-10-24 13:03:40.687 CEST from /usr/local/lib/ruby/2.6.0/erb.rb:901:in `result'
2019-10-24 13:03:40.688 CEST from /usr/local/bundle/gems/activestorage-6.0.0/lib/active_storage/engine.rb:111:in `block (2 levels) in <class:Engine>'
2019-10-24 13:03:40.688 CEST from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:72:in `class_eval'
2019-10-24 13:03:40.688 CEST from /usr/local/bundle/gems/activesupport-6.0.0/lib/active_support/lazy_load_hooks.rb:72:in `block in execute_hook'
...
2019-10-24 13:03:40.689 CEST from /usr/src/app/bin/rails:9:in `<top (required)>'
2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
2019-10-24 13:03:40.689 CEST from /usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
2019-10-24 13:03:40.689 CEST from /usr/src/app/bin/spring:15:in `require'
2019-10-24 13:03:40.689 CEST from /usr/src/app/bin/spring:15:in `<top (required)>'
2019-10-24 13:03:40.689 CEST from bin/rails:3:in `load'
2019-10-24 13:03:40.689 CEST from bin/rails:3:in `<main>'
2019-10-24 13:03:41.492 CESTContainer called exit(1).
My question is, why I'm getting this error? Everything looks fine in the hole process and I've followed the tutorial step by step (the only difference is that they are using MySQL and here I'm using Postgres).

Error "Could not find rake-10.5.0 in any of the sources" on Phusion Passenger Docker image

I am trying to deploy a Rails app using Docker and the Phusion Passenger Ruby base image, but whenever I try to access the app from the browser I get this error:
web_1 | [ 2016-02-08 04:18:44.6861 31/7ff292141700 age/Cor/App/Implementation.cpp:304 ]: Could not spawn process for application /home/app/webapp: An error occurred while starting up the preloader.
web_1 | Error ID: d3103e16
web_1 | Error details saved to: /tmp/passenger-error-EwymlW.html
web_1 | Message from application: <p>It looks like Bundler could not find a gem. Maybe you didn't install all the gems that this application needs. To install your gems, please run:</p>
web_1 |
web_1 | <pre class="commands">bundle install</pre>
web_1 |
web_1 | <p>If that didn't work, then the problem is probably caused by your application being run under a different environment than it's supposed to. Please check the following:</p>
web_1 |
web_1 | <ol>
web_1 | <li>Is this app supposed to be run as the <code>app</code> user?</li>
web_1 | <li>Is this app being run on the correct Ruby interpreter? Below you will
web_1 | see which Ruby interpreter Phusion Passenger attempted to use.</li>
web_1 | </ol>
web_1 |
web_1 | <p>-------- The exception is as follows: -------</p>
web_1 | Could not find rake-10.5.0 in any of the sources (Bundler::GemNotFound)
web_1 | <pre> /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/spec_set.rb:92:in `block in materialize'
web_1 | /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/spec_set.rb:85:in `map!'
web_1 | /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/spec_set.rb:85:in `materialize'
web_1 | /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/definition.rb:140:in `specs'
web_1 | /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/definition.rb:185:in `specs_for'
web_1 | /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/definition.rb:174:in `requested_specs'
web_1 | /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/environment.rb:18:in `requested_specs'
web_1 | /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/runtime.rb:13:in `setup'
web_1 | /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler.rb:127:in `setup'
web_1 | /var/lib/gems/2.2.0/gems/bundler-1.10.6/lib/bundler/setup.rb:18:in `<top (required)>'
web_1 | /usr/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
web_1 | /usr/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
web_1 | /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:430:in `activate_gem'
web_1 | /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:297:in `block in run_load_path_setup_code'
web_1 | /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:435:in `running_bundler'
web_1 | /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:296:in `run_load_path_setup_code'
web_1 | /usr/share/passenger/helper-scripts/rack-preloader.rb:100:in `preload_app'
web_1 | /usr/share/passenger/helper-scripts/rack-preloader.rb:156:in `<module:App>'
web_1 | /usr/share/passenger/helper-scripts/rack-preloader.rb:30:in `<module:PhusionPassenger>'
web_1 | /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<main>'</pre>
web_1 |
web_1 |
web_1 | [ 2016-02-08 04:18:44.6935 31/7ff293143700 age/Cor/Con/CheckoutSession.cpp:277 ]: [Client 1-2] Cannot checkout session because a spawning error occurred. The identifier of the error is d3103e16. Please see earlier logs for details about the error.
This is my Dockerfile:
FROM phusion/passenger-ruby22:0.9.18
# Set correct environment variables.
ENV HOME /root
# Use baseimage-docker's init process.
CMD ["/sbin/my_init"]
# Enable Nginx/Passenger
RUN rm -f /etc/service/nginx/down
# Enable portals virtual host
RUN rm /etc/nginx/sites-enabled/default
COPY portals.conf /etc/nginx/sites-enabled/portals.conf
RUN mkdir /home/app/webapp
# Load env vars into nginx
COPY rails-env.conf /etc/nginx/main.d/rails-env.conf
# Install gems dependencies
COPY Gemfile* /tmp/
WORKDIR /tmp
RUN bundle install
# Copy rails app
WORKDIR /home/app/webapp
COPY . ./
RUN chown -R app:app ./
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
I tried running bundler as RUN bundle install --deployment but it didn't work as well. I am passing RAILS_ENV and PASSENGER_APP_ENV via the rails-env.conf file and they are both set to production (which is the default according to the Passenger image docs).
If I docker exec -it bash <ID> into the container and run gem list I see that all the gems are installed, so I don't know what's wrong.
This error is due to out of date software. Because the passenger images are not updated frequently it is important to bring everything up to date in your Dockerfile. This is how I generally setup a Dockerfile based off a phusion image:
FROM phusion/passenger-ruby22:0.9.18
ENV SYSTEM_UPDATE=1
RUN apt-get update \
&& apt-get upgrade -y -o Dpkg::Options::="--force-confold" \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /home/app
COPY Gemfile /home/app/Gemfile
COPY Gemfile.lock /home/app/Gemfile.lock
RUN gem update --system && \
gem update bundler && \
bundle install --jobs 4 --retry 5
# The rest of your app setup here
ENTRYPOINT ["/sbin/my_init", "--"]
SYSTEM_UPDATE is just a cache buster variable. When I bump that up all the packages will be updated on the next docker build. It should be bumped frequently.
I also ensure gem and bundler are fully up to date before running bundle install.
Also, there is no benefit to copying your Gemfile and Gemfile.lock to the tmp directory, just copy it to your application directory.
You can remove your final Clean up APT when done. command - that's really not the right place for that. There should be a single RUN line that runs all the apt-get commands in a single layer.
Take a look at https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/ for the best practices around setting up a Dockerfile, especially the sections about using apt-get.
For me, rake was there, I fix it using
rake rails:update
have fun!

Sidekiq Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) on docker-compose

I'm trying to run sidekiq worker with Rails. When I try to docker-compose up worker I get the following error:
worker_1 | Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:332:in `rescue in establish_connection'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:318:in `establish_connection'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:94:in `block in connect'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:280:in `with_reconnect'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:93:in `connect'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:351:in `ensure_connected'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:208:in `block in process'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:293:in `logging'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:207:in `process'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis/client.rb:113:in `call'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:211:in `block in info'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:57:in `block in synchronize'
worker_1 | /usr/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:57:in `synchronize'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/redis-3.2.2/lib/redis.rb:210:in `info'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/cli.rb:71:in `block in run'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq.rb:84:in `block in redis'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:64:in `block (2 levels) in with'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:63:in `handle_interrupt'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:63:in `block in with'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:60:in `handle_interrupt'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/connection_pool-2.2.0/lib/connection_pool.rb:60:in `with'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq.rb:81:in `redis'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/lib/sidekiq/cli.rb:68:in `run'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/gems/sidekiq-4.0.1/bin/sidekiq:13:in `<top (required)>'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/bin/sidekiq:23:in `load'
worker_1 | /home/app/Nyvur/vendor/bundle/ruby/2.2.0/bin/sidekiq:23:in `<main>'
nyvur_worker_1 exited with code 1
Here's my docker-compose file:
web: &app_base
build: .
ports:
- "80:80"
volumes:
- .:/Nyvur
command: /usr/bin/start_server.sh
links:
- postgres
- mongo
- redis
environment: &app_environment
SIDEKIQ_CONCURRENCY: 50
SIDEKIQ_TIMEOUT: 10
ENABLE_DEBUG_SERVER: true
RACK_ENV: production
RAILS_ENV: production
worker:
build: .
volumes:
- .:/Nyvur
ports: []
links:
- postgres
- mongo
- redis
command: bundle exec sidekiq -c 50
postgres:
image: postgres:9.1
ports:
- "5432:5432"
environment:
LC_ALL: C.UTF-8
POSTGRES_DB: Nyvur_production
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 3x1mpl3
mongo:
image: mongo:3.0.7
ports:
- "27017:27017"
redis:
image: redis
ports:
- "6379:6379"
My Dockerfile:
FROM phusion/passenger-customizable
MAINTAINER VodkaMD <support#nyvur.com>
ENV RACK_ENV="production" RAILS_ENV="production"
SECRET_KEY_BASE="e09afa8b753cb175bcef7eb5f737accd02a4c16d9b6e5d475943605abd4277cdf47c488812d21d9c7117efd489d876f34be52f7ef7e88b21759a079339b198ce"
ENV HOME /root
CMD ["/sbin/my_init"]
RUN /pd_build/utilities.sh
RUN /pd_build/ruby2.2.sh
RUN /pd_build/python.sh
RUN /pd_build/nodejs.sh
RUN /pd_build/redis.sh
RUN /pd_build/memcached.sh
RUN apt-get update && apt-get install -y vim nano dialog net-tools build-essential wget libpq-dev git
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# RUN mkdir /etc/nginx/ssl
# RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
RUN rm -f /etc/service/nginx/down
RUN rm -f /etc/service/redis/down
RUN rm -f /etc/service/sshd/down
RUN rm -f /etc/service/memcached/down
WORKDIR /tmp
ADD Gemfile /tmp/
ADD Gemfile.lock /tmp/
RUN mkdir /home/app/Nyvur
ADD . /home/app/Nyvur
RUN chown -R app:app /home/app/Nyvur
WORKDIR /home/app/Nyvur
RUN bundle install --deployment
RUN bundle exec rake assets:precompile
RUN rm /etc/nginx/sites-enabled/default
COPY config/nginx/nginx_nyvur.conf /etc/nginx/sites-enabled/nginx_nyvur.conf
ADD config/nginx/postgres-env.conf /etc/nginx/main.d/postgres-env.conf
ADD config/nginx/rails-env.conf /etc/nginx/main.d/rails-env.conf
ADD config/nginx/start_server.sh /usr/bin/start_server.sh
RUN chmod +x /usr/bin/start_server.sh
RUN mkdir -p /home/app/Nyvur/tmp/pids
RUN mkdir -p /home/app/Nyvur/tmp/sockets
RUN mkdir -p /home/app/Nyvur/log
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
EXPOSE 80 443 9292
I've tried different configurations, I've checked other builds, but the problem still persists, So far, Sidekiq runs well outside of Docker.
Check if your redis server is running, start redis by using the following command in the terminal:
redis-server
Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
Your app tries to connect on the localhost interface of the container it is running in, but redis is running in a different container.
Modify your app config to use the link name of the redis container (redis in your case) as hostname for the connection.
On macOS (using Homebrew), I was able to fix this by running:
brew services start redis
If you haven't yet installed redis, you'll need to run the following first:
brew install redis
I have solved this error using the following snippet
sudo apt install redis-server
and then by running the redis-server using —
redis-server
Redis quick review
Had a similar issue, in my case I have a script that starts redis and other initial settings and I forgot to run it. It usually breaks with CaS (Tomcat) but this time it broke due to redis and I had all but forgotten that was being setup by the script.
Anyway to complement other answers, a quick way to check if there are issues is to run
redis-cli
Which will let you know if redis is working and therefore help you remember if you need to run it.
If Redi isn't running you'll get a redis console:
redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
If it is running you'll get a redis console (in interactive mode), see documentation:
redis-cli
127.0.0.1:6379>
For example you can say: PING and it will return PONG
redis-cli
127.0.0.1:6379> PING
PONG
To run the Redis Server don't forget to just do:
redis-server
Just start your redis server by using redis-server command.
After start sidekiq
If you are using heroku and having such problem then you need to specify REDIS_PROVIDER_URL in your env
Make sure you installed redis-server on your system :-
sudo apt install redis-server

Resources