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_USERNAME
toadmin
-e MONGO_INITDB_ROOT_PASSWORD=password
: Sets the environment variableMONGO_INITDB_ROOT_PASSWORD
topassword
mongo
: The image to use for the container
Helpful links
Related
More of Mongodb
- How to use triggers in MongoDB?
- How to use unwind in MongoDB?
- How to list MongoDB users?
- How to rename a field in MongoDB?
- How to rename a MongoDB collection?
- What is MongoDB default port?
- How to convert MongoDB ObjectId to string?
- How to create a many to many relation in MongoDB?
- How to work with time series data in MongoDB?
- How to empty an array in MongoDB?
See more codes...