How to store MongoDB data with docker-compose

I have this docker-compose:

version: "2"
    build: .
      - "3007:3007"
      - mongo
    image: mongo
      - /data/mongodb/db:/data/db
      - "27017:27017"

The volumes, /data/mongodb/db:/data/db, is the first part (/data/mongodb/db) where the data is stored inside the image and the second part (/data/db) where it’s stored locally?

  • spring rabbitmq client closes channel with error “Channel shutdown: connection error”
  • Elastic Beanstalk docker error
  • Graylog container cannot connect to MongoDB container
  • Apache Ignite cache operation failure
  • How to make postgresql database connectable from outside of the postgres docker container?
  • How to set proxy on rstudio from dockerhub behind a proxy?
  • It works on production (ubuntu) but when i run it on my dev-machine (mac) I get:

    ERROR: for mongo  Cannot start service mongo: error while creating mount source path '/data/mongodb/db': mkdir /data/mongodb: permission denied

    Even if I run it as sudo. I’ve added the /data directory in the “File Sharing”-section in the docker-program on the mac.

    enter image description here

    Is the idea to use the same docker-compose on both production and development? How do I solve this issue?

  • Docker API Security - How prevent certain functionality from client?
  • automated docker build run error: Unable to find image
  • Connect Symfony project to docker database
  • Since docker shares host kernel, why most docker images contain the kernel stuff?
  • Docker alpine image : ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2 “No such file or directory”)
  • How to integrate spark cluster with other Hadoop2.x cluster on Docker
  • 2 Solutions collect form web for “How to store MongoDB data with docker-compose”

    Actually it’s the other way around (HOST:CONTAINER), /data/mongodb/db is on your host machine and /data/db is in the container.

    You have added the /data in the shared folders of your dev machine but you haven’t created /data/mongodb/db, that’s why you get a permission denied error. Docker doesn’t have the rights to create folders.

    I get the impression you need to learn a little bit more about the fundamentals of Docker to fully understand what you are doing. There are a lot of potential pitfalls running Docker in production, and my recommendation is to learn the basics really well so you know how to handle them.

    Here is what the documentation says about volumes:

    […] specify a path on the host machine (HOST:CONTAINER)

    So you have it the wrong way around. The first part is the past on the host, e.g. your local machine, and the second is where the volume is mounted within the container.

    The error you see is caused by the fact that there is no /data/mongodb on your local machine, and creating that path fails due to permissions. To prevent this, use a location in your user home, i.e. something under ~/.

    Regarding your last question, have a look at this article: Using Compose in production.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.