MENU
2 Pulling & Running Containers
1) From Docker Hub,
pull the MongoDB image (from the default 'docker.io/library' repository), by running the following on the command-line terminal:
docker pull mongo

2) Check that you have indeed downloaded the image.
docker images

You can pull a specific version of the image by specifying the tag, eg. 'docker pull mongo:windowsservercore-ltsc2022', 'docker pull mongo:5.0'.
3) Now you can run the MongoDB container. (use either the image ID or container name.)
docker run -d mongo
Running the container in the detached mode (-d) allows you to continue entering commands in the same terminal as the MongoDB container is launched in the background.
Note that the 'run' command will 'pull' the image as well if you have not already done so.
4) Check that the container is indeed running:
docker ps

Notice that the container has been artibrarily named as "thirsty_satoshi". You can create the alias name of the container with 'docker run -d -name my-container mongo' in step 3.
5) To check the logs of the MongoDB container (use either the container ID or alias name.):
docker logs c16566e410b5
docker logs thirsty_satoshi
6) To stop the container (use either the container ID or alias name.):
docker stop c16566e410b5
docker stop thirsty_satoshi
7) To restart the container (use either the container ID or alias name.):
docker start c16566e410b5
docker start thirsty_satoshi
8) In case you want to want to obtain the container ID or alias name of a stopped container, you can use the 'ps' command with the '-a' flag, which displays the history of running containers:
docker ps -a
9) To navigate the virtual Linux file system of the container (use either the container ID or alias name.):
docker exec -it c16566e410b5 /bin/sh
docker exec -it thirsty_satoshi /bin/sh

10) To delete the image file, you will have to stop and delete the container first (use either the container ID or alias name. use either the image ID or image name.):
docker stop thirsty_satoshi
docker rm thirsty_satoshi
docker rmi mongo

You can create and run different MongoDB containers out of the same MongoDB image, as long as the alias names are different.
You can also stop and remove multiple containers in one go:
docker stop thirsty_satoshi another_container
docker rm thirsty_satoshi another_container