Ben S
Ben S

Reputation: 11

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

Upvotes: 1

Views: 581

Answers (2)

mariotux
mariotux

Reputation: 36

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: {}

Upvotes: 0

ZedTuX
ZedTuX

Reputation: 3027

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.

Upvotes: 2

Related Questions