I want to disable the default auth (avoid to use mongo --authenticationDatabase "auth_db").
in mongo using docker-compose, here is my docker-compose file:
version: "2"
services:
mongodb:
image: mongo:latest
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/null
- MONGO_INITDB_ROOT_USERNAME=some_user
- MONGO_INITDB_ROOT_PASSWORD=some_password
volumes:
- ./mongo_data:/data/db
ports:
- 27017:27017
For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.
Running MongoDB in a Docker Container You can pull the latest MongoDB image and run it in a Docker container. For production, the application can connect to a cloud-hosted database using the MongoDB Atlas or MongoDB Enterprise Server. This will pull the latest official image from Docker Hub.
Enable MongoDB auth in docker-compose In order to enable auth in MongoDB we will use --auth flag in docker-compose. After that we could use docker-compose up -d command again, to run MongoDB container. Connect MongoDB with defined authentication
We can place this script in a folder (lets call it mongo-scripts) that we will volume mount into the Docker container. Also, we need to start MongoDb with authentication enabled to be able to authenticate. MongoDb will now start up, run the scripts that create our users and then restart in authentication mode.
To authenticate as a user, you must provide a username, password, and the authentication database associated with that user. To authenticate using the mongo shell, either: Connect first to the MongoDB or mongos instance. Run the authenticate command or the db.auth () method against the authentication database.
MongoDB Atlas and MongoDB Server provide a variety of authentication mechanisms that allow users to secure their deployment, from a simple username/password authentication to a full enterprise-grade authentication mechanism, like LDAP and Kerberos.
Just don't pass the MONGO_INITDB_ROOT_USERNAME
and MONGO_INITDB_ROOT_PASSWORD
env vars when creating the container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With