7

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?

1 Answer 1

7

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"

4
  • 1
    Hmm, I need to do that because I already have mongo data in docker host and I want to use that data in my mongo service. If I use your suggestion and then try to access mongo from my mongo service, my database become empty.
    – Fernando
    Oct 5, 2018 at 8:30
  • 1
    Ok, well, I think in that case you should not define two different databases. If you share the data folder it effectively is only one database anyways. So just define a single mongo-db in your compose file and reference it in front and backend. Oct 5, 2018 at 8:57
  • 1
    hmm ok, I get it. Thank you for your help.
    – Fernando
    Oct 5, 2018 at 9:03
  • 1
    What to do when I want multiple instant having same data. So, when 1 fails, other one can store data. It can be done using replicaset, but why not in this? Feb 4, 2019 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.