Search code examples
ruby-on-railsdockersolrdocker-composesunspot-rails

Solr on a separate docker container with sunspot-rails


I am trying to move a working Rails app to docker environment.

Following the UNIX(/docker) philosophy I would like to have each service in its own container.

I managed to get redis and postgres working fine, but I am struggling to get slor and rails talking to each other.

In file app/models/spree/sunspot/search_decorator.rb when the line executes

@solr_search.execute

the following error appear on the console:

Errno::EADDRNOTAVAIL (Cannot assign requested address - connect(2) for "localhost" port 8983):

While researching for a solution I have found people just installing solr in the same container as their rails app. But I would rather have it in a separate container.

Here are my config/sunspot.yml

development:
  solr:
    hostname: localhost
    port: 8983
    log_level: INFO
    path: /solr/development

and docker-compose.yml files

version: '2'
services:
  db:
    (...)

  redis:
    (...)

  solr:
    image: solr:7.0.1

    ports:
      - "8983:8983"

    volumes:
      - solr-data:/opt/solr/server/solr/mycores

    entrypoint:
      - docker-entrypoint.sh
      - solr-precreate
      - mycore

    networks:
      - backend

  app:
    build: .

    env_file: .env

    environment:
      RAILS_ENV: $RAILS_ENV

    depends_on:
      - db
      - redis
      - solr

    ports:
      - "3000:3000"

    tty: true

    networks:
      - backend

volumes:
  solr-data:
  redis-data:
  postgres-data:

networks:
  backend:
    driver: bridge

Any suggestions?


Solution

  • Your config/sunspot.yml should have the following:

    development:
      solr:
        hostname: solr # since our solr instance is linked as solr
        port: 8983
        log_level: WARNING
        solr_home: solr
        path: /solr/mycore 
        # this path comes from the last command of our entrypoint as
        # specified in the last parameter for our solr container
    

    If you see

    Solr::Error::Http (RSolr::Error::Http - 404 Not Found
    Error:     Not Found
    
    URI: http://localhost:8982/solr/development/select?wt=json
    

    Create a new core using the admin interface at:

    http://localhost:8982/solr/#/~cores

    or using the following command:

    docker-compose exec solr solr create_core -c development
    

    I wrote a blog post on this: https://gaurav.koley.in/2018/searching-in-rails-with-solr-sunspot-and-docker

    Hopefully that helps those who come here at later stage.