无法从其他Docker容器连接到MongoDB容器

编程入门 行业动态 更新时间:2024-10-28 19:19:36
本文介绍了无法从其他Docker容器连接到MongoDB容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个通过Docker compose链接在一起的容器,我可以使用主机名从包含node.js应用程序的容器成功ping包含MongoDB的容器.但是,当涉及到实际连接到数据库时,node.js会告诉我该连接被拒绝.如果有帮助,我正在使用Docker for Mac.

I have two containers linked together by Docker compose and I can successfully ping the container containing MongoDB from the one containing my node.js app using the hostname. However, when it comes to actually connecting to the database, node.js tells me the connection is refused. I am using Docker for Mac if that helps.

mongoose.connect("mongodb://mongo", {server: {auto_reconnect: true}}).catch(error => { console.log("DB CONNECTION ERROR"); console.log(error) });

这是我的docker-compose.yml文件:

Here is my docker-compose.yml file:

version: "3" services: mongo: image: mongo:latest volumes: - ./db:/data/db restart: always expose: - 27017 ports: - 27017:27017 container_name: mongo goose: depends_on: - mongo build: . volumes: - ./app/server/templates:/usr/src/app/app/server/templates expose: - 3005 restart: always ports: - 3005:3005 container_name: goose-compose

推荐答案

我有一个类似构建的应用程序,它在两个单独的容器中使用flask和mongodb.我注意到您的撰写和我的撰写之间的区别是,每个容器都有提到的卷,但是没有定义卷,就像这样:

I have a similarly built app using flask and mongodb in two separate containers. One difference that I notice between your compose and mine, is that you have volumes mentioned for each container, but no volume definition, like so:

version: "3" services: web: image: jzakilla/bookfinderpy command: nginx -g "daemon off;" container_name: webapp ports: - 80:80 - 443:443 # expose internal container ports to host db: image: mongo container_name: mongodb ports: - 27017:27017 volumes: - book_db:/data/db volumes: book_db: driver: local

没有卷部分,没有驱动程序告诉容器如何访问数据,我想容器会旋转,但实际上无法启动mongo服务,从而给您带来拒绝的错误.

Without a volume section, and a driver to tell the container how to access the data I would imagine the container would spin up, but not actually be able to start the mongo service, thus giving you a denied error.

更多推荐

无法从其他Docker容器连接到MongoDB容器

本文发布于:2023-10-14 12:29:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   连接到   Docker   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!