Create a MongoDB Container on Docker
I will show you how to create a MongoDB container easily. We won’t use Dockerfile, Docker-compose, or any files. Instead, we’ll set up everything using just the command line. We’ll use Docker volumes to store the data so it stays even after the container is closed.
Create the network and docker volume
docker network create spark-network
docker volume create mongo_data
Now run the main command
docker run -d --name mongoDB --network spark-network -p 27017:27017 -v mongo-data:/data/db mongo
-d
: Run the container in detached mode.--name mongo
: Assign the name “mongo” to the container.--network spark-network
: Connect the container to the netowrk you created, can be any network “spark-network”.-p 27017:27017
: Map port 27017 on the host to port 27017 in the container.-v mongo-data:/data/db
: Use a Docker volume named “mongo-data” to persist MongoDB data.
Connect to MongoDB
You can now connect to MongoDB using a MongoDB client or management tool like MongoDB Compass, Robo 3T, or Studio 3T.
Here is an example:
mongodb://localhost:27017
Summary of the setup and steps
Step by Step Details | |
---|---|
Pull MongoDB Image | First, pull the latest MongoDB image from Docker Hub using this command: docker pull mongo |
Run MongoDB Container | Run a MongoDB container with this command. It names the container, exposes the port, and sets up a volume for data: docker run -d --name mongoDB --network cms-network -p 27017:27017 -v mongo-data:/data/db mongo |
Access MongoDB Shell | After starting the MongoDB container, access the MongoDB shell with this command: docker exec -it my-mongo mongo |
Connect to MongoDB from an App | Connect to your MongoDB container from any app using localhost as the hostname and 27017 as the port. |
Add a Docker Volume to MongoDB | To persist data, create a Docker volume and attach it to your MongoDB container with these commands: docker volume create mongo-data docker run --name my-mongo -d -p 27017:27017 -v mongo-data:/data/db mongo This creates a volume named mongo-data and starts a MongoDB container using this volume for data storage. |
Mount Host Directory in MongoDB Container | You can also mount a directory from your host system into the container to store data. This is useful for backups, restores, or direct access to MongoDB files: docker run --name my-mongo -d -p 27017:27017 -v /path/on/host:/data/db mongo Replace /path/on/host with the directory path on your host machine where you want to store MongoDB data. This links the /data/db directory in the container to the specified path on your host. |
Custom Configuration | For custom MongoDB settings, you can mount a configuration file from the host into the container when it runs. |
Secure MongoDB | For production, secure MongoDB with authentication. Start by running the container with the --auth flag to enable security: docker run --name my-mongo -d -p 27017:27017 -v ~/mongo-data:/data/db mongo --auth Then, create an admin user inside the MongoDB shell. |