docker-compose for MongoDB: example file and generator
Run a local MongoDB database with a password that keeps its data.
This docker-compose file starts a MongoDB database on your computer with one command and keeps your data between restarts. The port is open only on your own computer. Choose your version, user, password and port below, then run docker compose up -d.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
services:
mongo:
image: mongo:8
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: change-me
ports:
- "127.0.0.1:27017:27017"
volumes:
- mongo_data:/data/db
volumes:
mongo_data:How this setup works
Step-by-step setup
- Install Docker Desktop (Windows, macOS) or Docker Engine (Linux).
- Save the config above as docker-compose.yml in an empty folder.
- Change the password.
- Run docker compose up -d.
- Connect: mongosh "mongodb://admin:PASSWORD@localhost:27017", using your password.
What the important lines mean
- MONGO_INITDB_ROOT_* creates the admin user the first time the database starts.
- 127.0.0.1:27017:27017 opens the port only on your own computer, not on your network.
- volumes keeps your data when the container is removed.
Common errors and how to fix them
- Authentication failed
- The admin user is created only when the volume is empty. If you changed the password later, run docker compose down -v and start again. This deletes the data.
- port is already allocated
- Something else already uses port 27017. Pick another port above, such as 27018.
- command requires authentication
- You connected without a user. Add the user and password to the connection string.
Frequently asked questions
- How do I connect to MongoDB running in Docker?
- Use mongosh with a connection string such as mongodb://admin:PASSWORD@localhost:27017, replacing the user, password and port with your own.
- Will I lose my data when I stop the container?
- No, as long as the volume is in the file. Data is lost only if you run docker compose down -v, which deletes the volume.
- Why is the port bound to 127.0.0.1?
- So that only programs on your own computer can reach the database. Remove 127.0.0.1: from the ports line only if you really need access from other machines, and then use a strong password and a firewall.
Free tools that help
- Password generatorCreate a random password with the length and characters you choose. It runs in your browser, uses secure randomness and shows how strong it is.
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
Fix a problem with this setup
- Docker port already allocatedFix "port is already allocated" in Docker and Docker Compose: find the container or program that holds the port and free it.
- Docker container cannot reach localhostFix "connection refused" when an app in Docker calls localhost: use host.docker.internal, a Compose service name or host networking.
More container configs
- docker-compose for PostgreSQLRun a local PostgreSQL database that keeps its data.
- docker-compose for MySQLRun a local MySQL database in Docker with your own user, password and port, and keep the data between restarts.
- docker-compose for RedisRun Redis in Docker with a password, saved data and a memory limit.
- docker-compose for WordPressRun WordPress and its database locally in Docker with one command and keep your site between restarts.
- Dockerfile for Node.jsA small, working Dockerfile for a Node.js app, with build step, non-root user and health check options.
- Dockerfile for PythonA working Dockerfile for a Python web app, with Gunicorn, Uvicorn or a plain script.