This article will explain the procedure of running a MongoDB server with Docker.
How to Run a MongoDB Server Using Docker?
To run a MongoDB server with Docker, check out the below-provided steps:
- Pull MongoDB image from Docker Hub
- Start the MongoDB server using the “docker run -d –name <container-name> -p 27017:27017 mongo” command.
- View running container
- Access MongoDB container
- Check MongoDB server
- Connect to MongoDB server
- Execute MongoDB commands
Step 1: Pull MongoDB Image From Docker Hub
First, pull the MongoDB image from Docker Hub using the below-provided command:
The latest version of the MongoDB image has been downloaded successfully.
Step 2: Build and Start MongoDB Container
Then, use the below-provided command to start the MongoDB container and execute the MongoDB server:
- “-d” is utilized to execute the container in the background.
- “–name” assigns a name to the container i.e., “mongoCont”.
- “-p” allocates the port for the container i.e., “27017:27017”.
- “mongo” is the official Docker image to use for the container:
This command starts the MongoDB server.
Step 3: View Running Container
Next, view the running MongoDB container with the help of provided command:
The output indicates that the MongoDB container is running successfully.
Step 4: Access MongoDB Container
After that, use the “docker exec -it” command along with the container name to open the Bash shell inside the running MongoDB container:
The MongoDB container has been accessed and now users can execute commands inside the running container.
Step 5: Check MongoDB Server
To verify whether the MongoDB server is running or not, type out the below-listed command:
It can be observed that the MongoDB server is running successfully with version “v6.0.5”.
Step 6: Launch MongoDB
To connect to the MongoDB server, open the Mongo shell using the following command:
The above output shows that the Mongo shell has been opened successfully.
Step 7: View Existing Databases
Finally, write out the “show dbs” command to view the existing databases:
In the above output, all the existing databases can be seen.
To run a MongoDB server with Docker, first, pull the latest version of the MongoDB image from Docker Hub. Then, start the MongoDB server using the “docker run -d –name <container-name> -p 27017:27017 mongo” command. Next, access the MongoDB container and check the MongoDB server. After that, connect to the MongoDB server and execute MongoDB commands. This article has explained the procedure to run a MongoDB server with Docker.