Setup with Docker
The fastest way to get a real MongoDB to play with is Docker. The official mongo image gives you a clean, isolated server in one command, with nothing installed permanently on your machine and nothing to uninstall afterwards. This lesson gets you from “Docker is running” to “I can talk to MongoDB” — first with a one-liner, then with a small docker-compose.yml you can keep in your project.
You only need Docker Desktop (or the Docker engine) installed and running. Everything else comes from the image.
A one-line docker run
Section titled “A one-line docker run”This command pulls the official image, starts a container named mongo-dev, publishes the default MongoDB port 27017 to your machine, stores the data in a named volume so it survives a restart, and sets a root username and password:
docker run -d \ --name mongo-dev \ -p 27017:27017 \ -v mongo-data:/data/db \ -e MONGO_INITDB_ROOT_USERNAME=root \ -e MONGO_INITDB_ROOT_PASSWORD=secret \ mongo:7A few things worth understanding in that command:
-druns the container in the background (detached) so it does not tie up your terminal.-p 27017:27017maps the container’s port27017to the same port on your host, which is where MongoDB listens by default.-v mongo-data:/data/dbmounts a named volume at/data/db, the directory MongoDB writes its files to. The volume lives outside the container, so deleting and recreating the container does not lose your data.-e MONGO_INITDB_ROOT_USERNAME/-e MONGO_INITDB_ROOT_PASSWORDcreate an administrative user the first time the container initialises an empty data directory.
A docker-compose file
Section titled “A docker-compose file”For anything beyond a quick test, a docker-compose.yml is nicer: the configuration lives in a file you can commit, and a single command brings everything up. This version runs MongoDB and adds mongo-express, a lightweight web UI for poking at your data in the browser:
services: mongo: image: mongo:7 container_name: mongo-dev restart: unless-stopped ports: - "27017:27017" environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: secret volumes: - mongo-data:/data/db
mongo-express: image: mongo-express:latest container_name: mongo-express restart: unless-stopped ports: - "8081:8081" environment: ME_CONFIG_MONGODB_ADMINUSERNAME: root ME_CONFIG_MONGODB_ADMINPASSWORD: secret ME_CONFIG_MONGODB_URL: "mongodb://root:secret@mongo:27017/" depends_on: - mongo
volumes: mongo-data:Bring the whole stack up from the directory that holds the file:
docker compose up -dWhen it is running you can open mongo-express at http://localhost:8081 to browse databases and collections in a browser. The mongo-express service is optional — delete it if you only want the database.
How the pieces fit together
Section titled “How the pieces fit together”The container runs MongoDB and listens on port 27017. Your published port forwards localhost:27017 into it, the named volume keeps the data safe outside the container, and anything that speaks the MongoDB protocol — mongosh, Compass, or your application — connects through that port.
flowchart LR App["Your app / mongosh / Compass"] -->|"mongodb://localhost:27017"| Port["Host port 27017"] Port --> Container["mongo container"] Container -->|"writes to /data/db"| Volume["named volume: mongo-data"]
Verify it is running
Section titled “Verify it is running”Check that the container is up with docker ps:
docker psYou should see a row for the mongo image with a status of Up and the port mapping 0.0.0.0:27017->27017/tcp. Now connect with the shell. The mongosh client ships inside the image, so you can run it without installing anything:
docker exec -it mongo-dev mongosh "mongodb://root:secret@localhost:27017"If you have mongosh installed on your host instead, point it at the published port directly:
mongosh "mongodb://localhost:27017"A successful connection drops you at a test> prompt. From there, typing db.runCommand({ ping: 1 }) and getting back { ok: 1 } confirms the server is alive and answering.
Tips and gotchas
Section titled “Tips and gotchas”- Data persistence lives in the volume, not the container. As long as you keep the
mongo-datavolume, you can stop, remove, and recreate the container without losing anything. Runningdocker volume rm mongo-datais what actually deletes the data. - The root user is created only on first init. The
MONGO_INITDB_ROOT_*variables take effect when the data directory is empty. If you change them later against an existing volume, they are ignored — the existing user stays. - Port
27017is the MongoDB default. If something else already uses it, change the host side of the mapping, for example-p 27018:27017, and connect to27018. - Pin a major version like
mongo:7rather thanmongo:latestso an image update does not surprise you with a different server version.