|
| 1 | +# How to Run MongoDB in Docker Locally |
| 2 | + |
| 3 | +Running MongoDB inside a Docker container is an efficient way to set up a development database environment without installing MongoDB directly on your system. Follow this guide to get MongoDB up and running locally using Docker. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🧰 Prerequisites |
| 8 | + |
| 9 | +Before you begin, ensure you have the following installed: |
| 10 | + |
| 11 | +* [Docker Desktop](https://www.docker.com/products/docker-desktop/) (for Windows/Mac) |
| 12 | +* [Docker Engine](https://docs.docker.com/engine/install/) (for Linux) |
| 13 | +* Basic knowledge of terminal/command prompt |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## ⚙️ Step 1: Pull the MongoDB Docker Image |
| 18 | + |
| 19 | +Open your terminal and run: |
| 20 | + |
| 21 | +```bash |
| 22 | +docker pull mongo |
| 23 | +``` |
| 24 | + |
| 25 | +This command downloads the official MongoDB image from Docker Hub. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 🏃 Step 2: Run MongoDB Container |
| 30 | + |
| 31 | +Start a MongoDB container with the following command: |
| 32 | + |
| 33 | +```bash |
| 34 | +docker run -d \ |
| 35 | + --name mongodb \ |
| 36 | + -p 27017:27017 \ |
| 37 | + -e MONGO_INITDB_ROOT_USERNAME=admin \ |
| 38 | + -e MONGO_INITDB_ROOT_PASSWORD=admin123 \ |
| 39 | + mongo |
| 40 | +``` |
| 41 | + |
| 42 | +### Explanation: |
| 43 | + |
| 44 | +* `-d`: Runs the container in detached mode (in the background) |
| 45 | +* `--name mongodb`: Assigns the container a name |
| 46 | +* `-p 27017:27017`: Maps MongoDB’s default port to your local system |
| 47 | +* `-e MONGO_INITDB_ROOT_USERNAME`: Sets the root username |
| 48 | +* `-e MONGO_INITDB_ROOT_PASSWORD`: Sets the root password |
| 49 | + |
| 50 | +Once the command runs successfully, MongoDB will start inside the container. |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## 🔍 Step 3: Verify the Container is Running |
| 55 | + |
| 56 | +Run this command to check: |
| 57 | + |
| 58 | +```bash |
| 59 | +docker ps |
| 60 | +``` |
| 61 | + |
| 62 | +You should see an entry similar to: |
| 63 | + |
| 64 | +``` |
| 65 | +CONTAINER ID IMAGE COMMAND STATUS PORTS |
| 66 | +abcd1234efgh mongo "docker-entrypoint..." Up 5 minutes 0.0.0.0:27017->27017/tcp |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## 🧩 Step 4: Connect to MongoDB |
| 72 | + |
| 73 | +You can connect to MongoDB using: |
| 74 | + |
| 75 | +### Option 1: Mongo Shell |
| 76 | + |
| 77 | +```bash |
| 78 | +docker exec -it mongodb mongosh -u admin -p admin123 |
| 79 | +``` |
| 80 | + |
| 81 | +### Option 2: MongoDB Compass (GUI) |
| 82 | + |
| 83 | +* Open MongoDB Compass. |
| 84 | +* Enter the connection string: |
| 85 | + |
| 86 | + ``` |
| 87 | + mongodb://admin:admin123@localhost:27017 |
| 88 | + ``` |
| 89 | +* Click **Connect**. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## 📦 Step 5: Persist Data with a Volume |
| 94 | + |
| 95 | +By default, data inside containers is **temporary**. To persist MongoDB data, mount a local volume: |
| 96 | + |
| 97 | +```bash |
| 98 | +docker run -d \ |
| 99 | + --name mongodb \ |
| 100 | + -p 27017:27017 \ |
| 101 | + -e MONGO_INITDB_ROOT_USERNAME=admin \ |
| 102 | + -e MONGO_INITDB_ROOT_PASSWORD=admin123 \ |
| 103 | + -v ~/mongo-data:/data/db \ |
| 104 | + mongo |
| 105 | +``` |
| 106 | + |
| 107 | +Now your MongoDB data will be stored in the local folder `~/mongo-data` even if the container stops or is removed. |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## 🧹 Step 6: Stop or Remove the Container |
| 112 | + |
| 113 | +To stop MongoDB: |
| 114 | + |
| 115 | +```bash |
| 116 | +docker stop mongodb |
| 117 | +``` |
| 118 | + |
| 119 | +To remove MongoDB: |
| 120 | + |
| 121 | +```bash |
| 122 | +docker rm mongodb |
| 123 | +``` |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## ✅ Step 7: (Optional) Use Docker Compose |
| 128 | + |
| 129 | +Create a file named `docker-compose.yml`: |
| 130 | + |
| 131 | +```yaml |
| 132 | +version: '3.8' |
| 133 | +services: |
| 134 | + mongodb: |
| 135 | + image: mongo |
| 136 | + container_name: mongodb |
| 137 | + ports: |
| 138 | + - "27017:27017" |
| 139 | + environment: |
| 140 | + MONGO_INITDB_ROOT_USERNAME: admin |
| 141 | + MONGO_INITDB_ROOT_PASSWORD: admin123 |
| 142 | + volumes: |
| 143 | + - mongo-data:/data/db |
| 144 | + |
| 145 | +volumes: |
| 146 | + mongo-data: |
| 147 | +``` |
| 148 | +
|
| 149 | +Then run: |
| 150 | +
|
| 151 | +```bash |
| 152 | +docker-compose up -d |
| 153 | +``` |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## 🚀 You’re Done! |
| 158 | + |
| 159 | +Your MongoDB instance is now running locally inside Docker. You can connect from your backend apps using this connection string: |
| 160 | + |
| 161 | +``` |
| 162 | +mongodb://admin:admin123@localhost:27017 |
| 163 | +``` |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +### 💡 Pro Tips |
| 168 | + |
| 169 | +* Use Docker volumes to persist data. |
| 170 | +* Use environment variables or `.env` files for credentials. |
| 171 | +* Stop and start containers easily with `docker stop` and `docker start`. |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +**Author:** *Your Name* |
| 176 | +**Date:** $(date +%Y-%m-%d) |
0 commit comments