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.
- Root user was not created
- MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD must both be set, and they have no effect once /data/db already holds a database. The user is created in the admin database.
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.
- Is authentication on by default?
- No. The image page says the default configuration requires no authentication. Setting both root variables starts mongod with --auth.
- What does MONGO_INITDB_DATABASE do?
- It chooses the database that .js scripts in /docker-entrypoint-initdb.d run against (test if unset). MongoDB creates a database only when data is first inserted.
- Which folders hold the data?
- /data/db for data. The image also declares /data/configdb for config servers.
Free tools that help
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 port mappingWhat -p 8080:80 and ports: "8080:80" mean in Docker: the first number is on your computer, the second is inside the container. Which URL to open and how to fix 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.