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 as mongodb.

  • -p is used to define both the exposed port (left side of :) and the default port (right side of :) on which mongodbis running.

  • --mount is used to define persistent Docker volume named mongodata.

  • -e allows the setting of required environmental variables. MONGO_INITDB_ROOT_USERNAME sets the root user and MONGO_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.