Python couchbase连接在尝试访问docker中的数据库时引发错误

2024-06-26 13:58:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个python脚本中得到以下错误,该脚本首先在docker中启动couchbase实例,然后尝试连接到couchbase数据库

如果我已经单独启动了容器,那么python couchbase连接就可以正常工作,但是如果代码试图在同一个python脚本中启动docker couchbase容器和连接,则会抛出连接错误

这是错误

Traceback (most recent call last):
  File "/Users/APatel/Documents/playground/exapp/cb_connect_example.py", line 38, in <module>
    cluster = Cluster('couchbase://localhost', ClusterOptions(authenticator))
  File "/Users/APatel/.local/share/virtualenvs/exapp-oN-J56Zb/lib/python3.6/site-packages/couchbase/cluster.py", line 490, in __init__
    super(Cluster, self).__init__(connection_string=str(self.connstr), _conntype=_LCB.LCB_TYPE_CLUSTER, **self._clusteropts)
  File "/Users/APatel/.local/share/virtualenvs/exapp-oN-J56Zb/lib/python3.6/site-packages/couchbase_core/client.py", line 159, in __init__
    self._do_ctor_connect()
  File "/Users/APatel/.local/share/virtualenvs/exapp-oN-J56Zb/lib/python3.6/site-packages/couchbase/cluster.py", line 510, in _do_ctor_connect
    super(Cluster,self)._do_ctor_connect(*args,**kwargs)
  File "/Users/APatel/.local/share/virtualenvs/exapp-oN-J56Zb/lib/python3.6/site-packages/couchbase_core/client.py", line 168, in _do_ctor_connect
    self._connect()
couchbase.exceptions.LCB_0x401 (generated, catch: BaseException, KeyValueException, AnalyticsException, ViewException): <RC=0x401[LCB_ERR_SOCKET_SHUTDOWN (1025)], There was a problem while trying to send/receive your request over the network. This may be a result of a bad network or a misconfigured client or server, C Source=(src/bucket.c,1209)>

下面是python代码

from couchbase.cluster import Cluster, ClusterOptions
from couchbase_core.cluster import PasswordAuthenticator
import docker
import time

client = docker.from_env()


def remove_e2e_container():
    docker_containers = client.containers.list()
    for container in docker_containers:
        if container.name == 'e2e_container':
            container.remove(force=True)


def create_container():
    remove_e2e_container()
    client.containers.run('bentonam/couchbase-docker', name='e2e_container', detach=True,
                          environment=["CLUSTER_RAMSIZE=500", "BUCKET_RAMSIZE=500",
                                       "BUCKET=accums_e2e_test",
                                       "SERVICES: data,index,query"],
                          ports={'8091/tcp': 8091, '8092/tcp': 8092, '8093/tcp': 8093, '8094/tcp':
                              8094, '11210/tcp': 11210})


create_container()


config = {
    'login': 'Administrator',
    'password': 'password',
    'host': "couchbase://localhost",
    'schema': 'accums_e2e_test'
}

authenticator = PasswordAuthenticator(config.get('login'), config.get('password'))
cluster = Cluster('couchbase://localhost', ClusterOptions(authenticator))
cb = cluster.bucket('accums_e2e_test')

results = cb.query("CREATE PRIMARY INDEX ON `accums_e2e_test`")


for i in results:
    print(i)

Tags: dockerinpyselfclientcontainerconnectline