Yes, it’s possible to use docker-compose to mount your existing MongoDB database.

huangapple go评论109阅读模式
英文:

Is it possible to mount my existing Mongo DataBase with docker-compose?

问题

在经过几个月的开发后,我发现将我的MERN应用程序容器化是更好的选择。我成功创建了一个**.yaml文件,一切都正常工作,但问题是我已经有了大量已收集的数据。我想要能够将这些数据挂载到容器中,但我不知道如何操作。我阅读了很多资料,但在组合应用程序后,我的数据仍然没有出现。以下是我的docker-compose.yaml**文件的外观:

  1. version: '3.9'
  2. services:
  3. # MongoDB 服务
  4. mongo_db:
  5. container_name: db_container
  6. image: mongo:latest
  7. restart: always
  8. ports:
  9. - 2717:27017
  10. volumes:
  11. - /mnt/c/temp/mongo/db:/data/db
  12. # Node API 服务
  13. api:
  14. build: .
  15. ports:
  16. - 4001:4001
  17. environment:
  18. PORT: 4001
  19. MONGODB_URI: mongodb://db_container:27017
  20. DB_NAME: project-system
  21. depends_on:
  22. - mongo_db
  23. volumes:
  24. mongo_db:

正如您在以下行中所看到的:

  1. volumes:
  2. - /mnt/c/temp/mongo/db:/data/db

我试图指定来自我的C:\驱动器的路径,但这不起作用。我还尝试将相同的行放在:

  1. volumes:
  2. mongo_db:

(文件底部),但同样没有成功。基本上,我的现有数据库位于

C:\data\db

如何将其指定为MongoDB服务的源呢?

英文:

After some months of development I got to a point where it is better to dockerize my MERN application. I managed to create .yaml file and everything is working OK but the problem is that I already have big amount of data that is collected. I want to be able to mount this data to container but I don't know how to do it. Read a lot of stuff but still my data is not appearing after composing the applications. Here is how my docker-compose.yaml file looks-like:

  1. version: '3.9'
  2. services:
  3. #MongoDB Service
  4. mongo_db:
  5. container_name: db_container
  6. image: mongo:latest
  7. restart: always
  8. ports:
  9. - 2717:27017
  10. volumes:
  11. - /mnt/c/temp/mongo/db:/data/db
  12. #Node API Service
  13. api:
  14. build: .
  15. ports:
  16. - 4001:4001
  17. environment:
  18. PORT: 4001
  19. MONGODB_URI: mongodb://db_container:27017
  20. DB_NAME: project-system
  21. depends_on:
  22. - mongo_db
  23. volumes:
  24. mongo_db:

As you can see in this row:

  1. volumes:
  2. - /mnt/c/temp/mongo/db:/data/db

I am trying to point the path from my C:\ drive but this doesn't work. I also tried the same row in:

  1. volumes:
  2. mongo_db:

(the bottom of file) but again without success. Basically my existing DB is on

> C:\data\db

How can I point this to be the source of MongoDB service?

答案1

得分: 1

First, you need to create the dump from local MongoDB and copy those files to docker MongoDB. You can use these commands to create:

  1. mongodump --uri 'mongodb://localhost:27017/yourdatabase' --archive=<your file> --gzip
  2. mongorestore --uri 'mongodb://remotehost:27017/yourdatabase' --archive=<your file> --gzip

You should be able to access the docker from local host.

Note: Reference this answer if you don't get it correct.

You can do these changes on the path you are mounting to make data persistent. Create a new folder C:/data/docker_mongo to make data persistent.

  1. version: '3.9'
  2. services:
  3. #MongoDB Service
  4. mongo_db:
  5. container_name: db_container
  6. image: mongo:latest
  7. restart: always
  8. ports:
  9. - 2717:27017
  10. volumes:
  11. - C:/data/docker_mongo:/data/db
  12. #Node API Service
  13. api:
  14. build: .
  15. ports:
  16. - 4001:4001
  17. environment:
  18. PORT: 4001
  19. MONGODB_URI: mongodb://db_container:27017
  20. DB_NAME: project-system
  21. depends_on:
  22. - mongo_db
  23. volumes:
  24. mongo_db:
英文:

First, you need to create the dump from local MongoDB and copy those files to docker MongoDB. You can use these commands to create:

  1. mongodump --uri &#39;mongodb://localhost:27017/yourdatabase&#39; --archive=&lt;your file&gt; --gzip
  2. mongorestore --uri &#39;mongodb://remotehost:27017/yourdatabase&#39; --archive=&lt;your file&gt; --gzip

You should be able to access the docker from local host.

Note: Reference this answer if you don't get it correct.

You can do these changes on the path you are mounting to make data persistent. Create a new folder C:/data/docker_mongo to make data persistent.

  1. version: &#39;3.9&#39;
  2. services:
  3. #MongoDB Service
  4. mongo_db:
  5. container_name: db_container
  6. image: mongo:latest
  7. restart: always
  8. ports:
  9. - 2717:27017
  10. volumes:
  11. - C:/data/docker_mongo:/data/db
  12. #Node API Service
  13. api:
  14. build: .
  15. ports:
  16. - 4001:4001
  17. environment:
  18. PORT: 4001
  19. MONGODB_URI: mongodb://db_container:27017
  20. DB_NAME: project-system
  21. depends_on:
  22. - mongo_db
  23. volumes:
  24. mongo_db:

huangapple
  • 本文由 发表于 2023年1月4日 20:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75005199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定