2
votes

In my project I have few container managed by docker-compose. I would like to store data from mongoDB on persistent volume. MongoDB seems to store data in /data/db but files are not transferred to my host file system and they disappear after rebuilding container.

Here is my docker-compose config file:

version: '2'

services:

  db:
    image: mongo:latest
    volumes: 
      - "db-data:/data/db"
    ports:
      - "27017:27017"
    networks:
      - elk

  web: 
    build: ./app
    environment:
      - WAIT_COMMAND=1
    ports:
      - "3000:3000"
    networks:
      - elk
    depends_on: 
      - db


networks:
  elk:
    driver: bridge

volumes:
  db-data:

1

1 Answers

4
votes

You tries to use what is called 'named volumes' that are stored inside docker itself. To mount volume to your file system just write path and not volume name. https://docs.docker.com/compose/compose-file/#short-syntax-3

version: '2'

services:

  db:
    image: mongo:latest
    volumes: 
      - /data/db:/data/db
    ports:
      - "27017:27017"
    networks:
      - elk

  web: 
    build: ./app
    environment:
      - WAIT_COMMAND=1
    ports:
      - "3000:3000"
    networks:
      - elk
    depends_on: 
      - db


networks:
  elk:
    driver: bridge