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

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

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

问题

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

version: '3.9'

services:
  # MongoDB 服务
  mongo_db:
    container_name: db_container
    image: mongo:latest
    restart: always
    ports:
      - 2717:27017
    volumes:
      - /mnt/c/temp/mongo/db:/data/db

  # Node API 服务
  api:
    build: .
    ports:
      - 4001:4001
    environment:
      PORT: 4001
      MONGODB_URI: mongodb://db_container:27017
      DB_NAME: project-system
    depends_on:
      - mongo_db

volumes:
  mongo_db:

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

volumes:
  - /mnt/c/temp/mongo/db:/data/db

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

volumes:
  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:

version: '3.9'

services:
  #MongoDB Service
  mongo_db:
    container_name: db_container
    image: mongo:latest
    restart: always
    ports:
      - 2717:27017
    volumes: 
      - /mnt/c/temp/mongo/db:/data/db 

  #Node API Service
  api:
    build: .
    ports: 
     - 4001:4001
    environment:
      PORT: 4001
      MONGODB_URI: mongodb://db_container:27017
      DB_NAME: project-system
    depends_on:
      - mongo_db
  
volumes:
  mongo_db: 

As you can see in this row:

  volumes: 
      - /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:

volumes:
  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:

mongodump --uri 'mongodb://localhost:27017/yourdatabase' --archive=<your file> --gzip
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.

version: '3.9'

services:
  #MongoDB Service
  mongo_db:
    container_name: db_container
    image: mongo:latest
    restart: always
    ports:
      - 2717:27017
    volumes: 
      - C:/data/docker_mongo:/data/db 

  #Node API Service
  api:
    build: .
    ports: 
     - 4001:4001
    environment:
      PORT: 4001
      MONGODB_URI: mongodb://db_container:27017
      DB_NAME: project-system
    depends_on:
      - mongo_db
  
volumes:
  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:

mongodump --uri &#39;mongodb://localhost:27017/yourdatabase&#39; --archive=&lt;your file&gt; --gzip
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.

version: &#39;3.9&#39;

services:
  #MongoDB Service
  mongo_db:
    container_name: db_container
    image: mongo:latest
    restart: always
    ports:
      - 2717:27017
    volumes: 
      - C:/data/docker_mongo:/data/db 

  #Node API Service
  api:
    build: .
    ports: 
     - 4001:4001
    environment:
      PORT: 4001
      MONGODB_URI: mongodb://db_container:27017
      DB_NAME: project-system
    depends_on:
      - mongo_db
  
volumes:
  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:

确定