Docker - Setting MongoDB
Docker - Setting up MongoDB
Section titled “Docker - Setting up MongoDB”MongoDB is a popular NoSQL document-oriented database frequently used in modern web applications. Docker makes it easy to run MongoDB instances for development, testing, or even production.
This guide shows how to run a MongoDB server container and connect to it from another container using Docker’s networking features.
Running the MongoDB Server Container
Section titled “Running the MongoDB Server Container”Step 1: Pull the official MongoDB image from Docker Hub.
# Pull the latest official MongoDB imagedocker pull mongoStep 2: Create a user-defined network. This allows containers to easily communicate using their names.
docker network create mongo-netStep 3: Run the MongoDB server container, connecting it to the network and setting necessary environment variables.
docker run -d \ --name mongodb-server \ --network mongo-net \ -e MONGO_INITDB_ROOT_USERNAME=mongoadmin \ -e MONGO_INITDB_ROOT_PASSWORD=secret \ mongoExplanation:
-d: Run in detached mode (background).--name mongodb-server: Assign a name to the container.--network mongo-net: Connect the container to our user-defined network.-e MONGO_INITDB_ROOT_USERNAME=...: Sets the root username on first initialization.-e MONGO_INITDB_ROOT_PASSWORD=...: Sets the root password on first initialization. Use strong passwords in real scenarios!mongo: The image to use.
Optional: For data persistence (highly recommended), mount a volume:
# Create a named volume first (optional, Docker creates it if it doesn't exist)# docker volume create mongo-data
docker run -d \ --name mongodb-server \ --network mongo-net \ -v mongo-data:/data/db \ -e MONGO_INITDB_ROOT_USERNAME=mongoadmin \ -e MONGO_INITDB_ROOT_PASSWORD=secret \ mongo-v mongo-data:/data/db maps the named volume mongo-data to the default MongoDB data directory inside the container, ensuring your data survives container restarts or removal.
Connecting with a MongoDB Client Container
Section titled “Connecting with a MongoDB Client Container”Step 4: Run another container using the same mongo image to act as a client. Connect it to the same network.
# Run a temporary interactive mongo container connected to the networkdocker run -it --rm --network mongo-net mongo bash-it: Run interactively with a TTY.--rm: Automatically remove the container when it exits.--network mongo-net: Connect to the same network as the server.mongo: Use the mongo image.bash: Start a bash shell inside the client container.
Step 5: Inside the client container’s bash shell, connect to the server using the mongosh (or older mongo) client, referencing the server container by its name (mongodb-server).
# Inside the client container's shell:mongosh --host mongodb-server --username mongoadmin --password secretExplanation:
mongosh: The MongoDB shell command.--host mongodb-server: Connect to the server using its container name (Docker resolves this via the user-defined network).--username mongoadmin: Use the root username defined earlier.--password secret: Use the root password defined earlier. (You might be prompted interactively if you omit--password secret).
If the connection is successful, you will see the MongoDB shell prompt (>).
Step 6: You can now run MongoDB commands.
# Example commands inside the MongoDB shell:show dbsuse myNewDatabasedb.myCollection.insertOne({ name: "Docker Test" })db.myCollection.find()Type exit to leave the MongoDB shell, and exit again to leave the client container (which will be removed due to --rm). Your mongodb-server container continues running in the background.