I am using OpenSearch in docker desktop (Windows).I was trying to connect my python code running on google colab with the OpenSearch instance running in docker desktop.
Steps of installing Opensearch with docker-compose.yml file-
https://opensearch.org/downloads.html
https://opensearch.org/docs/latest/security-plugin/configuration/disable/
Code-
!pip install opensearch-py
from opensearchpy import OpenSearch
host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
# ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.
# Optional client certificates if you don't want to use HTTP basic authentication.
# client_cert_path = '/full/path/to/client.pem'
# client_key_path = '/full/path/to/client-key.pem'
# Create the client with SSL/TLS enabled, but hostname verification disabled.
client = OpenSearch(
hosts = [{'host': host, 'port': port}],
http_compress = True, # enables gzip compression for request bodies
http_auth = auth,
# client_cert = client_cert_path,
# client_key = client_key_path,
use_ssl = True,
verify_certs = True,
ssl_assert_hostname = False,
ssl_show_warn = False,
# ca_certs = ca_certs_path
)
# Create an index with non-default settings.
index_name = 'python-test-index'
index_body = {
'settings': {
'index': {
'number_of_shards': 4
}
}
}
response = client.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response)
Error-
---------------------------------------------------------------------------
ConnectionRefusedError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/urllib3/connection.py in _new_conn(self)
158 conn = connection.create_connection(
--> 159 (self._dns_host, self.port), self.timeout, **extra_kw)
160
16 frames
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
NewConnectionError Traceback (most recent call last)
NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f1f3be83690>: Failed to establish a new connection: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/opensearchpy/connection/http_urllib3.py in perform_request(self, method, url, params, body, timeout, ignore, headers)
262 if isinstance(e, ReadTimeoutError):
263 raise ConnectionTimeout("TIMEOUT", str(e), e)
--> 264 raise ConnectionError("N/A", str(e), e)
265
266 # raise warnings if any from the 'Warnings' header.
ConnectionError: ConnectionError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1f3be83690>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.VerifiedHTTPSConnection object at 0x7f1f3be83690>: Failed to establish a new connection: [Errno 111] Connection refused)
Found similar query on another site-
Opensearch instances running on Docker Desktop.
I searched on internet but didn't find answer, pls let me know.
Snap-
Thank You
Issue was I was trying to connect connect opensearch docker instance using google colab notebooks.
After running the script in python virtual env, I am able to connect docker instance.
Ref link- Docker : Opensearch refuses connection with the example in opensearch documentation in docker
Working code-
from opensearchpy import OpenSearch
def connect_to_os():
host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
# ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.
# Optional client certificates if you don't want to use HTTP basic authentication.
# client_cert_path = '/full/path/to/client.pem'
# client_key_path = '/full/path/to/client-key.pem'
# Create the client with SSL/TLS enabled, but hostname verification disabled.
try:
client = OpenSearch(
hosts = [{'host': host, 'port': port}],
http_compress = True, # enables gzip compression for request bodies
http_auth = auth,
# client_cert = client_cert_path,
# client_key = client_key_path,
use_ssl = True,
verify_certs = False,
ssl_assert_hostname = False,
ssl_show_warn = False,
# ca_certs = ca_certs_path
)
except BaseException:
#connection error; send error message
print("ERROR unable to connect opensearch instance")
return False
return client
# Create an index with non-default settings.
index_name = 'python-test-index'
index_body = {
'settings': {
'index': {
'number_of_shards': 4
}
}
}
client = connect_to_os()
response = client.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response)