Given that docker is active and running, you can deploy MongoDB by the following steps:

‐it flag provides an interactive shell to the docker container, ‐v flag maps the mongoData host volume to the /data/db container volume, ‐‐name is the name of the container and ‐d indicates running the container as a background process.

  • Download the latest MongoDB docker image:
    $sudo docker pull mongo
  • In case you need a specific version of MongoDB (e.g. 4.4.1), then type:
    $sudo docker pull mongo:4.2.2
  • Create a directory on your host system:
    $sudo mkdir ~/mongoData
  • Start the container with the command:
    $sudo docker run -it -v ~/mongoData:/data/db --name mongodb -d mongo
  • The mongo word is the image’s name Connect to the container:
    $sudo docker exec -it mongodb bash
  • Then type:
    $sudo mongo