Solution to Challenge: Running MongoDB Database
Learn the solution to the challenge you completed.
We'll cover the following
Solution
The following command will run the MongoDB
:
docker run \
-it --rm --name mongodb \
-p 3000:27017 \
--mount "src=mongodata,target=/data/db" \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=mysecret \
mongo
-
-it
runs an interactive session. -
--rm
removes the container after the container stops. -
--name
is used to name the container asmongodb
. -
-p
is used to define both the exposed port (left side of:
) and the default port (right side of:
) on whichmongodb
is running. -
--mount
is used to define persistent Docker volume namedmongodata
. -
-e
allows the setting of required environmental variables.MONGO_INITDB_ROOT_USERNAME
sets the root user andMONGO_INITDB_ROOT_PASSWORD
sets the root password.
π Connect the terminal to see the above command in action.
Get hands-on with 1400+ tech skills courses.