Docker compose mirroring folder

Viewed 915

I'm trying to create a mongodb service that runs in a docker container. My purpose is persist all the data inside container in the host machine. For this I have a docker-compose.yml file, whose content is:

version: '3.2'

services:
  mongodb:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: myrootmongousername
      MONGO_INITDB_ROOT_PASSWORD: myrootmongopassword
      MONGO_INITDB_DATABASE: dbnameiwanttocreate
    ports:
      - '27010:27017'
    volumes:
      - 'mongodata:/data'

As you can see, my volumes section declares to drop all content of /data folder inside container into mongodata folder in host. I created a folder named mongodata just at the same height that my docker-compose.yml file.

myprojectfolder
|__docker-compose.yml
|__mongodata

When I do docker-compose up it creates a container and I can connect and so on. However, my mongodata folder is completely empty. That's cannot be true, because if I go inside docker container (docker exec -it <container-id> bash) and explore /data folder, it is not empty at all.

What is my mistake here?

Thanks!

1 Answers

The following Docker compose file works for the directory structure below on ubuntu 18.04, check if docker has permissions to write into the local folder mongodata

If that doesn't work paste logs by running docker-compose logs will update answer specific to the issue.

version: '3.2'

services:
  mongodb:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: myrootmongousername
      MONGO_INITDB_ROOT_PASSWORD: myrootmongopassword
      MONGO_INITDB_DATABASE: dbnameiwanttocreate
    ports:
      - '27010:27017'
    volumes:
      - './mongodata:/data'
├── docker-compose.yml
└── mongodata
    ├── configdb
    └── db
Related