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

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"

Related

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>

Dockerized flask server is not respnsive from within the container, but is responsive when is run on host

I'm trying to setup a Flask <-> MongoDB <-> mongo_express application. I have 3 containers defined in my docker-compose.yml, and I start them successfully. However, while the Mongo part is OK (I can access the DB via the express api at localhost:8081), Flask can't access the DB.
What I'm looking for:
I want to be able to send requests from host machine (or any other
in the network) to Flask (running on 0.0.0.0:5000), from Flask to DB
(running on localhost:27017, accessed using pymongo wrapper).
Also, I want to be able to have access to mongo_express (on
localhost:8081) and from it to the DB [This part is already
working!]
In order to debug it, I removed the Flask container from the docker-compose.yml, restarted, and run it locally, and voila! everything works (meaning my pytest runs are ok - I have a test where I send a request to the Flask server, which in turn, uses the pymongo wrapper to access the DB, and return data from it to the client).
I guess my network configuration is flawed, but I don't understand where.
Here is my docker-compose.yml (Flask is commented out, since it is currently running locally):
version: "3.8"
services:
# mgmt_server:
# build: ./server # Dockerfile just copies py files, installs pip requirements, and runs "python server.py"
# container_name: mgmt_server
# restart: always
# networks:
# - backend # Connect to flask
# ports:
# - "5000:5000"
mongo:
image: mongo:latest
container_name: mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=pass
volumes:
- /data/db:/data/db
networks:
- backend # Connect to flask
- frontend # Connect to mongo_express
ports:
- "27017:27017"
mongo-express:
image: mongo-express:latest
container_name: mongo_express
restart: always
environment:
- ME_CONFIG_MONGODB_SERVER=mongo
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
# Uncomment if a secure login via browser is required
# - ME_CONFIG_BASICAUTH_USERNAME=root
# - ME_CONFIG_BASICAUTH_PASSWORD=pass
links:
- mongo
networks:
- frontend # Connect to mongo_express
ports:
- 8081:8081
networks:
backend:
driver: bridge
frontend:
driver: bridge
$ docker network ls # When mgmt_server is run locally
NETWORK ID NAME DRIVER SCOPE
10577560a149 bridge bridge local
a037a11e12bb host host local
b153eea9db12 node_mgmt_backend bridge local
c8e1b58ffb44 node_mgmt_frontend bridge local
4f7b75b5695a none null local
The problem was that Flask tried to access Mongo on localhost. When running Flask on the host, this is OK, but when Flask is containerized, it gets its own ip, and so does Mongo, and localhost just doesn't point to the right place.
Editing the docker-compose.yml networks, and redirecting pymongo client to the updated Mongo IP fixed the issue:
version: "3.8"
services:
mgmt_server:
build: ./server
container_name: mgmt_server
restart: always
ports:
- "5000:5000"
networks:
app_net:
ipv4_address: 172.16.238.2
mongo:
image: mongo:latest
container_name: mongodb
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=pass
volumes:
- /data/db:/data/db
ports:
- "27017:27017"
networks:
app_net:
ipv4_address: 172.16.238.3
mongo-express:
image: mongo-express:latest
container_name: mongo_express
restart: always
environment:
- ME_CONFIG_MONGODB_SERVER=mongo
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
# Uncomment if a secure login via browser is required
# - ME_CONFIG_BASICAUTH_USERNAME=root
# - ME_CONFIG_BASICAUTH_PASSWORD=pass
links:
- mongo
ports:
- 8081:8081
networks:
app_net:
ipv4_address: 172.16.238.4
networks:
app_net:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
This was helpful, but eventually I found this to be more informative.

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?

with docker-compose my app container cannot see the mongodb container

Here is my docker compose file:
version: "3.3"
services:
test:
image: test
networks:
- mongo_net
ports:
- 4000:80
depends_on:
- mongodb
links:
- mongodb
mongodb:
image: mongo:latest
networks:
- mongo_net
ports:
- 27017:27017
volumes:
- local_data:/data/db
volumes:
local_data:
networks:
mongo_net:
driver: bridge
The 'test' image cannot find the 'mongodb' instance.
My assumption is that the 'links' section would connect the two, but it is not happening.
What am I missing?
for your compose file try just using depends_on. links is deprecated and maybe thats why you are currently getting issues since this is a V3 compose file.

How to run MongoDB and Mongo-express with docker-compose?

I try to run MongoDB and Mongo-express by Docker-compose. I use following config:
version: '3'
services:
mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
- MONGO_INITDB_DATABASE=project
mongo-express:
image: mongo-express
environment:
- ME_CONFIG_MONGODB_SERVER=mongo
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=false
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
- ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
- ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
- ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
depends_on:
- mongo
ports:
- "8080:8081"
and .env file:
MONGO_ROOT_USER=devroot
MONGO_ROOT_PASSWORD=devroot
MONGOEXPRESS_LOGIN=dev
MONGOEXPRESS_PASSWORD=dev
After docker-compose up mongo-service is running, but mongo-express fails. I see error in logs:
mongo-express_1 | Welcome to mongo-express
mongo-express_1 | ------------------------
mongo-express_1 | Mongo Express server listening at http://0.0.0.0:8081
mongo-express_1 | Server is open to allow connections from anyone (0.0.0.0)
mongo-express_1 | Database connected
mongo-express_1 | Connecting to admin...
mongo-express_1 | TypeError: db is not a function
mongo-express_1 | at /node_modules/mongo-express/lib/db.js:114:53
mongo-express_1 | at Array.forEach (native)
What I do wrong? Thanks for advance!
it works in my mac. If you delete docker-compose containers and rebuild, maybe it will work. I added my files. Only i changed mongo-express port.
.env
MONGO_ROOT_USER=devroot
MONGO_ROOT_PASSWORD=devroot
MONGOEXPRESS_LOGIN=dev
MONGOEXPRESS_PASSWORD=dev
docker-compose.yml
version: '3'
services:
mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
- MONGO_INITDB_DATABASE=project
mongo-express:
image: mongo-express
environment:
- ME_CONFIG_MONGODB_SERVER=mongo
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=false
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
- ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
- ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
- ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
depends_on:
- mongo
ports:
- "8888:8081"
Docker Version
Client:
Version: 18.06.0-ce
API version: 1.38
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:05:26 2018
OS/Arch: darwin/amd64
Experimental: false
Server:
Engine:
Version: 18.06.0-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:13:46 2018
OS/Arch: linux/amd64
Experimental: true
This problem comes when your database uses a volumes configuration, but mongodb and mongo-express doesn't share a network configuration.
This worked perfectly for me:
version: "3.7"
services:
db:
container_name: mongo-dev
image: mongo:4.2
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_DATABASE=auth
- MONGO_INITDB_ROOT_PASSWORD=pass
networks:
- mongo-compose-network
ports:
- '27017:27017'
volumes:
- ./data:/data/db
mongo-express:
container_name: mongo-express
image: mongo-express
depends_on:
- db
networks:
- mongo-compose-network
environment:
- ME_CONFIG_MONGODB_SERVER=mongo-dev
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=pass
- ME_CONFIG_BASICAUTH_USERNAME=admin
- ME_CONFIG_BASICAUTH_PASSWORD=tribes
ports:
- '8081:8081'
volumes:
- ./data:/data/db
networks:
mongo-compose-network:
driver: bridge
Running it like this just worked for me maybe it helps you
mongoex:
image: mongo-express
environment:
- ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
- ME_CONFIG_MONGODB_SERVER=database
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=false
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
ports:
- "8081:8081"
links:
- database
database:
image: mongo:latest
command: --smallfiles
ports:
- "27017:27017"
good luck and regards!
Mongo-express changed the user and pass ENV keywords:
OLD:
- ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
NEW:
- ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASSWORD}
reference:
https://hub.docker.com/_/mongo-express
Some updates in new verion of mongo-express, the accepted solution doesn't work any more, especially you need fix this issue
TypeError: Cannot read property 'listDatabases' of undefined
Tips
Do remember, if you adjust the setting, restart it with option --force-recreate.
docker-compose up --force-recreate -d
Here is the docker-compose.yml I am using currently.
version: '3'
services:
mongo:
image: mongo:${MONGO_TAG}
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
- MONGO_INITDB_DATABASE=project
mongo-express:
image: mongo-express
environment:
- ME_CONFIG_MONGODB_SERVER=mongo
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
- ME_CONFIG_MONGODB_AUTH_DATABASE=admin
- ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USER}
- ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASSWORD}
- ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
- ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
links:
- mongo
ports:
- "8081:8081"
Although this post is bit old. I would like to share the docker-compose.yml that worked for me.
The below spins up a mongoDB instance and mongo-express.
version: '3.7'
services:
mongo_db:
image: mongo:4.2.12
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: rootpassword
ports:
- 27017:27017
volumes:
- /C/Mine/mongoData:/data/db
mongo_express:
image: mongo-express:0.54.0
environment:
- ME_CONFIG_OPTIONS_EDITORTHEME=default
- ME_CONFIG_MONGODB_SERVER=mongo_db
- ME_CONFIG_MONGODB_PORT=27017
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
- ME_CONFIG_MONGODB_AUTH_DATABASE=mydb
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpassword
ports:
- "8081:8081"
restart: on-failure
depends_on:
- mongo_db
The simplest way is run docker-compose up -d in directory with docker-compose.yml:
version: "3"
services:
mongo:
image: "mongo:3-stretch"
mongo-express:
image: "mongo-express:latest"
ports:
- "8081:8081"
And then open http://localhost:8081/ in your browser.
In case you are still having this issue... Or in case someone else is going absolutely crazy trying to figure out why none of this works...
Running docker-compose up --force-recreate
Doesn't recreate the container completely (not sure why) - it DOES look like it did, but I figured out that it wasn't recreating the container's data (databases!) when I got into the DB without Auth, and a collection that I created an hour ago was still there.
I had run the above command, and also 'docker image rm mongo' as well as removing the mongo-express (Not needed)
The problem is I had to do a 'docker container list' to see the two containers I had (one for Mongo by itself and one for Mongo with Mongo-express - not sure which I needed to delete, but I deleted them both...)
Once I did that, and did the 'up' your configuration pulling the values from the .env file worked fine - it did the INITDB... and set the Admin passwords correctly in the database!
So, if you EVER created it with any configuration to test it, you will have a container that it is reusing the data for (I think it doesn't recreate that to avoid data loss - I mean, 99.9% of the time you just want to recreate the PROGRAM part, not the data - otherwise you would lose your databases.)
Killed a couple hours figuring this one out...
docker-compose.yml
version: '3'
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
mongo-express:
image: mongo-express
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
depends_on:
- mongodb
Save and get into the folder and run docker-compose up. Now mongo will run at port http://localhost:8081/.
This is very useful things but same time unstable. You need find out your own way How to write docker-compose.yml For me It's was works something like that
version: '3'
services:
db:
image: mongo:2.6.12
container_name: app_db
volumes:
- store:/data/db
ports:
- "27017:27017"
mongo-express:
container_name: mongo-express
links:
- 'db:mongo'
ports:
- '8081:8081'
environment:
- 'ME_CONFIG_OPTIONS_EDITORTHEME=ambiance'
- 'ME_CONFIG_BASICAUTH_USERNAME=user'
- 'ME_CONFIG_BASICAUTH_PASSWORD=pass'
image: mongo-express
I also faced similar issue
mongo-express | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo-dev:27017] on first connect [Error: connect ECONNREFUSED 172.20.0.2:27017
Added the below line to recover from this, because docker sometimes takes time to expose the mongo port 27017 and mongo-express fails to connect and stops, with restart: always mongo-express container reconnect's to mongo port 27017
each time when it crashes
mongo-express:
container_name: mongo-express
image: mongo-express
restart: always