在谷歌运行的docker容器中运行flask应用程序,不会连接到mongodb数据库



我已经使用docker在google run上启动了我的烧瓶服务器。这是我的案卷。

FROM python:3.9.7-slim
# copy the requirements file into the image
COPY ./requirements.txt /app/

# switch working directory
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends 
bzip2 
g++ 
git 
graphviz 
libgl1-mesa-glx 
libhdf5-dev 
openmpi-bin 
wget 
python3-tk && 
rm -rf /var/lib/apt/lists/*

# install the dependencies and packages in the requirements file
RUN pip install -r requirements.txt

# copy every content from the local file to the image
COPY . /app
# configure the container to run in an executed manner
# ENTRYPOINT [ "python" ]
# CMD ["app.py" ]
# Minimize image size 
RUN (apt-get autoremove -y; 
apt-get autoclean -y)
CMD ["gunicorn"  , "-b", "0.0.0.0:8888", "app:app"]

它在本地运行良好,在谷歌上运行时,当我访问索引页时,它可以工作,但每当它试图连接到我的mongodb atlas数据库时,它都会返回错误连接超时我在我的mongo端添加了0.0.0.0 ip异常,允许从任何ip访问。所以我有一种感觉,问题出在谷歌方面,它以某种方式阻止了数据库的出站流量。

如何允许我的服务器连接到我的mongo数据库?我是否缺少某种类型的配置?我查看了VPC,但不太明白发生了什么。

以下是我的网址:mongodb+srv://:@。。mongodb.net/?retryWrites=true&w=多数";

我得到的错误:pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[Erno 111]连接被拒绝,超时:30s,拓扑描述:<TopologyDescription id:635ae8cfe34d6bf95c474b3f,topology_type:未知,服务器:[<ServerDescription('localhost',27017(server_type:未知,rtt:无,error=AutoReconnect('localhost:27017:[Erno 111]连接被拒绝'(>]>

感谢的帮助

缺少环境变量,docker是否会自动忽略您的env文件?我添加了mongo url作为环境变量

最新更新