使用Loopback和Mongodb的Docker-compose无法连接mongdb [英] Docker-compose with Loopback and Mongodb fail to connect mongdb
问题描述
我最近一直在学习docker,但无法在我的回送api和mongodb之间建立连接.该应用程序在本地开发环境中运行良好.使用docker-compose,我的mongodb服务器运行在localhost:27017
上,而我的react-frontend服务在localhost:3000
上,并且看起来不错.仅环回无法连接到mongodb并引发以下错误:
I have been learning docker lately and I was unable to make a connection between my loopback api and mongodb. The application works fine in local dev environment. With docker-compose my mongodb server runs at localhost:27017
and my react-frontend serves at localhost:3000
and looks fine. Only loopback is unable to connect to mongodb and throws the following errors:
loopback_1 | Web server listening at: http://localhost:8080
loopback_1 | Browse your REST API at http://localhost:8080/explorer
loopback_1 | Connection fails: MongoError: failed to connect to server
[localhost:27017] on first connect [MongoError: connect ECONNREFUSED
127.0.0.1:27017]
loopback_1 | It will be retried for the next request.
loopback_1 |
loopback_1 | /opt/src/app/node_modules/mongodb/lib/mongo_client.js:421
loopback_1 | throw err
loopback_1 | ^
loopback_1 | MongoError: failed to connect to server [localhost:27017]
on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
loopback_1 | at Pool.<anonymous>
(/opt/src/app/node_modules/mongodb-core/lib/topologies/server.js:336:35)
这是我的 Loopback-API
FROM node:6
RUN mkdir -p /opt/src/app
WORKDIR /opt/src/app
COPY package.json /opt/src/app
RUN npm cache clean
RUN npm install
COPY . /opt/src/app
EXPOSE 8080
CMD ["npm", "start"]
我的 docker-compose 文件如下所示:
version: '3.0' # specify docker-compose version
# Define the services/containers to be run
services:
database: # name of first service
image: mongo # specify the image to build container from
ports:
- "27017:27017" # specify port forwarding
loopback: # name of second service
build: myapp_backend # specify the directory of the Dockerfile
ports:
- "8080:8080" # specify port mapping
links:
- database # link this service to the database service
react: # name of third service
build: myapp_frontend # specify the directory of the Dockerfile
ports:
- "3000:3000" #specify port mapping
最后,loopback-api中的 datasource 文件看起来像这样:
And finally datasource file in loopback-api looks like this:
{
"db": {
"host": "localhost",
"port": 27017,
"url": "",
"database": "test",
"password": "",
"name": "mongoDS",
"user": "",
"connector": "mongodb"
}
}
推荐答案
您的数据源文件应具有:
Your datasource file should have:
"host": "database"
...
"host": "database"
...
因为在docker-compose.yml中,您将mongo容器作为database
链接到回送,所以不是localhost
.请记住,您正在尝试从回送docker容器连接到mongo.另外,您还应确保mongo在环回之前启动,因此将depends_on: database
添加到docker-compose.yml中的环回服务.
since in docker-compose.yml you link mongo container to loopback as database
, so it's not localhost
. Remember, you are trying to connect to mongo from loopback docker container. Also you should make sure that mongo is started before loopback, so add depends_on: database
to loopback service in docker-compose.yml.
这篇关于使用Loopback和Mongodb的Docker-compose无法连接mongdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!