All tools / Docker Compose examples
MongoDB with Docker Compose
MongoDB with authentication, a named volume, a health check and the port published only on localhost.
Customize names, versions and passwords →
compose.yaml
services:
mongodb:
image: mongo:8.0
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: "admin"
MONGO_INITDB_ROOT_PASSWORD: "change-me"
volumes:
- mongodb-data:/data/db
ports:
- "127.0.0.1:27017:27017"
healthcheck:
test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand('ping')"]
interval: 10s
timeout: 5s
retries: 10
volumes:
mongodb-data:
Passwords here are placeholders (change-me). The generator creates random ones for you.
How to use it
Commands
docker compose up -d docker compose ps docker compose logs -f
- Connect with mongodb://admin:<password>@localhost:27017/?authSource=admin
Prefer Kubernetes? See the MongoDB Kubernetes version.