Setup Solr and Drupal using Docker

Last time I tried to create from scratch a Drupal instance that used a Solr server wasn’t as easy as I expected. Maybe command line tools for installing modules have spoiled me and I just expected that running something like drupal module:install solr would configure everything for me.

Here is how I made it work, but please consider I’m not a Solr expert. If you know a better way to do this please let me know.

Setup Drupal

Let’s suppose we want to create a blog that uses a Solr for searching its contents.

First, to create a basic Drupal install we can use a recently released project from WeKnow to quickly create a Drupal instance using Docker. Follow the README file keeping the default .env values so we can install Drupal using this command:

$ docker-compose exec —user=82 php drupal site:install standard —db-host=mariadb —db-name=drupal —db-user=drupal —db-pass=drupal —quiet

Don’t forget to add the hosts entry, as mentioned in the README file, or use a proxy like hotel. The objective is to use a custom domain name that the web server can identify (if we use localhost and in the .env file we used HOST_NAME=drupal.vm the web server won’t serve the request).

You can verify it is working if you see a fresh Drupal install when you visit http://drupal.develop.

Install Solr

Let’s install Solr now. We will need some config files found in the module to setup our Solr core, so lets download it inside the container:

$ docker-compose exec --user=82 php composer req drupal/search_api_solr

We will create a sample core named *blog*. Copy the files located at web/modules/contrib/search_api_solr/solr-conf/5.x/ to env/solr/blog/conf/ and create this file in env/solr/blog/core.properties (you will need to create the env dir):

env/solr/blog/core.properties

    name=blog
    config=solrconfig.xml
    schema=schema.xml
    dataDir=/var/lib/solr/books

Update docker-compose.yml to include the solr container and config its port and a way to access it using traefik (using http://solr.drupal.develop if using the defaults in .env):

docker-compose.yml

    services:
      # ...
      solr:
        image: solr:5.5
        labels:
          - 'traefik.backend=solr'
          - 'traefik.port=8983'
          - 'traefik.frontend.rule=Host:solr.${HOST_NAME}'
        volumes:
          - ./env/solr/blog:/opt/solr/server/solr/blog
          - solrdata:/var/lib/solr

    volumes:
      mysqldata:
        driver: "local"
      solrdata:
        driver: "local"

Don’t forget to add the solr.drupal.develop to your hosts file).

After running docker-compose up -d (and adding to your hosts file the entry for solr.drupal.development) you will see this error in solr:

This happened because the user and the uid has to match with the user the solr process runs as in the solr container. To fix this we have to create another container that fixes the perms where the core will write the index.

We will use just-containers/base-alpine image, which has utilities to do this (s6).

Add the docker file to update fix the perms and update docker-compose to use this new container:

./dockerfiles/solr_data/Dockerfile

    FROM just-containers/base-alpine
    VOLUME ["/var/lib/solr"]
    ADD root /

./dockerfiles/solr_data/root/etc/fixattrs.d

    /var/lib/solr false solr,8983 0755

And update the docker-compose.yml to include this image:

    services:
      solr:
        image: solr:5.5
        labels:
          - 'traefik.backend=solr'
          - 'traefik.port=8983'
          - 'traefik.frontend.rule=Host:solr.${HOST_NAME}'

        # add the volumes from the new container
        volumes_from:
          - solr_data
        # the core configs
        volumes:
          - ./env/solr/blog:/opt/solr/server/solr/books

      # added container to fix perms
      solr_data:
        build: ./dockerfiles/solr_data
        volumes:
          - real_solr_data:/var/lib/solr

    volumes:
      mysqldata:
        driver: "local"
      real_solr_data:
        driver: "local"

Now if you run docker-compose up -d and you visit the Solr page you should see your errors fixed.

Configure Solr

Now that we have the server running, lets configure Drupal so we can use it as a search engine. Fortunately, most of the hard work is solved by enabling search_api_solr_default module.

    $ docker-compose exec --user=82 php drupal moi search_api_solr_defaults

Go to the Search API configs (/admin/config/search/search-api) and you will see a new Solr server entry, although with errors because it is missing the server name and the core it will use.

Click edit and set the host (ie. the solr container name, solr) and the core we created before (blog):

Click save and you should be able to to see all green when you go back to the Search API config page. You can now start using your solar server for searching.

The defaults module contains a sample search page to easily get you started in /solr-search/content. It is implemented as a View.

References

You might find this links useful:

Permalink: Setup Solr and Drupal using Docker