我有一个 dockerized 的 python 脚本,在脚本中我调用了 ArangoDB。我有另一个运行 ArangoDB 映像的容器。当我运行这两个容器时,我可以在我的本地主机上访问 ArangoDB,如果我从其容器外部运行我的 python 脚本,它可以成功调用 ArangoDB。但是,当我从 docker 容器内运行脚本时,它无法连接到容器化的 ArangoDB,因为它没有识别出服务器已启动并正在运行,我收到以下错误:
File "/usr/local/lib/python3.7/site-packages/arango/client.py", line 167, in db
raise ServerConnectionError('bad connection: {}'.format(err))
arango.exceptions.ServerConnectionError: bad connection: HTTPConnectionPool(host='127.0.0.1', port=8529): Max retries exceeded with url: /_db/dox/_api/collection (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f17a4db3450>: Failed to establish a new connection: [Errno
111] Connection refused'))
这是我用于 python 脚本的 dockerfile
FROM python:3.7-buster
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
CMD [ "python", "main.py" ]
这就是我从我的 python 脚本中调用 ArangoDB 的地方:
class ArangoDB():
def __init__(self, db, username, password):
"""
Creates an ArangoDB session and connects to a specified Arango database.
Parameters
----------
db : str
Arango database name
username : str
Arango database username
password : str
Arango database password
Returns
-------
An instance of ArangoDB API Wrapper
"""
logger.info(f'User {username} Connecting to database: {db}')
self._conn = ArangoClient()
self._db = self._conn.db(name=db, username=username, password=password,verify=True)
logger.info(f"Successfully connected to database: {db}")
最后,这是我的 docker-compose.yml
version: "3.8"
services:
arangodb:
image: arangodb:latest
container_name: db
environment:
ARANGO_ROOT_PASSWORD: route66
ports:
- "8529:8529"
volumes:
- arangodb_data:/var/lib/arangodb3
- arangodb_apps_data:/var/lib/arangodb3-apps
python:
build: .
depends_on:
- arangodb
ports:
- "5000:5000"
container_name: myapp
networks:
default:
external:
name: back
volumes:
arangodb_data:
arangodb_apps_data: