mongodbHow to specify a password for MongoDB Docker?
MongoDB Docker can be configured to use a password for authentication. To do this, you need to set the MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD environment variables when running the container.
Example
docker run -d -p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
mongo
This will create a MongoDB container with the username admin and password password.
Code explanation
docker run: Runs a command in a new container-d: Runs the container in detached mode, meaning it runs in the background-p 27017:27017: Maps the container's port 27017 to the host's port 27017-e MONGO_INITDB_ROOT_USERNAME=admin: Sets the environment variableMONGO_INITDB_ROOT_USERNAMEtoadmin-e MONGO_INITDB_ROOT_PASSWORD=password: Sets the environment variableMONGO_INITDB_ROOT_PASSWORDtopasswordmongo: The image to use for the container
Helpful links
Related
More of Mongodb
- How to use watch in MongoDB?
- How to use unwind in MongoDB?
- How to use triggers in MongoDB?
- How to use transactions in MongoDB?
- What is MongoDB default port?
- How to list all indexes in MongoDB?
- How to use regex in MongoDB?
- How to use MongoDB queue?
- How to check the version of MongoDB?
See more codes...