docker-compose service name problem using mongo official image - mongodb

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?

Related

Getting "MongoServerError: Authentication failed" when connecting to MongoDB in Docker container

Here is my docker-compose file:
version: '3' services: mongo:
image: mongo
container_name: mongo
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME: admin
- MONGO_INITDB_ROOT_PASSWORD: root
ports:
- 27017:27017
volumes:
- ./mongo-data:/data/db
command: --wiredTigerCacheSizeGB 1.5
I try to connect to Mongo container but constantly receive the same error:
MongoServerError: Authentication failed
Here are 2 variants of connection strings I've used, neither worked:
mongodb://admin:root#localhost:27017/admin
mongodb://admin:root#localhost:27017/admin?authSource=admin
What can I try next?

MongoError: Authentication failed with docker-compose

I am trying to build this app https://github.com/dwgebler/node-express-example. I want to use it as a template for making a Mongodb http express server. I tried to build the docker compose file in this repo which looks like this (modified slightly):
version: "3.8"
services:
database:
image: mongo
container_name: mongo-database
restart: always
volumes:
- "mongodata:/data/db"
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_SERVER: mongo-database
web:
image: node:16-alpine
container_name: node-server
restart: always
user: "node"
ports:
- "9443:443"
volumes:
- "./app:/var/app/"
working_dir: "/var/app"
environment:
NODE_ENV: dev
DB_USER: root
DB_PASSWORD: example
DB_NAME: mongo-database
command: "./wait.sh mongo-database 27017 'npm start'"
volumes:
mongodata:
But when I run the command docker compose up -d in the same directory as the docker-compose.yml file, I get this in the "node-server" container logs:
Mongo started
MongoError: Authentication failed.
at MessageStream.messageHandler (/var/app/node_modules/mongodb/lib/cmap/connection.js:272:20)
at MessageStream.emit (node:events:513:28)
at processIncomingData (/var/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
at MessageStream._write (/var/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
at writeOrBuffer (node:internal/streams/writable:391:12)
at _write (node:internal/streams/writable:332:10)
at MessageStream.Writable.write (node:internal/streams/writable:336:10)
at Socket.ondata (node:internal/streams/readable:754:22)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:315:12) {
ok: 0,
code: 18,
codeName: 'AuthenticationFailed'
}
Why am I getting a MongoError: Authentication failed? Notice that I set
DB_USER: root
DB_PASSWORD: example
DB_NAME: mongo-database
In the web section of the docker file which matches the MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD, from the database section and I set DB_NAME to mongo-database which matches the database section name. So why isn't it authenticating?
MONGO_INITDB_ROOT_USERNAME and PASSWORD create a user in the Mongo admin database. Your connection string tries to authenticate against the mongo-database database and it can't find the user. There are messages in the Mongo logs that tell you this.
To fix it, you need to authenticate against the admin database by adding authSource=admin to your connection string, like this
const uri = `mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}#mongo-database/${process.env.DB_NAME}?retryWrites=true&writeConcern=majority&authSource=admin`;

MongoDB authentication fails in Docker

My docker-compose.yml
mongo:
image: mongo
volumes:
- ./mongo:/data/db
ports:
- 27017:27017
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: pass
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: pass
ME_CONFIG_MONGODB_URL: mongodb://root:pass#mongo:27017/
When I run docker-compose up it logs the following errors
for the mongo service:
UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
and for the mongo-express service:
"error":"UserNotFound: Could not find user "root" for db "admin"
I tried adding ?authSource=admin to the ME_CONFIG_MONGODB_URL but the error remains.

Docker mongo-express cannot connect to mongo if started with /data/db volume

I have a docker-compose.yml which I start with docker-compose up — and it works.
version: "3"
services:
mongo:
image: mongo
restart: always
ports:
- 27017-27019:27017-27019
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
MONGO_INITDB_DATABASE: brandRegistry
volumes:
- ./setup/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
# - ./data/mongo-volume:/data/db
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
depends_on:
- mongo
But if I comment - ./data/mongo-volume:/data/db in the log says:
mongo-express_1 | Tue Jan 28 22:21:16 UTC 2020 retrying to connect to mongo:27017 (4/5)
mongo-express_1 | /docker-entrypoint.sh: connect: Connection refused
mongo-express_1 | /docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Connection refused
I do not see how this relates to the volume...
I would like to store persistent data. Do you have a tip?
Thanks in advance!
I just did not wait long enough. Mongo needs way longer if started with a data volume outside of the container. And mongo-express will try to connect again and again. After a while it will succeed.
Add this line to mongo-express configuration on docker-compose.yaml [or how ever you named the file]
restart: unless-stopped
mongo-express will restart , until mongo container is ready, and when it is ready it will connect and stay up.

How to use Robo3T to connect mongodb server running as a docker container

I have a mongodb running as a docker container which created by docker-compose.
Here is my docker-compose.yml:
verion:'3.7'
services:
mongo_env:
image: mongo:4.2.1-bionic
ports:
- "27017:27017"
volumes:
- $PWD/DBVOL/mongo/data:/data/db:rw
environment:
MONGO_INITDB_ROOT_USERNAME: <rootuser>
MONGO_INITDB_ROOT_PASSWORD: <mypassword>
MONGO_INITDB_DATABASE: <mydatabase>
I want to connect mongodb from outside using Robo 3T,But It always tell me that 'Failed to load database'.Here is my Robo 3T config:
Please Help me It's emergency!
Are your sure it started successfull? Your Docker Compose has a typo in "version:3.7".
correct version:
version: '3.7'
services:
mongo_env:
image: mongo:4.2.1-bionic
ports:
- "27017:27017"
volumes:
- $PWD/DBVOL/mongo/data:/data/db:rw
environment:
MONGO_INITDB_ROOT_USERNAME: <rootuser>
MONGO_INITDB_ROOT_PASSWORD: <mypassword>
MONGO_INITDB_DATABASE: <mydatabase>
Please check after starting if the container is running with
docker ps