I'm practicing Docker with a dummy app but I can't connect with the database. 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 in server
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
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
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.
sudo systemctl stop mongod
But now I don't understand why It gives an error when I do 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
. To communicate between containers
in a docker.network
all you have to do is use the service
name. Instead of mongodb://mongo:27017/prueba-docker
try connecting to mongodb://db:27017/prueba-docker
.
You also mentioned a port conflict. It seems like you are already running 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:
ports:
- "27018:27017"
Or you could just don't expose your mongodb
at all if no external application
is using your db
. This only introduces security threats
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.