I want to run two mongo docker containers with docker compose. The mongo containers have same shared volumes with the docker host. When I ran it with docker compose, only one mongo container is working meanwhile the other is shutting down because it said
DBPathInUse: Unable to lock the lock file: /data/db/mongod.lock (Unknown error). Another mongod instance is already running on the /data/db directory, terminating
This is my docker compose file
version: '3'
services:
frontend:
image: fernandomaxwell/frontend
ports:
- "3007:3007"
networks:
main:
database_frontend:
backend:
image: fernandomaxwell/backend
ports:
- "2007:2007"
networks:
main:
database_backend:
mongo_backend:
image: mongo
volumes:
- "/var/lib/mongodb:/data/db"
ports:
- "27017:27017"
networks:
database_backend:
mongo_frontend:
image: mongo
volumes:
- "/var/lib/mongodb:/data/db"
ports:
- "27018:27017"
networks:
database_frontend:
networks:
main:
database_backend:
database_frontend:
Any idea to solve this?
The problem is here:
The mongo containers have same shared volumes with the docker host
You cannot run two mongo instances on the same data-directory. It would lead to data corruption and strange problems, so mongo-db explicitly prohibits doing that (see also this question here)
Why do you want to do this? Normally you would provide two different volumes for your mongo instances like this:
version: '3'
services:
frontend:
image: fernandomaxwell/frontend
ports:
- "3007:3007"
networks:
main:
database_frontend:
backend:
image: fernandomaxwell/backend
ports:
- "2007:2007"
networks:
main:
database_backend:
mongo_backend:
image: mongo
volumes:
- "/var/lib/mongodb-back:/data/db"
ports:
- "27017:27017"
networks:
database_backend:
mongo_frontend:
image: mongo
volumes:
- "/var/lib/mongodb-front:/data/db"
ports:
- "27018:27017"
networks:
database_frontend:
networks:
main:
database_backend:
database_frontend:
Additionally you should consider to use named volumes, instead of host-paths. Doing that you don't need to take care of creating the directories on the host before starting the compose-file. To use named volumes just change the volume declaration from "/var/lib/mongodb-back:/data/db" to "mongodb-back:/data/db"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With