Container volume can not in sync with host os volume

Hi all,

Recently, I just started to use nodejs and MongoDB in raspberry pi and want to deploy it through the balena cloud.

Thus, I made a docker-compose file and connected MongoDB through the client. Have a look at docker-compose file: https://www.dropbox.com/s/q3r7x8yx7wavc9j/docker-compose.yml?dl=0

Next, I created a test database, and MongoDB service made some files on data directory in the container, but when I restarted the MongoDB service through the balena cloud, the container data is wiped out. This may be an obvious reason, but as I bound the MongoDB container volume with resin-data host volume, container volume data should be auto-synced with host volume in resin-data.

In host os, a resin-data directory has no data. Even, I checked in /var/lib/docker/volumes/_resin-data/_data directory, but It does not have the data,

image

Does anyone have any idea about how the host os volume can be auto sync with the container volume specific in MongoDB database?

Hi,
I’m looking at your dockerfile here: https://github.com/andresvidal/rpi3-mongodb3/blob/master/Dockerfile
it seems like you define /data/db and /data/configdb as volumes (https://github.com/andresvidal/rpi3-mongodb3/blob/master/Dockerfile#L28). This means that docker will create a new volume on run that holds the data that was specified in the Dockerfile (https://docs.docker.com/engine/reference/#volume). So the resin-data volume is mounted at /data and there are two new mount points at /data/db and /data/configdb.
Hope this helps.
Best regards,

Hi @afitzek, My docker-compose file is https://www.dropbox.com/s/q3r7x8yx7wavc9j/docker-compose.yml?dl=0, while the docker file(https://github.com/andresvidal/rpi3-mongodb3/blob/master/Dockerfile), which has been already created for MongoDB by other users. I just used it to install MongoDB.

When I use the mongo docker image in docker, It works but it does not work with balena cloud.

Hi,

I haven’t tried it myself yet, but I would suspect that this compose file should work as expected:

version: '2' # specify docker-compose version
# Define the services/containers to be run
volumes: 
    mongodb-data:
    mongodb-cfg:
services:
    nodejs:
      build: './nodejs' # specify the directory of the Dockerfile
      ports:
        - "3000:3000" #specify ports forewarding
      privileged: true
      restart: always
    mongodb:
      image: andresvidal/rpi3-mongodb3
      volumes:
        - 'mongodb-data:/data/db'
        - 'mongodb-cfg:/data/configdb'
      ports:
        - "27017:27017" # specify port forewarding
      privileged: true
      restart: always
      labels:
        io.balena.features.kernel-modules: true

Can you give it a try?
Best regards,

Hi @afitzek, Thank you so much. It worked, but I get another issue, let’s have a look,

26%20PM

Mongod service is restarting because of the mongod.lock file is not deleted by docker-compose. Docker-compose wants to preserve the volumes for reuse.

Can you please help me?

Thanks and regards,
Nikunj

Hi @afitzek, The issue is resolved by this docker-compose file,

 version: '2' # specify docker-compose version
# Define the services/containers to be run
volumes: 
    mongodb-data:
    mongodb-cfg:
services:
    nodejs:
      build: './nodejs' # specify the directory of the Dockerfile
      ports:
        - "3000:3000" #specify ports forewarding
      privileged: true
      restart: always
    mongodb:
      image: andresvidal/rpi3-mongodb3
      volumes:
        - 'mongodb-data:/data/db'
        - 'mongodb-cfg:/data/configdb'
      ports:
        - "27017:27017" # specify port forewarding
      command: bash -c "mongod --repair && mongod"
      privileged: true
      restart: always
      labels:
        io.balena.features.kernel-modules: true

Thanks, @afitzek.

Regards,
Nikunj

1 Like

Hi,
Great to hear that it works!
Happy coding.
Cheers,

1 Like

I’m having this issue still after using this solution. Service still shuts down and keeps restarting. any ideas @afitzek?

03%20PM

03%20PM|690x243

Hi,
I have no idea why the mongodb would shutdown. I can’t spot any errors in the log you provided, it just seems to decide to shutdown. (I have to say that I never worked with mongodb. So not much expertise there. :wink:)
Although I suspect the db would do a lot of disk IO, which can corrupt cheap sd cards very quickly. Maybe you are hitting something like this. Did it work at some point and just started to restart over and over again?

Hi @mowi22, Can you please post your docker file here , if it is possible?

Best regards,
Nikunj