Best-practice configuration for MongoDB with docker-compose

Create /var/lib/mongodb/docker-compose.yml:

This will store the MongoDB data in /var/lib/mongodb/data. I prefer this variant to using docker volumes since this method keeps all MongoDB-related data in the same directory.

Then create a systemd service using

See our post on how to Create a systemd service for your docker-compose project in 10 seconds for more details on this method.

You can access MongoDB at localhost:27017! It will autostart after boot

Restart by

Stop by

View logs:

View logs in less:


Check out similar posts by category: Docker, MongoDB