简体   繁体   中英

How to attach persistent volume in docker-compose file for mongodb?

I have a docker-compose file that will bring up mongo and mongo-express containers in the same network "mynet". I have created a network by:

docker network create mynet

I have created a volume named "demo-vol" externally by docker command.

docker volume create demo-vol

Inside the container, I have created a sample mongo collection. When I do docker-compose up I'm able to see the container running but I'm not able to find the mongo data in that specified volume.

Below is my docker-compose.yaml file

version: '3'
services:
  mongo:
    image: mongo
    container_name: mymongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - "/demo-vol:/data/db"
    networks:
      - mynet
    ports:
      - 27017:27017
  mongoexpress:
    image: mongo-express
    container_name: mymongoexpress
    ports:
      - 8081:8081
    volumes:
      - "/demo-vol:/data/db"
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
    depends_on:
      - mongo
    networks:
      - mynet
volumes:
  demo-vol:
    external: true
networks:
  mynet:
    external: true

What I need is: Even after deleting the container, I want my data to be persistent.

How to do that and please explain. Where i'm going wrong?

Note:I'm a beginner to Docker concepts.

Thanks in advance.

You can to use local driver for volume

volumes:
  demo-vol:
    driver: local

and try to remove slash

volumes:
  - demo-vol:/data/db

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM