Please go easy on me here, My experience with ElasticSearch is pretty much solely with use of SearchKick and my own Rails app.
I have a Rails app which I have been running locally on my machine using SearchKick and ElasticSearch for quite some time now.
Recently I’ve been reading about how to convert my dev environment into a Dockerised form.
I have got my Rails app and its database working nicely but then I came to add Search back in .
My Docker Compose file is below:
version: '3.7'
services:
app:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/app
- bundle-volume:/usr/local/bundle
- yarn-volume:/app/node_modules
ports:
- "3000:3000"
depends_on:
- db
- search
db:
image: 'postgres:9.6'
env_file:
- .env.development
volumes:
- db-volume:/var/lib/postgresql/data
ports:
- "5432:5432"
# environment:
# - POSTGRES_HOST_AUTH_METHOD=trust
search:
image: 'elasticsearch'
env_file:
- .env.development
volumes:
- 'search-volume:/usr/share/elasticsearch/data'
ports:
- '9200:9200'
volumes:
bundle-volume:
yarn-volume:
db-volume:
search-volume:
When I start the container and head to the Rails console to try and run <Model>.reindex I get the following error:
Traceback (most recent call last): 1: from (irb):1
Faraday::ConnectionFailed (Failed to open TCP connection to
localhost:9200 (Cannot assign requested address - connect(2) for
“localhost” port 9200))
I have set an Environment var like so in my .env.development file:
ELASTICSEARCH_URL=http://search:9200
The error shows it’s trying to connect to localhost which is exactly where I think the issue lays.
It doesn’t look like the variable i’m setting in my .env.development file is being used in the way it should be.
Anyone with an in-depth knowledge of Docker able to help me out here?
Thanks,
Neil
EDIT:
Dockerfile:
FROM ruby:2.6.5
# Install 3rd party dependencies.
RUN 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 update && \
apt -y install nodejs \
yarn \
build-essential \
libpq-dev
# Sets Environment variables
ENV RAILS_ROOT /app
ENV PORT 3000
# Working directory.
RUN mkdir $RAILS_ROOT
WORKDIR $RAILS_ROOT
# Script run when container first starts.
COPY docker-entrypoint.sh docker-entrypoint.sh
RUN chmod +x docker-entrypoint.sh
ENTRYPOINT [ "/app/docker-entrypoint.sh" ]
docker-entrypoint.sh:
#!/bin/bash
set -e
bundle install
yarn install --check-files --frozen-lockfile
# Remove a potentially pre-existing server.pid for Rails.
rm -f /app/tmp/pids/server.pid
# Run the command.
exec "$#"
.env:
ELASTICSEARCH_URL=http://search:9200
DATABASE_HOST=db
DATABASE_USER=postgres
DATABASE_PASSWORD=postgres
DATABASE_NAME=mysite_development
DATABASE_PORT=5432
Gemfile:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.3'
# Use postgresql as the database for Active Record
gem 'pg', '~> 1.2.3'
# Use Puma as the app server
gem 'puma', '~> 4.3.5'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 6.x'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbo-rails', '~> 0.5.11'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
gem 'redis', '~> 4'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
gem 'devise' # Authorisation and Authentication
gem 'httparty' # Make HTTP requests
gem 'searchkick', '~> 4.5.0' # ElasticSearch searching
gem 'geocoder' # Allows models to have a location
gem 'gretel' #Breadcrumb functionality
gem 'nav_lynx' # Navigation links
gem 'inline_svg', '~> 1.7.2' # Inlines SVG files
gem 'flickraw' # Integration with the Flickr API
gem 'countries', require: 'countries/global' # Country list (with helper to omit ISO3166:: module prefix)
gem 'kaminari' # Pagination
gem 'flutie' # Utility classes
gem 'acts-as-taggable-on', '~> 7.0.0' # Tagging
gem 'friendly_id', '~> 5.4'
gem 'acts_as_list' # Ordering
gem 'delayed_job_active_record' # Background Jobs
gem 'foreman'
gem 'daemons'
gem 'delayed_job_web'
gem 'activerecord-hierarchical_query', '~> 1.3.0'
gem 'roo', '~> 2.8.3'
gem 'faker'
group :development, :test do
gem 'dotenv-rails'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'rspec-rails'
gem "bullet"
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.4'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.1'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'capistrano', '~> 3.11'
gem 'capistrano-rails', '~> 1.4'
gem 'capistrano-passenger', '~> 0.2.0'
gem 'capistrano-rbenv', '~> 2.1', '>= 2.1.4'
gem 'ed25519'
gem 'bcrypt_pbkdf'
gem 'rubocop', require: false
gem 'rubocop-rails', require: false
EDIT 2:
Latest error message:
Traceback (most recent call last): 1: from (irb):1
Faraday::ConnectionFailed (Failed to open TCP connection to
search:9200 (getaddrinfo: No address associated with hostname))
EDIT 3:
So, after getting my machine back today, I tried your suggestions. It turns out the search container was erroring. The error is pasted below:
search_1 | [2021-08-04T20:44:40,178][WARN
][o.e.b.ElasticsearchUncaughtExceptionHandler] [unknown] uncaught
exception in thread [main] search_1 |
org.elasticsearch.bootstrap.StartupException:
ElasticsearchException[java.io.IOException: failed to read [id:7,
file:/usr/share/elasticsearch/data/nodes/0/_state/node-7.st]]; nested:
IOException[failed to read [id:7,
file:/usr/share/elasticsearch/data/nodes/0/_state/node-7.st]]; nested:
XContentParseException[[-1:36] [node_meta_data] unknown field
[node_version], parser not found];
It looks like it's failing due to the location the Volume is set to. I temporarily commented out the Volume configuration for that service and my Rails model now gets indexed.
I'm assuming now though, that if I close the container and start it up again that the Index will have been removed because it's no longer stored in a Volume?
Any ideas how I can get the index data stored in a volume and let the container work?
Related
Want to achieve
Thank you for browsing.
I got the following error when using "bundle install" to build with CircleCI.
In my environment, I can install the bundle without any problems.
I hope you can tell me how to solve this problem.
error
To see why this extension failed to compile, please check the mkmf.log which can
be found here:
/home/circleci/myapp/web/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.0-static/mysql2-0.5.2/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in
/home/circleci/myapp/web/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.2 for
inspection.
Results logged to
/home/circleci/myapp/web/vendor/bundle/ruby/2.6.0/extensions/x86_64-linux/2.6.0-static/mysql2-0.5.2/gem_make.out
An error occurred while installing mysql2 (0.5.2), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'`
succeeds before bundling.
In Gemfile:
mysql2
Exited with code exit status 5
CircleCI received exit code 5
Codes
.circleci/config.yml
version: 2.1
orbs:
ruby: circleci/ruby#1.1.2
jobs:
build:
docker:
- image: cimg/ruby:2.6.5
working_directory: ~/myapp/web
steps:
- checkout:
path: ~/myapp
- ruby/install-deps
workflows:
version: 2
build_and_test:
jobs:
- build
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.7'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# jQuery
gem "jquery-rails"
gem 'rails-i18n', '~> 6'
gem "enum_help"
gem "config"
gem 'faraday'
gem 'acts_as_paranoid', '~> 0.6.0'
gem 'http-cookie'
gem 'kaminari'
gem 'devise'
gem 'devise_token_auth'
gem 'devise-security'
gem 'devise-two-factor', '~> 3.1'
# AWS
gem 'aws-sdk'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
gem 'composite_primary_keys'
# Antivirus
gem 'clamav-client', require: 'clamav/client'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
I tried
Mention to run gem install mysql2 -v before ruby/install-deps
- run:
name: Install mysql2
command: gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'
- ruby/install-deps
↓
build
↓
Another error occurred.
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/home/circleci/.rubygems/extensions/x86_64-linux/2.6.0-static/mysql2-0.5.2/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /home/circleci/.rubygems/gems/mysql2-0.5.2 for inspection.
Results logged to /home/circleci/.rubygems/extensions/x86_64-linux/2.6.0-static/mysql2-0.5.2/gem_make.out
Exited with code exit status 1
CircleCI received exit code 1
I modified the config.yml as follows and was able to build
steps:
- checkout:
path: ~/myapp
- run:
name: default mysql client install
command: |
sudo apt update
sudo apt-get install default-mysql-client
sudo apt-get install libmysqlclient-dev
- run:
name: bundle Install
command: bundle install --path vendor/bundle
Here is the Gemfile:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.0.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.0'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Flexible authentication solution for Rails with Warden
gem 'devise', '~> 4.7'
# A simple HTTP and REST client
gem 'rest-client', '~> 2.1'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
# Devise supports i18n in controllers, models, and in other areas,
# but it does not have support for internationalized views.
# devise-i18n adds this support.
gem 'devise-i18n', '~> 1.9'
# Devise Bootstrap Views
gem 'devise-bootstrap-views', '~> 1.0'
# Configure Devise to send its emails asynchronously using ActiveJob
gem 'devise-async', '~> 1.0'
# Stripe is the easiest way to accept payments online
gem 'stripe', '~> 5.22'
# A gem that provides a client interface for the Sentry error logger
gem 'sentry-raven', '~> 3.0'
# Taming Rails' Default Request Logging
gem 'lograge', '~> 0.11'
# Ruby state machines
gem 'aasm', '~> 5.0'
# Allows to use ActiveRecord transactional callbacks outside of ActiveRecord models, literally everywhere in your application.
gem 'after_commit_everywhere', '~> 0.1', '>= 0.1.5'
# Agnostic pagination in plain ruby
gem 'pagy', '~> 3.8'
# Connection Pool
gem 'connection_pool', '~> 2.2'
# Redis client
gem 'hiredis', '~> 0.6'
gem 'redis', '~> 4.1', require: ['redis', 'redis/connection/hiredis']
# Sidekiq
gem 'sidekiq', '~> 6.0'
# Check password strength against several rules
gem 'password_strength', '~> 1.1'
# Slack Ruby Client
gem 'slack-ruby-client', '~> 0.15'
# Assets on AWS S3/CloudFront
gem 'asset_sync', '~> 2.12'
gem 'fog-aws', '~> 3.6'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0'
gem 'rspec-rails', '~> 4.0'
gem 'amazing_print', '~> 1.2'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'capybara-screenshot'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
gem 'factory_bot_rails', '~> 6.0'
gem 'shoulda-matchers', '~> 4.1'
gem 'rails-controller-testing'
gem 'faker'
gem 'database_cleaner-active_record'
gem 'coderay'
gem 'mock_redis'
end
The application seems to be blocked during Rails.application.initialize!.
I wonder if it could be a gem that is not compatible with Ruby 3.0, but I started a new app from scratch, installed all the gems and the app started normally.
I'm using puma (5.1.1) as web server.
The app is running in docker container via docker-compose.
FROM ruby:2.7.2
# Update for security reason
RUN apt-get update -yqq && apt-get upgrade -yqq -o Dpkg::Options::="--force-confold" && apt-get install -yqq --no-install-recommends \
vim # needed for editing rails credentials
# Ensure we install an up-to-date version of Node
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
# Ensure latest packages for Yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
nodejs \
yarn
ENV RAILS_ROOT /var/www/app
RUN mkdir -p $RAILS_ROOT
COPY Gemfile* /var/www/app/
WORKDIR /var/www/app
ENV BUNDLE_PATH /gems
RUN bundle install
COPY package.json package.json
COPY yarn.lock yarn.lock
RUN yarn install --check-files
COPY . /var/www/app/
EXPOSE 3000
# Clean up APT when done.
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENTRYPOINT ["./puma-entrypoint.sh"]
CMD ["bundle", "exec", "puma", "-C", "config/puma/development.rb"]
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
threads min_threads_count, max_threads_count
# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }
# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
# preload_app!
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
Is there a way to have logs to see what is happening during boot process?
Please give more information.
On which system are you?
How do you start your application? If you get in trouble, start it always on console.
Puma has a log output. Search for this location. Put log output here.
Try to remove all gems or big part of it to find error.
Researching for this topic, I realized that there isn't much useful information, so just trying to contribute for this cause...
You don't give too much information, but try again changing FROM ruby:2.7.2 to FROM ruby: 3.0.0 in your Dockerfile. Also check that Bundler is still working with ruby 3.0, if it's not, try updating this to v2.2.
https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/
https://bundler.io/blog/
Error when running rails docker container with volumes
bundler: failed to load command: rails (/usr/local/bundle/bin/rails)
Bundler::GemNotFound: Could not find rake-12.3.2 in any of the sources
I am able to run my rails docker container without volume.
But when i attach volume as such:
docker run --name rails-chat-tutorial-web \
-e DATABASE_HOST=172.17.0.1 \
-e DATABASE_PORT=5432 \
-e DATABASE_USERNAME=postgres \
-e DATABASE_PASSWORD=postgres \
-e REDIS_URL=redis://172.17.0.1:6379/1 \
-p 3000:3000 \
-v $(pwd):/application rails-chat-tutorial
I will get this error output:
bundler: failed to load command: rails (/usr/local/bundle/bin/rails)
Bundler::GemNotFound: Could not find rake-12.3.2 in any of the sources
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler/spec_set.rb:87:in `block in materialize'
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler/spec_set.rb:81:in `map!'
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler/spec_set.rb:81:in `materialize'
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler/definition.rb:170:in `specs'
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler/definition.rb:237:in `specs_for'
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler/definition.rb:226:in `requested_specs'
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler/runtime.rb:108:in `block in definition_method'
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler/runtime.rb:20:in `setup'
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler.rb:107:in `setup'
/usr/local/bundle/gems/bundler-2.0.1/lib/bundler/setup.rb:20:in `<top (required)>'
/usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
I have tried including these lines in my Dockerfile but still get the error:
RUN gem install rake -v '12.3.2'
RUN bundle install --binstubs
RUN bundle install --path vendor/bundle
RUN bundle install --local
RUN bun
dle install --local --path=vendor/cache
This is my Dockerfile:
FROM ruby:2.5.0-stretch
COPY ./Gemfile ./application/
COPY ./Gemfile.lock ./application/
WORKDIR /application
ENV BUNDLER_VERSION 2.0.1
RUN gem install bundler -v '2.0.1'
RUN bundle install --deployment --without development test
RUN apt-get update -qq && apt-get install -y build-essential
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get install -y nodejs
RUN bundle install --local --path=vendor/cache
RUN npm install yarn -g
COPY . .
ENV RAILS_ENV production
ENV SECRET_KEY_BASE production_test_key rails c
RUN bundle exec rake assets:precompile
EXPOSE 3000
CMD bundle exec rails server
Content of Gemfile:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end
group :production do
gem 'pg'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'devise'
gem 'bootstrap', '~> 4.3.1'
gem 'jquery-rails'
gem 'simple_form'
gem 'redis'
gem 'httparty', '~> 0.17.0'
gem 'rake', '12.3.2'
If I run the rails container with shell and gem list, i get 'rake (12.3.2, 12.3.0)'
I have been on this for the past 2 days but no progress.
Thank you in advance for those who are able to provide some guidance.
When you add a docker run -v $(pwd):/application option, it hides everything in the /application directory in the image and replaces it with the content from your host system. That in particular includes the /application/vendor directory: any bundle commands in your Dockerfile are completely ignored, and your host system's ./vendor directory gets used instead.
There's not really a good answer to this, if you must have live editing and reloading in your deployed container. The Node ecosystem is similar (third-party libraries are in ./node_modules) and most similar questions are about Node rather than Ruby. Add a volume to Docker, but exclude a sub-folder suggests adding an anonymous volume for ./vendor; the first time only that you run your application it will get populated from the image, but if you later change your Gemfile it won't get updated and replicating this setup is unnecessarily complicated in cluster environments like Kubernetes.
If you want to try the anonymous-volume path, that could look like
docker run --name rails-chat-tutorial-web ... \
-v $PWD:/application -v /application/vendor \
rails-chat-tutorial
$EDITOR Gemfile
bundle install
docker stop rails-chat-tutorial-web
docker rm -v rails-chat-tutorial-web
docker run ...
(docker rm -v will delete the anonymous volume, and it will get recreated on the next docker run with updated contents. You've told Docker that directory contains important non-code data that must be preserved across runs.)
The sequence that's worked well for me is to develop my application in total ignorance of Docker: develop it locally, run it, write good rspec tests, and generally believe it works. Only then do I docker build and docker run an image, without bind-mounting my source code into it. If it breaks then I reproduce the issue on my local development environment, write a test for it, and fix it, then repeat the docker build; docker run sequence.
When I run docker-compose up -d I get the following error:
web_1 | Could not locate Gemfile or .bundle/ directory
However the image is being created. I want to have a Rails application, and for the Docker files to live outside the rails application. I have the following Dockerfile
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
WORKDIR /app
COPY ./app/Gemfile Gemfile
# Install Gems
RUN bundle install
ADD . /app
In the docker-compose.yml file I have the Following configuration:
version: '3'
services:
db:
image: postgres:9.6.1 # To Edit - Default is postgresql.
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
And for the Gemfile I have the following code:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use postgresql as the database for Active Record
gem 'pg'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15', '< 4.0'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
I'm not sure if it has something to do with the Rails or Ruby version. I also see that the Gemfile is there once I go into the container and run ls so is not that theres no Gemfile.
root#ab38d643df47:/app# ls
Dockerfile Gemfile Gemfile.lock README.md app docker-compose.yml
try this:
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
WORKDIR app
COPY Gemfile Gemfile
# Install Gems
RUN bundle install
ADD . app
I am trying to build a docker container but getting the following error message:
Step 8/12 : RUN bundle binstubs bundler --force
---> Running in ed94b127974b
Could not find gem 'rails (>= 5.1.5, ~> 5.1)' in any of the gem sources listed
in your Gemfile.
ERROR: Service 'dockerzon' failed to build: The command '/bin/sh -c bundle binstubs bundler --force' returned a non-zero code: 7
I have tried with different versions of rails in my Gemfile with no success! This is what I have in my Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
# gem 'rails', '4.2.6'
# gem 'rails', '>= 5.1.5'
gem 'rails', '~> 5.1', '>= 5.1.5'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0.7'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 4.1.6'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 1.0.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 3.5.1'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
gem 'pg', '~> 1.0.0'
gem 'redis-rails', '~> 5.0.2'
gem 'sidekiq', '~> 5.1.1'
gem 'puma', '~> 3.11.2'
gem 'rack-timeout', '~> 0.4.2'
and this is my Dockerfile:
# Use the barebones version of Ruby 2.5
FROM ruby:2.5-slim
# Optionally set a maintainer name to let people know who made this image.
# Install dependencies:
# - build-essential: To ensure certain gems can be compiled
# - nodejs: Compile assets
# - libpq-dev: Communicate with postgres through the postgres gem
RUN apt-get update && apt-get install -qq -y --no-install-recommends \
build-essential nodejs libpq-dev
# Set an environment variable to store where the app is installed to inside
# of the Docker image. The name matches the project name out of convention only.
ENV INSTALL_PATH /dockerzon
RUN mkdir -p $INSTALL_PATH
# This sets the context of where commands will be ran in and is documented
# on Docker's website extensively.
WORKDIR $INSTALL_PATH
# Ensure gems are cached and only get updated when they change. This will
# drastically increase build times when your gems do not change.
COPY Gemfile Gemfile
# We want binstubs to be available so we can directly call sidekiq and
# potentially other binaries as command overrides without depending on
# bundle exec.
# RUN bundle install --binstubs
RUN bundle binstubs bundler --force
# Copy in the application code from your work station at the current directory
# over to the working directory.
COPY . .
# Provide a dummy DATABASE_URL to Rails so it can pre-compile assets.
RUN bundle exec rake RAILS_ENV=production DATABASE_URL=postgresql://user:pass#127.0.0.1/dbname SECRET_TOKEN=dummytoken assets:precompile
# Ensure the static assets are exposed through a volume so that nginx can read
# in these values later.
VOLUME ["$INSTALL_PATH/public"]
# The default command that gets ran will be to start the Puma server.
CMD bundle exec puma -C config/puma.rb
Try to update the version of your RubyGems:
gem update --system
Then change the line in Gemfile to:
gem 'rails', '~> 5.1.5'
Now run bundler to install the gems:
bundle install
Add below lines in your Dockerfile
RUN gem install rails
ruby:2.5-slim this docker image doesn't contain rails. It only contains ruby. You can't install rails using Gemfile. You have to install rails before bundling your gemfile.