我正在为我自己测试一些东西,我遇到了问题:试图将一个节点表达式容器(App)连接到一个mongo容器(数据库),如果我连接到MongoCompose,但不能连接到nodeexpress的容器中,我就可以连接到mongo容器(数据库),如果我连接到localhost:27017
,就不能用'mongodb://localhost:27017/dbtest'
配置url连接节点表达式的容器来连接像这样的数据库。
因此,我查找了一些解决方案(比如this),并回答说,我看到的不是'mongodb://localhost:27017/dbtest'
,而是需要写我的容器'mongodb://mymongo:27017/dbtest'
的名称,但是对我来说,这不起作用,只是收到了ECONNREFUSED错误。
容器在同一个网络中,这是我的dockerfile和docker-compose文件。
Dockerfile
#node 8.16.2
FROM node:8.16.2
COPY . /app
WORKDIR /app
RUN npm install
EXPOSE 3000
CMD ["npm","start"]
docker-compose.yaml
version: "3.7"
services:
db:
image: mongo
ports:
- 27017:27017
networks:
- testing
app:
build:
context: .
dockerfile: Dockerfile
networks:
- testing
networks:
testing:
我解决了这样的问题,比如mongodb://172.17.0.1:27017/dbtest
,其中172.17.0.1
是作为容器的网络的网关。
有人能解释一下这种行为吗?如果是正确的话?平台Linux
发布于 2020-01-30 10:17:11
mymongo
这个名字是从哪里来的?您已经在撰写文件中将mongodb服务的名称定义为db
。所以使用连接字符串'mongodb://db:27017/dbtest'
version: "3.7"
services:
db: ---------------> This is the name of your mongo service
image: mongo
ports:
- 27017:27017
networks:
- testing
app:
build:
context: .
dockerfile: Dockerfile
networks:
- testing
networks:
testing:
https://stackoverflow.com/questions/59991144
复制相似问题