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?

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.

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

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.

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� MongoDB will have user root and password rootpassword. It will expose port 27017 to the host machine. It will also using data container to save the data, called mongodb_data_container. It's useful for data persistent, so the data will not be deleted even later you call command docker-compose down. Run it

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.

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

How to spin MongoDB server with Docker and Docker Compose, Tagged with mongodb, docker, dockercompose. It will also using data container to save the data, called mongodb_data_container . It's useful� I just learned how to use docker with docker-compose to simplified my full-stack side-project. It’s easy to make a project with docker as a container to images like MongoDB, Redis, Elastic Search…

Since Docker-Compose syntax version 3.2, you can use a long syntax of the volume property to specify the type of volume. This allows you to create a "Bind" volume, which effectively links a folder from a container to a folder in your host.

Here is an example :

version : "3.2"

        container_name: mongo
        image: mongo
                - type: bind
                  source: /data
                  target: /data/db
                - "42421:27017"

source is the folder in your host and target the folder in your container

More information avaliable here :

MongoDB container with Docker Compose, Docker Compose. How to run MongoDB container with Docker Compose? directory as /data/db/ in the container, storing MongoDB database files locally. - . As you can see, docker-compose binary is working correctly. Setting Up MongoDB Server using Docker Compose: In this section, I will show you a very basic MongoDB and Mongo Express setup using Docker Composer. First, create a new project directory mongo-latest/ as follows: $

Docker-Compose MongoDB – Linux Hint, Save the file and run from the same directory: $docker-compose up -d. This will start a mongoDB service with two volumes named db-data and mongo-config the � image: mongo:latest Specifies an image to use, in this case the mongo:latest.At the moment of writing this maps to version 4.0.9 based on Ubuntu Xenial. Optionally, if you want to build your own version of the image, you could replace this line with build: .docker/mongodb and build your own image using the Dockerfile.

MongoDB with Docker Compose, They work in the same way, the only difference being in how the data is stored. The ListDataCollector stores data as a list of objects on your� We still use MongoDB’s data location, /data/db, as in container. volumes: shared_database:/data/db Let’s update our docker-compose file and add binding to the named volume.

Import data to MongoDB � Codefresh, If you want to import/restore or to do something else before using mongo db in your In the root of this repository you'll find a file named docker-compose.yml . MongoDB document databases provide high availability and easy scalability.