Unable to connect to mongodb using URL using monstache - mongodb

I've been trying to synchronize my mongodb with elastic for two days and I'm going crazy.
After many attempts and changes in my dockerfile and in my docker-compose I get this error, but the container with mongodb is up and running.
monstache | ERROR 2018/07/27 17:59:07 Unable to connect to mongodb using URL 'mongodb:27018': no reachable servers
monstache | panic: Unable to connect to mongodb using URL 'mongodb:27018': no reachable servers
monstache |
monstache | goroutine 1 [running]:
monstache | log.(*Logger).Panicf(0xc420020c30, 0xd4fc15, 0x2d, 0xc42006fc18, 0x2, 0x2)
monstache | /usr/local/go/src/log/log.go:219 +0xdb
monstache | main.main()
monstache | /home/vagrant/go/src/github.com/rwynn/monstache/monstache.go:2400 +0x320
monstache exited with code 2
This is my dockerfile
FROM golang
ADD build-4.4.0/linux-amd64/monstache /go/bin/monstache
ENTRYPOINT ["monstache", "-mongo-url='mongodb:27018'", "-elasticsearch-url=elasticsearch:9200"]
And this is my docker-compose file
version: '3.3'
services:
mongodb:
image: mongo
restart: always
container_name: mongodb
volumes:
- ./data/mongodb:/usr/share/mongodb/data
ports:
- 27018:27017
elasticsearch:
image: elasticsearch
restart: always
container_name: elasticsearch
volumes:
- ./data/elastic:/usr/share/elasticsearch/data
ports:
- 9200:9200
monstache:
build: ./monstache/
restart: always
container_name: monstache
links:
- elasticsearch
- mongodb
Any idea?
Thanks!

Finally, we desist to use monstache, and we use mongo-connector in a python dockerfile to share the data from mongodb to elasticsearch.
FROM python:3.4.3
RUN pip install 'mongo-connector[elastic5]' && \
pip install 'elastic2-doc-manager[elastic5]'
I hope this can help to someone.

You have to change 'mongo-url' to -mongo-url='mongodb:27017', because all your containers are running in docker network and mongodb is available there on port 27017 since this is exposed port.
And the below block in compose file will also make mongodb available on your host network at port 27018 but that is accessible via your browser in your local machine env, not in docker network.
ports:
- 27018:27017
This compose file should also work without links.
version: '3.3'
services:
mongodb:
image: mongo
restart: always
container_name: mongodb
volumes:
- ./data/mongodb:/usr/share/mongodb/data
ports:
- 27018:27017
elasticsearch:
image: elasticsearch
restart: always
container_name: elasticsearch
volumes:
- ./data/elastic:/usr/share/elasticsearch/data
ports:
- 9200:9200
monstache:
build: ./monstache/
restart: always
container_name: monstache

Related

mongo-express | Could not connect to database using connectionString: mongodb://127.0.0.1:27017/"

When trying to spin up a docker container with docker-compose,
I get the following error message:
(node:8) [MONGODB DRIVER] Warning: Current Server Discovery
and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
mongo-express | Could not connect to database using connectionString: mongodb://127.0.0.1:27017/"
I noticed that mongo-express always adds a double quote to the end of my connection string, is this intentional or did I mess something up in my docker-compose file?
The file:
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: unless-stopped
networks:
- backbone
expose:
- 27017
ports:
- 27017:27017
env_file:
- ./.env
command: [--auth]
environment:
- MONGO_INITDB_ROOT_USERNAME=${DB_USERNAME}
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- ./data:/data/db
mongo-express:
image: mongo-express
container_name: mongo-express
restart: unless-stopped
networks:
- backbone
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_SERVER=127.0.0.1
- ME_CONFIG_MONGODB_URL="mongodb://127.0.0.1:27017"
- ME_CONFIG_BASICAUTH_USERNAME=${DB_USERNAME}
- ME_CONFIG_BASICAUTH_PASSWORD=${DB_PASSWORD}
networks:
backbone:
driver: bridge
I already tried updating my version of docker/docker-compose and mongodb.
How do I resolve the error?
Or is there a workaround I could use?
In a container, localhost refers to the container itself. So when Mongo Express tries to connect to the database at localhost, it looks for the database inside the Mongo Express container.
Docker compose let's you refer to containers using their container names, so you should change your Mongo Express environment variables to
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_URL="mongodb://mongodb:27017"

How to connect remotely to Mongodb running on Docker-compose?

How to change this script so that I can connect remotely to my Mongodb running in docker-compose, from different machines (that are not connected to the same network/internet provider).
I want to allow all remote connections.
I don't care about security matters as it's just for practice purposes!
docker-compose.yaml script file:
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=admin
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
data: {}
networks:
default:
name: mongodb_network
I solved my issue by migrating my data to Atlas cloud.mongodb.com
Answer update: To provide more info as suggested by #nuhkoca, This is the video tutorial to create a mongodb atlas: https://www.youtube.com/watch?v=xrc7dIO_tXk&t=15s And this is the link to the db from my resources file in my Springboot backend api:
server.port=<Port_number>
spring.data.mongodb.uri=<Link_to_mongodb_atlas>

connect EHOSTUNREACH 172.24.0.2:27017 docker-compose

I am currently unable to connect to mongo when I run docker-compose, I keep on getting the error below
connect EHOSTUNREACH 172.25.0.2:27017
Here is my docker-compose.yml file
version: "3.4"
services:
app:
container_name: checki
restart: always
build:
context: .
network: host
ports:
- "3000:9000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27018:27017"
and my db connection string is like this
"mongodb://mongo:27017/checki"
Please assist

Docker: Unable to connect one container with another on same network

I've a following docker-compose.yaml file:
version: "3"
services:
# Database
database:
image: mongo
container_name: database
restart: always
volumes: ["dbdata:/data/db"]
environment:
- MONGO_INITDB_ROOT_USERNAME=myusername
- MONGO_INITDB_ROOT_PASSWORD=mypassword
ports:
- 7000:27017
command: mongod
networks:
- webapp
# Server
server:
container_name: server
restart: always
build: ./server
volumes: ["./server:/var/www", "/var/www/node_modules"]
ports:
- "9000:3000"
depends_on:
- database
networks:
- webapp
networks:
webapp:
driver: bridge
volumes:
dbdata:
Now, I'm able to connect with my mongo database from my local windows machine through mongodb client but my server container is not able connect with database container.
I'm using following connection URI on my server to establish a connection:
mongodb://myusername:mypassword#database:7000/mydatabase
It must be noted that on my local machine I'm using the same URI to connect successfully with the database, the only difference is that I'm using localhost instead of database (container name) on my local machine.
Can you please tell me what I'm doing wrong here?

No connection between mongo and laravel using docker

I'm trying to dockerize laravel project with mongodb but i'm getting
No suitable servers found (serverselectiontryonce set): [Failed connecting to '127.0.0.1:27107': Connection refused calling inmaster on 27017]
here's my docker-compose file
version: '3.6'
services:
mongodb:
image: mongo:latest
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
web:
build: .
volumes:
- ./tmp/db:/var/lib/mongodb/data
ports:
- 8000:8000
links:
- mongodb
depends_on:
- mongodb
Change the mongodb connection string from '127.0.0.1:27107' to 'mongodb:27107' because mongo is running in separate container. Mongodb instance can be reached with linked service name.