[英]Can't connect to mongo in Docker
I'm practicing Docker with a dummy app but I can't connect with the database.我正在使用虚拟应用程序练习 Docker,但我无法连接到数据库。 If anyone can give a clue about what the problem could be, please.
如果有人可以提供有关问题可能是什么的线索,请。 Thanks in advance for any help.
在此先感谢您的帮助。 If any more info is needed please let me know.
如果需要更多信息,请告诉我。
Here is my Dockerfile for api:这是我的 Dockerfile 对于 api:
Dockerfile in server服务器中的 Dockerfile
FROM node:14-alpine
WORKDIR usr/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3400
CMD ["npm", "start"]
Here is my connection to db in db.js这是我在 db.js 中与 db 的连接
const mongoose = require('mongoose');
const uri = 'mongodb://mongo:27017/prueba-docker';
const connect = async () => {
try {
await mongoose.connect(uri, {});
console.log('conected to db');
} catch (err) {
console.log('error connecting to db', err);
}
};
module.exports = { connect };
And here docker-compose在这里 docker-compose
version: "3"
services:
prueba-react:
image: prueba-react
build: ./client/
stdin_open: true
ports:
- "3000:3000"
networks:
- mern-app
prueba-api:
image: prueba-api
build: ./server/
ports:
- "3400:3400"
networks:
- mern-app
depends_on:
- db
db:
image: mongo:4.4-bionic
ports:
- "27017:27017"
networks:
- mern-app
volumes:
- mongo-data:/data/db
networks:
mern-app:
driver: bridge
volumes:
mongo-data:
driver: local
First I found out that I should stop mongo wiht the command because I had a port conflict.首先,我发现我应该使用命令停止 mongo,因为我遇到了端口冲突。
sudo systemctl stop mongod
But now I don't understand why It gives an error when I do docker-compose up但是现在我不明白为什么当我执行 docker-compose up 时会报错
This is the error I get这是我得到的错误
prueba-api_1 | error connecting to db MongooseServerSelectionError: getaddrinfo EAI_AGAIN mongo
prueba-api_1 | at NativeConnection.Connection.openUri (/usr/app/node_modules/mongoose/lib/connection.js:796:32)
prueba-api_1 | at /usr/app/node_modules/mongoose/lib/index.js:328:10
prueba-api_1 | at /usr/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
prueba-api_1 | at new Promise (<anonymous>)
prueba-api_1 | at promiseOrCallback (/usr/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
prueba-api_1 | at Mongoose._promiseOrCallback (/usr/app/node_modules/mongoose/lib/index.js:1149:10)
prueba-api_1 | at Mongoose.connect (/usr/app/node_modules/mongoose/lib/index.js:327:20)
prueba-api_1 | at Object.connect (/usr/app/db.js:6:20)
prueba-api_1 | at Object.<anonymous> (/usr/app/app.js:6:4)
prueba-api_1 | at Module._compile (internal/modules/cjs/loader.js:1072:14) {
prueba-api_1 | reason: TopologyDescription {
prueba-api_1 | type: 'Unknown',
prueba-api_1 | servers: Map(1) { 'mongo:27017' => [ServerDescription] },
prueba-api_1 | stale: false,
prueba-api_1 | compatible: true,
prueba-api_1 | heartbeatFrequencyMS: 10000,
prueba-api_1 | localThresholdMS: 15,
prueba-api_1 | logicalSessionTimeoutMinutes: undefined
prueba-api_1 | }
In your docker-compose.yml
you are creating a network
called mern-app
where all services
are assigned to that network
.在您的
docker-compose.yml
中,您正在创建一个名为mern-app
的network
,其中所有services
都分配给该network
。 To communicate between containers
in a docker.network
all you have to do is use the service
name.要在
docker.network
中的containers
之间进行通信,您所要做的就是使用service
名称。 Instead of mongodb://mongo:27017/prueba-docker
try connecting to mongodb://db:27017/prueba-docker
.而不是
mongodb://mongo:27017/prueba-docker
尝试连接到mongodb://db:27017/prueba-docker
。
You also mentioned a port conflict.您还提到了端口冲突。 It seems like you are already running
mongodb
.您似乎已经在运行
mongodb
。 If you don't want to stop your mongodb
everytime you want to try out your docker-compose
, you could just map to another port:如果您不想每次尝试
docker-compose
mongodb
您可以将 map 连接到另一个端口:
ports:
- "27018:27017"
Or you could just don't expose your mongodb
at all if no external application
is using your db
.或者,如果没有外部
application
正在使用您的db
,您可以根本不公开您的mongodb
。 This only introduces security threats
.这只会引入
security threats
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.