How to store MongoDB data using docker-compose

I have this docker-compose:

version: "2" services: api: build: . ports: - "3007:3007" links: - mongo mongo: image: mongo volumes: - /data/mongodb/db:/data/db ports: - "27017:27017" 

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 is stored locally?

It works in 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 added the /data directory in the File Sharing section of the Mac docker.

enter image description here

Is the idea to use the same docker composition for both production and development? How to solve this problem?

+5
source share
2 answers

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

You added /data to the shared folders of your development computer, but you did not create /data/mongodb/db , so you get permission denied error. Docker does not have permission to create folders.

+7
source

I got the impression that you need to learn a little more about the basics of Docker in order to fully understand what you are doing. There are many potential pitfalls with Docker, and I recommend that you learn the basics well so that you know how to deal with them.

Here's what the documentation of volumes says:

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

So you are mistaken. The first part is the past on the host, for example, on your local computer, and the second is where the volume is mounted inside the container.

For your final question, take a look at this article: Using Compose in Production .

0
source

Source: https://habr.com/ru/post/1269103/


All Articles