我是 rethinkdb 和 docker compose 的新手,并且一直在尝试。但是我在尝试使用 docker compose 运行一个小型数据迁移项目时遇到了问题。更具体地说,我在 docker 容器中运行了 jupyterlab,在另一个容器中运行了 rethinkdb,然后我将这两个容器组合在一起。虽然我可以看到 rethinkdb 在 localhost 8080 上运行,但我无法从复制到 jupyterlab 中的代码连接到它。这是我的代码,请帮忙!
---
version: "3.8"
services:
rethinkdb:
image: rethinkdb:2.4.4-bookworm-slim
#build: ./Rethinkdb
ports:
- 8080:8080
- 28015:28015
- 29015:29015
jupyterlab:
build: ./Python Codes
#build: ./Jupyterlab
ports:
- 8888:8888
volumes:
- ./Python Codes:/home/jovyan/work
这是我的 jupyterlab 的代码:
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
COPY . .
RUN pip install jupyter -U && pip install jupyterlab
RUN apt-get update && apt-get install -y libglib2.0-0 libgl1-mesa-glx && rm -rf /var/lib/apt/lists/*
RUN pip install -r requirements.txt
RUN pip install nodejs
EXPOSE 8888
CMD ["jupyter", "lab","--ip=0.0.0.0", "--no-browser", "--allow-root"]
我用来连接到我的 rethinkdb 的代码是:
rethink = r.RethinkDB()
rethink.connect('localhost', 29015).repl()
我收到的错误:
---------------------------------------------------------------------------
ConnectionRefusedError Traceback (most recent call last)
File /usr/local/lib/python3.9/site-packages/rethinkdb/net.py:349, in SocketWrapper.__init__(self, parent, timeout)
348 try:
--> 349 self._socket = socket.create_connection((self.host, self.port), timeout)
350 self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
File /usr/local/lib/python3.9/socket.py:844, in create_connection(address, timeout, source_address)
843 try:
--> 844 raise err
845 finally:
846 # Break explicitly a reference cycle
File /usr/local/lib/python3.9/socket.py:832, in create_connection(address, timeout, source_address)
831 sock.bind(source_address)
--> 832 sock.connect(sa)
833 # Break explicitly a reference cycle
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
ReqlDriverError Traceback (most recent call last)
Cell In[3], line 2
1 rethink = r.RethinkDB()
----> 2 rethink.connect('localhost', 28015).repl()
File /usr/local/lib/python3.9/site-packages/rethinkdb/__init__.py:90, in RethinkDB.connect(self, *args, **kwargs)
89 def connect(self, *args, **kwargs):
---> 90 return self.make_connection(self.connection_type, *args, **kwargs)
File /usr/local/lib/python3.9/site-packages/rethinkdb/net.py:830, in make_connection(connection_type, host, port, db, auth_key, user, password, timeout, ssl, url, _handshake_version, **kwargs)
816 password = None
818 conn = connection_type(
819 host,
820 port,
(...)
828 **kwargs
829 )
--> 830 return conn.reconnect(timeout=timeout)
File /usr/local/lib/python3.9/site-packages/rethinkdb/net.py:696, in Connection.reconnect(self, noreply_wait, timeout)
693 self.close(noreply_wait)
695 self._instance = self._conn_type(self, **self._child_kwargs)
--> 696 return self._instance.connect(timeout)
File /usr/local/lib/python3.9/site-packages/rethinkdb/net.py:538, in ConnectionInstance.connect(self, timeout)
537 def connect(self, timeout):
--> 538 self._socket = SocketWrapper(self, timeout)
539 return self._parent
File /usr/local/lib/python3.9/site-packages/rethinkdb/net.py:437, in SocketWrapper.__init__(self, parent, timeout)
435 except Exception as ex:
436 self.close()
--> 437 raise ReqlDriverError(
438 "Could not connect to %s:%s. Error: %s"
439 % (self.host, self.port, str(ex))
440 )
ReqlDriverError: Could not connect to localhost:28015. Error: [Errno 111] Connection refused
请帮忙,非常感谢!
我尝试了端口29015和28015。我还尝试运行jupyter/minimal-notebook而不是自行构建的jupyterlab,但是当我尝试这个时,当我复制到jupyterlab时我无法运行我的代码。我怀疑这与我的 jupyter 有关,但我可能是错的......
这是您的问题:
rethink.connect('localhost', 28015).repl()
。你应该尝试使用 rethink.connect('rethinkdb', 28015).repl()
在容器内运行时,每个容器通常都有自己独立的网络命名空间。这意味着容器中的“localhost”指的是容器本身,而不是主机或其他容器。因此
jupyterlab
应用程序无法查看 rethinkdb
服务,因为 28015 端口暴露于您自己的本地计算机,并且从您自己的本地主机可见,但从容器中看不到。
默认情况下,您使用的 Docker Compose 会为您的应用程序设置单个网络。服务的每个容器都加入默认网络,并且可以被该网络上的其他容器访问,并且可以通过服务的名称发现。 (https://docs.docker.com/compose/networking/)