Search code examples
pythondocker-composesolrnetwork-programmingpysolr

Connecting to Solr using pysolr gives Errno 111: Connection refused


I have a Flask app that should connect to Solr using pysolr:

import flask as f
import pysolr

app = f.Flask(__name__)
solr = pysolr.Solr('http://localhost:8983/solr/')


@app.route("/ping")
def ping():
    return solr.ping()

But running it gives me the error:

pysolr.SolrError: Failed to connect to server at http://localhost:8983/solr/admin/ping/?: HTTPConnectionPool(host='localhost', port=8983): Max retries exceeded with url: /solr/admin/ping/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f1a085f6510>: Failed to establish a new connection: [Errno 111] Connection refused'))

(truncated because the traceback is extremely long and gives nothing more of much value)

I know that Solr is running, because I can access http://localhost:8983/solr on my web browser just fine.

This occurs no matter whether I reference it in my code as localhost, 0.0.0.0 or 127.0.0.1.

I am running both Solr and my webapp inside docker-compose, but I've had the same issue before I used Docker. However, in the event that it's relevant, here's my docker-compose.yml:

version: '3'

services:

  solr:
    image: solr
    ports:
     - "8983:8983"
    volumes:
      - ./data:/var/solr/
    entrypoint:
    - bash
    - "-c"
    - "precreate-core stable; precreate-core unstable; precreate-core archive; exec solr -f"

  frontend:
    build: ./src
    ports:
      - "80:80"
    depends_on:
      - solr

volumes:
  data:


Solution

  • This ended up being due to Docker after all. Since it's running frontend and solr as two separate services, you can access the Solr backend by using the hostname solr like so:

    solr = pysolr.Solr("http://solr:8983/solr/<core_name>")