从容器连接到 Atlas mongoDB 超时错误

Timeout error connecting to Atlas mongoDB from a container

我的应用程序可以连接到我的 Atlas mongodb 当我 运行 在本地没有容器时。

当我将我的应用程序放入容器中时,我无法访问 Atlas mongodb。

我已将所有 IP 列入白名单。

        self.myclient = pymongo.MongoClient(mongodb+srv://admin:<password>@mongodbscrapperconf-lctzq.gcp.mongodb.net/test?retryWrites=true&w=majority&ssl=true&authSource=admin)

我已尝试使用我的容器连接到本地的 Atlas 数据库,并 运行在 GCP 中连接我的容器。在这两种情况下,我都从 mongoDB.

收到超时错误

我的 DockerFile 看起来像

FROM python:3.7
COPY . /app
WORKDIR /app
RUN apt-get update && apt-get install ca-certificates && rm -rf /var/cache/apk/*
COPY ./host.crt /usr/local/share/ca-certificates
RUN update-ca-certificates
RUN pip install -r requirements.txt
EXPOSE 8000
CMD python ./controller.py cloud

requirements.txt

iso4217
validators
pymongo
pymongo[srv]
flask
lxml
flask-cors
configparser

更新:我发现连接已加密并且连接需要接受 SSL 证书。我已经尝试过了,但 Atlas 仍然拒绝连接。

我通过添加

解决了这个问题
pymongo[tls,srv]

到requirements.txt

这是结果requirements.txt

iso4217
validators
pymongo[tls,srv]
flask
lxml
flask-cors
configparser