Mongo-Express: failed to connect to server [mongo:27017] on first connect - mongodb

I'm trying to run the simplest container of mongo and mongo-express using docker-compose. I have faced with many errors that will be explained later on.
I have tried the following docker-compose configurations:
1.
version: '2'
services:
mongo:
image: mongo:latest
mongo-express:
image: mongo-express:latest
ports:
- 8082:8081
2.
version: '2'
services:
mongo:
image: mongo:latest
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
ports:
- 27017:27017
volumes:
- db-data:/data/db
- mongo-config:/data/configdb
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
volumes:
db-data:
mongo-config:
and etc. But all of them have the following common error whenever I execute docker-compose -f docker-compose.yml up:
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 | basicAuth credentials are "admin:pass", it is recommended you change this in your config.js!
mongo-express_1 |
mongo-express_1 | /node_modules/mongodb/lib/server.js:265
mongo-express_1 | process.nextTick(function() { throw err; })
mongo-express_1 | ^
mongo-express_1 | MongoError: failed to connect to server [mongo:27017] on first connect
mongo-express_1 | at Pool.<anonymous> (/node_modules/mongodb-core/lib/topologies/server.js:326:35)
mongo-express_1 | at emitOne (events.js:116:13)
mongo-express_1 | at Pool.emit (events.js:211:7)
mongo-express_1 | at Connection.<anonymous> (/node_modules/mongodb-core/lib/connection/pool.js:270:12)
mongo-express_1 | at Object.onceWrapper (events.js:317:30)
mongo-express_1 | at emitTwo (events.js:126:13)
mongo-express_1 | at Connection.emit (events.js:214:7)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:175:49)
mongo-express_1 | at Object.onceWrapper (events.js:315:30)
mongo-express_1 | at emitOne (events.js:116:13)
I have searched this issue all over the internet and github repositories and tried the other solutions, but none of them worked appropriately.
link1 | link2 | link3 | link4 | etc.

First I have run docker stop $(docker ps -a -q) and then changed the docker-compose.yml file to the following content:
version: '2'
services:
mongo:
image: mongo:3.4
container_name: mongo
ports:
- '27017:27017'
volumes:
- '/data/configdb:/data/configdb'
- '/data/db:/data/db'
mongo-express:
image: mongo-express:0.49.0
container_name: mongo_express
depends_on:
- 'mongo'
ports:
- '5050:8081'
environment:
- 'ME_CONFIG_OPTIONS_EDITORTHEME=ambiance'

docker run -d
-p 8081:8081
--name mongo-express
-e ME_CONFIG_MONGODB_ADMINUSERNAME=**<username>**
-e ME_CONFIG_MONGODB_ADMINPASSWORD=**<password>**
--net **<mongo network name>**
-e ME_CONFIG_MONGODB_SERVER=**<mongodb container name>**
-e ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
mongo-express

Related

docker-compose service name problem using mongo official image

I followed official mongo image description trying to using it with docker-compose.But i've noticed that the mongo service name can not be the same as the image name.Here is the situation:
created a docker-compose.yml file which filled with this:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example#mongo:27017/
execute docker-compose
docker-compose -f docker-compose.yml up
Then the error appears:
Could not connect to database using connectionString: mongodb://root:example#mongo:27017/"
mongo-express_1 | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:19)
mongo-express_1 | at Connection.emit (events.js:314:20)
But when I change the service name mongo to myMongo,it just work out!:
version: '3.1'
services:
myMongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example#myMongo:27017/
So, where am i wrong?

Mongo express always crashes in docker-compose

Here is my docker-compose file:
version: "3.1"
services:
mongodb:
container_name: mongodb
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: $$Admin123
MONGO_INITDB_DATABASE: xplorie
# hostname: mongodb
networks:
mynetwork:
ipv4_address: 172.19.0.20
ports:
- 27017:27017
volumes:
- /var/data/mongo:/data/db
mongo-express:
image: mongo-express
environment:
# ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_PORT: 27017
# ME_CONFIG_MONGODB_AUTH_USERNAME: admin
# ME_CONFIG_MONGODB_AUTH_PASSWORD: $$Admin123
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: $$Admin123
# ME_CONFIG_BASICAUTH_USERNAME: admin
# ME_CONFIG_BASICAUTH_PASSWORD: $$Admin123
# networks:
# mynetwork:
# ipv4_address: 172.19.0.21
volumes:
- /var/data/mongoclient:/data/db
depends_on:
- mongodb
ports:
- "3300:3000"
restart: always
networks:
mynetwork:
driver: bridge
ipam:
config:
- subnet: 172.19.0.0/24
gateway: 172.19.0.1
but when I run this mongo express always crashes. this is the log:
mongo-express_1 | Welcome to mongo-express
mongo-express_1 | ------------------------
mongo-express_1 |
mongo-express_1 |
mongo-express_1 | (node:7) [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_1 | Could not connect to database using connectionString: mongodb://mongo:27017"
mongo-express_1 | (node:7) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [Error: getaddrinfo EAI_AGAIN mongo
mongo-express_1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
mongo-express_1 | name: 'MongoNetworkError'
mongo-express_1 | }]
mongo-express_1 | at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
mongo-express_1 | at Pool.emit (events.js:314:20)
mongo-express_1 | at /node_modules/mongodb/lib/core/connection/pool.js:564:14
mongo-express_1 | at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
mongo-express_1 | at /node_modules/mongodb/lib/core/connection/connect.js:32:7
mongo-express_1 | at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
mongo-express_1 | at Object.onceWrapper (events.js:421:26)
mongo-express_1 | at Socket.emit (events.js:314:20)
mongo-express_1 | at emitErrorNT (internal/streams/destroy.js:92:8)
mongo-express_1 | at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
mongo-express_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21)
mongo-express_1 | (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
mongo-express_1 | (node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
deploy_mongo-express_1 exited with code 0
You have the mongo database container on one network (user defined) and the mongo-express container on the default bridge network. So they can't talk to each other.
Also, the hostname of the mongo db container is mongodb, so you should comment that back in.
Mongo express listens on port 8081, so that's the port you should map.
I'd just use the default bridge network and do something like this
version: "3.1"
services:
mongodb:
container_name: mongodb
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: $$Admin123
MONGO_INITDB_DATABASE: xplorie
ports:
- 27017:27017
volumes:
- /var/data/mongo:/data/db
mongo-express:
image: mongo-express
environment:
ME_CONFIG_MONGODB_SERVER: mongodb
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: $$Admin123
# ME_CONFIG_BASICAUTH_USERNAME: admin
# ME_CONFIG_BASICAUTH_PASSWORD: $$Admin123
depends_on:
- mongodb
ports:
- "3300:8081"
restart: always
You should then be able to access Mongo express on http://localhost:3300/.
Be aware that Mongo will not create a database for you because you don't have any initialization scripts. From the docs: 'MongoDB is fundamentally designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created.'
Another thing is that Mongo Express is still alpha software and it tries to connect to the database immediately when it starts and that connection often fails because Mongo needs a little time to be ready. By setting restart: always as you have, you'll restart Express until the connection succeeds. But you might see a failed container or two on startup.

Default Docker Compose for MongoDB and Mongo-Express times out on Debian

I am trying to setup a docker-compose setup to run MongoDB and Mongo-Express on a Debian virtual machine.
I've read up on problems with volumes, same docker networks, setting ME_CONFIG_MONGODB_SERVER to the Mongodb container name and having problems with UFW.
This is my current docker-compose.yml
version: '3.7'
services:
mongodb:
image: mongo
container_name: mongodb
restart: always
ports:
- '27017:27017'
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=default
networks:
- mongo-compose-network
volumes:
- /home/paul/mongodbdocker/data:/data/db
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
depends_on:
- mongodb
networks:
- mongo-compose-network
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_BASICAUTH_USERNAME=admin
- ME_CONFIG_BASICAUTH_PASSWORD=password
ports:
- '8081:8081'
volumes:
- /home/paul/mongodbdocker/data:/data/db
networks:
mongo-compose-network:
driver: bridge
I have disabled the UFW firewall with sudo ufw disable and also edited the ufw configuration as described in here What is the best practice of docker + ufw under Ubuntu from Feng.
The MongoDB starts up, but sadly mongo-express fails on first connection with this error:
mongo-express | Welcome to mongo-express
mongo-express | ------------------------
mongo-express |
mongo-express |
mongo-express | (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://admin:password#mongodb:27017/"
mongo-express | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongodb:27017] on first connect [MongoNetworkTimeoutError: connection timed out
mongo-express | at connectionFailureError (/node_modules/mongodb/lib/core/connection/connect.js:362:14)
mongo-express | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:16)
mongo-express | at Object.onceWrapper (events.js:420:28)
mongo-express | at Socket.emit (events.js:314:20)
mongo-express | at Socket._onTimeout (net.js:483:8)
mongo-express | at listOnTimeout (internal/timers.js:554:17)
mongo-express | at processTimers (internal/timers.js:497:7)]
mongo-express | at Pool.<anonymous> (/node_modules/mongodb/lib/core/topologies/server.js:441:11)
mongo-express | at Pool.emit (events.js:314:20)
mongo-express | at /node_modules/mongodb/lib/core/connection/pool.js:564:14
mongo-express | at /node_modules/mongodb/lib/core/connection/pool.js:1000:11
mongo-express | at /node_modules/mongodb/lib/core/connection/connect.js:32:7
mongo-express | at callback (/node_modules/mongodb/lib/core/connection/connect.js:300:5)
mongo-express | at Socket.<anonymous> (/node_modules/mongodb/lib/core/connection/connect.js:330:7)
mongo-express | at Object.onceWrapper (events.js:420:28)
mongo-express | at Socket.emit (events.js:314:20)
mongo-express | at Socket._onTimeout (net.js:483:8)
mongo-express | at listOnTimeout (internal/timers.js:554:17)
mongo-express | at processTimers (internal/timers.js:497:7)
mongo-express | (node:8) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
mongo-express | (node:8) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Mongo authentication inside Docker

I am trying to run the mongo docker image with authentication. Following the most simple example from the documentation I ran the mongo and the mongo-express images by the docker-compose up command. My docker-compose.yml at this stage:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
This runs, both containers start ok and I can browse the contents of mongo from the mongo-express website. However, whenever I change the username or the password in the docker-compose.yml file, for example to this:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example123
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example123
the mongo-express throws an unauthotrized error message:
mongo-express_1 | Admin Database connected
mongo-express_1 | { MongoError: Authentication failed.
mongo-express_1 | at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1 | at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1 | at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1 | at emitOne (events.js:116:13)
mongo-express_1 | at Socket.emit (events.js:211:7)
mongo-express_1 | at addChunk (_stream_readable.js:263:12)
mongo-express_1 | at readableAddChunk (_stream_readable.js:250:11)
mongo-express_1 | at Socket.Readable.push (_stream_readable.js:208:10)
mongo-express_1 | name: 'MongoError',
mongo-express_1 | message: 'Authentication failed.',
mongo-express_1 | ok: 0,
mongo-express_1 | errmsg: 'Authentication failed.',
mongo-express_1 | code: 18,
mongo-express_1 | codeName: 'AuthenticationFailed' }
mongo-express_1 | unable to list databases
mongo-express_1 | { MongoError: command listDatabases requires authentication
mongo-express_1 | at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1 | at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1 | at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1 | at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1 | at emitOne (events.js:116:13)
mongo-express_1 | at Socket.emit (events.js:211:7)
mongo-express_1 | at addChunk (_stream_readable.js:263:12)
mongo-express_1 | at readableAddChunk (_stream_readable.js:250:11)
mongo-express_1 | at Socket.Readable.push (_stream_readable.js:208:10)
mongo-express_1 | name: 'MongoError',
mongo-express_1 | message: 'command listDatabases requires authentication',
mongo-express_1 | ok: 0,
mongo-express_1 | errmsg: 'command listDatabases requires authentication',
mongo-express_1 | code: 13,
mongo-express_1 | codeName: 'Unauthorized' }
No matter what username or password I enter in docker-compose.yml, I cannot make mongo-express connect to mongo, only if I use the original root and example pair.
Note, that I am not getting the username and password as environment variables, but they are directly typed into the docker-compose.yml file as you can see here.
Also note, that when I change the MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD (mongo's) variables to anything, they don't seem to have an effect, I can still connect with mongo-express using the original root and example credentials.
What causes this behaviour? How can I make this work?
Your docker-compose command:
docker-compose up --build --force-recreate
Mongo image uses anonymous volumes, so you need also --renew-anon-volumes (doc):
docker-compose up --build --force-recreate --renew-anon-volumes
Otherwise previous volume with already initialized DB is used => INITDB env variables won't be used.
What worked for me was just adding the environment variable for the mongodb server.
ME_CONFIG_MONGODB_SERVER: mongo
like this:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_SERVER: mongo
If you create the mongodb container (docker-compose up will build/pull the image and start a container) giving it the username and password, mongodb will configure itself on the first run and never again! Until you create a new container for that service, the default user/pass will be the ones set initially. If you want to change the default, a new container must be created.
You can also add users after the container has started.
Only stopping the service containers (docker-compose stop) will not destroy de container. To do so, call docker-compose down or, when starting, call docker-compose up --force-recreate.
Regards
In my case I had to first stop all my containers.
Then docker system prune -a --volumes
⚠️ This is the last resort. Before using this command check the docker doc,
The docker system prune -a --volumes can solve the problem but a much simpler way is (if you are mounting a data volume for db to be persistent) to just delete and recreate a new one. This way other volumes will be untouched.
And of course combine it with : docker-compose up --build --force-recreate --renew-anon-volumes

Express container unable to connect to Mongo Image with Docker-Compose

I am trying to connect an express container to MongoDB image with docker compose but the connection is being rejected, I can connect to the db with robomongo. I can't get what is happening, this is the express code that connects it:
mongoose.connect('mongodb://localhost:27017/database')
.then(()=>console.log('connection succesfull to url'))
.catch((err)=>console.error(err));
This is the docker-compose file
version: "3"
services:
backend:
build:
context: ../backend
dockerfile: ${PWD}/images/backend/Dockerfile
container_name: backend
ports:
- "${BACKEND_PORT}:${BACKEND_PORT}"
env_file:
- ./deploy.env
environment:
- PORT=3000
- MONGO_CONNECTION=${MONGO_CONNECTION}
command: npm start
links:
- mongodb
depends_on:
- mongodb
front-app:
build:
context: ../front-app
dockerfile: ${PWD}/images/angular/Dockerfile
container_name: front-app
ports:
- "${FRONTEND_PORT}:4200"
env_file:
- ./deploy.env
command: npm start
mongodb:
image: mongo:3.6
container_name: mongo
volumes:
- "${MONGO_DB_DATA}:/data/db"
- "${MONGO_DB_DATA}:/data/configdb"
ports:
- "27017:27017"
This is the error
backend | { MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
backend | at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
backend | at Pool.emit (events.js:182:13)
backend | at Pool.EventEmitter.emit (domain.js:442:20)
backend | at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
backend | at Object.onceWrapper (events.js:273:13)
backend | at Connection.emit (events.js:182:13)
backend | at Connection.EventEmitter.emit (domain.js:442:20)
backend | at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
backend | at Object.onceWrapper (events.js:273:13)
backend | at Socket.emit (events.js:182:13)
backend | at Socket.EventEmitter.emit (domain.js:442:20)
backend | at emitErrorNT (internal/streams/destroy.js:82:8)
backend | at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
backend | at process._tickCallback (internal/process/next_tick.js:63:19)
backend | name: 'MongoNetworkError',
backend | errorLabels: [ 'TransientTransactionError' ],
backend | [Symbol(mongoErrorContextSymbol)]: {} }
Every process running in docker container thinks that he is "the only one in the world". It means that for this process localhost means: my, container's localhost. And your backend is alone in his container so that's why he cannot find mongodb under localhost.
To fix that problem you should place hostname "mongodb" instead of "localhost" as in docker-compose you can access services using theirs names - it means that mongodb container can also access your backend using "backend" domain.
Please note also that "links" is deprecated in docker and shouldn't be used - it's not required in your configuration as docker-compose gives every service within docker-compose file access to each other using method mentioned above.
Jakub Bujny answer is spot on, I did not understand because it did not include an example. So I am adding one.
mongoose.connect('mongodb://mongodb:27017/database')
.then(()=>console.log('connection succesfull to url'))
.catch((err)=>console.error(err));