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 check the version of MongoDB?
- How to work with time series data in MongoDB?
- How to empty an array in MongoDB?
- How to use watch in MongoDB?
- How to update many documents in MongoDB?
- How to use unwind in MongoDB?
- How to use triggers in MongoDB?
- How to list MongoDB users?
- How to perform a health check for MongoDB?
- How to use the limit operator in MongoDB?
See more codes...