Hi I'm trying to develop simple api but I couldn't establish connection between api and mongodb. When I send a query to database it hangs and couldn't get response. I am trying to connect to mongo container with this code:
mongoose.connect("mongodb://mongo/micro-linkedin_api_1");
Here is my docker-compose file:
version: "3"
services:
api:
restart: always
build: ./api
ports:
- "3000:3000"
links:
- mongodb
env_file:
- ./api/.env
volumes:
- ./api:/usr/xd/job-bot/api
mongodb:
image: mongo
ports:
- "27017:27017"
scraper:
build: ./scraper
and my folder structure is like this one :
As I can see in docker-compose you defined 2 services:
api and mongodb
in Mongoose docs, this is how you connect:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
You did:
mongoose.connect("mongodb://mongo/micro-linkedin_api_1");
so the host is not correct, you used mongo instead of mongodb as defined in docker-compose services.
Related
So, I have created an app using Express.js + Typescript + mongoose and this docker-compose for development:
version: "3.8"
services:
web:
container_name: selfapi
restart: unless-stopped
depends_on:
- db
build:
context: .
dockerfile: Dockerfile
target: development
ports:
- ${PORT}:${PORT}
environment:
MONGODB_URL: ${MONGODB_URL}
NODE_ENV: development
AUTH_TOKEN: ${AUTH_TOKEN}
volumes:
- ./src/:/home/node/app/src
- ./tests/:/home/node/app/tests
- web_nodemodules:/home/node/app/node_modules
db:
container_name: mongodb
image: mongo:5.0.4
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGODB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_ROOT_PASSWORD}
volumes:
- mongodb_data:/data/db/
- mongodb_configdb:/data/configdb/
mongo-express:
container_name: mongo-express
image: mongo-express:0.54.0
restart: always
depends_on:
- db
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGODB_ROOT_USERNAME}
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGODB_ROOT_PASSWORD}
ME_CONFIG_MONGODB_SERVER: db
ME_CONFIG_BASICAUTH_USERNAME: ${MONGO_EXPRESS_USERNAME}
ME_CONFIG_BASICAUTH_PASSWORD: ${MONGO_EXPRESS_PASSWORD}
volumes:
web_nodemodules:
mongodb_data:
mongodb_configdb:
Following that and assuming that both username and password for mongodb are root, we can get to the database url mongodb://root:root#db:27017 for any service within the compose, and mongodb://root:root#localhost:27017 for the host machine.
In my app, I connect to database this way:
import mongoose from 'mongoose';
export const connectMongoDb = async () => {
try {
return await mongoose.connect(process.env.MONGODB_URL as string);
} catch {
return process.exit(1);
}
};
But it only works if I don't add any database name to the url, so the url can't be something like mongodb://root:root#db:27017/myDatabase.
When I put only the raw mongodb url, and use the command ts-node-dev --poll --respawn --transpile-only --inspect -- ./src/index.ts, everything works fine as you will see below:
API:
But when I add something to my mongodb using the express app, it goes to a database called test, as you can see here in the mongo-express:
Now, let's say I don't want that to happen, intead, I want that the collection projects goes to a database calles MyApp, so I'd put it in the db url, becoming: mongodb://root:root#db:27017/MyApp. Following mongodb docs, if the database doesn't exist, it creates on first insertion, but this is what happens:
.env
API:
Apparently it is working, right? But no, take a look how it is on browser when I access the API:
"This page is not working"
"No data was sent by localhost"
That is happening just because I added the name I wanted that my database to have.
How can I fix that, changing the name, because it's REALLY annoying that all my data goes to a database calles TEST...
PS.: When my app is in a production environment, the url WITH the database name works, but the url is from mongodb Atlas, so it is something like this: mongodb+srv://test:test#db1.mongodb.net/GithubProjects?retryWrites=true&w=majority, as you can see, the database name is GithubProjects, and it works.
i have a node web service and I'm a bit new to docker, so please bear with me. Below is my docker-compose file. I have run this image locally and it works fine, but when trying to deploy it to Heroku I get the following error:
{ MongoError: failed to connect to server [mongo:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongo mongo:27017]
I've found where I'm supposed to set the environment flags in Heroku, so I managed to check that and its fine. But it still can't find mongo:27017
What could I be doing wrong?
version: "3"
services:
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/charecters
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
mongo-seed:
build: ./mongo-seed
links:
- mongo
volumes:
- ./mongo-seed:/mongo-seed
command:
- /mongo-seed/script.sh
app:
container_name: app
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo
environment:
- DB_SERVERS=mongo:27017
links:
- mongo
is depreciated try
depends_on:
- mongo
also, check that
mongo:
container_name: mongo, is properly referenced in DB setup
change command:
- /mongo-seed/script.sh
try command:
- 'node your-app-file'
I have two docker container one with my nodejs webapp and one with a mongo db running.
I build a authentication in my mongo db container and now i get this error message:
mongo | 2018-08-22T13:48:24.102+0000 I ACCESS [conn1] SASL SCRAM-SHA-1 authentication failed for blub on admin from client XXXXX; UserNotFound: Could not find user blub#admin
mongo | 2018-08-22T13:48:24.105+0000 I NETWORK [conn1] end connection XXXXX (0 connections now open)
app | (node:16) UnhandledPromiseRejectionWarning: MongoError: Authentication failed.
I build both container with this docker-compose.yml
version: "2"
services:
app:
container_name: app
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
environment:
- MONGODB_INITDB_ROOT_USERNAME=blub
- MONGODB_INITDB_ROOT_PASSWORD=pass
My app connects with the mongodb like this:
mongoose.connect("mongodb://blub:pass#mongo:27017/");
I found a solution:
My docker-compose:
version: '2'
services:
app:
container_name: app
restart: always
build: .
ports:
- 3000:3000
links:
- mongodb
mongodb:
container_name: mongodb
image: mongo:3.4
ports:
- 27017:27017
volumes:
- ./data:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin123456
command: mongod
the connection in the nodejs app: mongoose.connect("mongodb://admin:admin123456#mongodb:27017/");
But i actually canĀ“t figure out, why it isn't working on my first try.
I have two docker-compose.yml files. In the first one, I run a mongodb instance:
version: '3'
services:
mongodb:
image: mongo:latest
container_name: "mongodb"
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
In the second one, I run my web app and I want to link it with mongodb container:
version: '3'
services:
webapp:
build:
context: .
external_links:
- mongodb
ports:
- 8080:8080
But, when I run my webapp, I get a connection error. I'm connecting to this URI:
const DB_URI = 'mongodb://mongodb:27017/mydb';
but I get MongoNetworkError. What am I missing?
I would recommend you to use those two services in the same docker-compose file and replace external_links with links
version: '3'
services:
mongodb:
image: mongo:latest
container_name: "mongodb"
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
webapp:
build:
context: .
links:
- mongodb
depends_on:
- mongodb
ports:
- 8080:8080
How could I ensure mongodb is up and running before webapp is started?
AFAIK, the links will take care of the the situation.
or
You could use depends_on
Solved. Hosts were not accessible because they were in different networks. Creating a network (https://docs.docker.com/compose/networking/) and running both services in that network solved the problem.
I am trying to get a dockerised keystoneJS to talk to a dockerised mongoDB instance and I am struggling to see where I am going wrong in terms of linking them together. I have gone through the docker docs and similar examples online that are trying to do what I am trying to do, but still I am unable to get the two to talk to each other.
The main issues are it being unable to find 'localhost:27017' or Error: Invalid mongodb uri. Must begin with "mongodb://" Received: "mongodb://mongo:27017/"
Below are the relevant files:
Dockerfile for keystone
FROM node:6.9.1
RUN mkdir -p /docker
WORKDIR /docker
COPY . /docker
RUN npm install --no-optional
CMD ["node", "keystone.js"]
docker-compose.yml
version: '2'
services:
keystone:
image: keystone-test
ports:
- "3000:3000"
depends_on:
- mongo
networks:
- localnetwork
environment:
- MONGO_URI="mongodb://mongo:27017/"
mongo:
image: mongo:3
command: mongod --smallfiles
ports:
- "27017:27017"
networks:
- localnetwork
networks:
localnetwork:
keystone.js
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGO_URI);
..... and some usual keystone stuff
It takes me a lot of time to figure out. In your docker-compose.yml, the MONGO_URI shouldn't wrap in double quotes, it should be mongodb://mongo:27017/ instead of "mongodb://mongo:27017". MONGO_URI=mongodb://mongo:27017/ instead of MONGO_URI="mongodb://mongo:27017/"