加载Mongo中的.bson集合的方法在docker-compose中是怎样的?

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

How to load the .bson collection for mongo in docker-compose

问题

以下是您要翻译的内容:

以下用于Docker和docker-compose.yml的代码无法正常工作,无法将sample_collection.bson加载到docker中。

Dockerfile

  1. FROM python:3.8
  2. WORKDIR /salary_data_ggregation
  3. COPY requirements.txt .
  4. RUN pip install -r requirements.txt
  5. COPY . .
  6. CMD ["python", "run.py"]

docker-compose.yml

  1. services:
  2. bot:
  3. build: .
  4. depends_on:
  5. - mongo
  6. mongoimport:
  7. image: mongo
  8. container_name: mongo_import
  9. volumes:
  10. - ./bot/database/static_collection/sample_collection.bson:/data/sample_collection.bson
  11. command: mongoimport --host mongodb --db aggregation --collection sample_collection --file /data/sample_collection.bson
  12. mongo:
  13. image: mongo
  14. container_name: mongo_db
  15. ports:
  16. - 27017:27017
  17. depends_on:
  18. - mongoimport

当我进入MongoDB Compass时,我期望看到一个aggregation数据库和一个sample_collection。

英文:

The following code for Docker and docker-compose.yml does not work properly, it fails to load sample_collection.bson into docker

Dockerfile

  1. FROM python:3.8
  2. WORKDIR /salary_data_ggregation
  3. COPY requirements.txt .
  4. RUN pip install -r requirements.txt
  5. COPY . .
  6. CMD ["python", "run.py"]

docker-compose.yml

  1. services:
  2. bot:
  3. build: .
  4. depends_on:
  5. - mongo
  6. mongoimport:
  7. image: mongo
  8. container_name: mongo_import
  9. volumes:
  10. - ./bot/database/static_collection/sample_collection.bson:/data/sample_collection.bson
  11. command: mongoimport --host mongodb --db aggregation --collection sample_collection --file /data/sample_collection.bson
  12. mongo:
  13. image: mongo
  14. container_name: mongo_db
  15. ports:
  16. - 27017:27017
  17. depends_on:
  18. - mongoimport

I expected to see an aggregation database and a sample_collection when I enter MongoDB Compass

答案1

得分: 0

如果我理解问题正确的话,depends_on 应该反过来 - mongoimport 服务应该在尝试导入之前等待Mongo启动。此外,主机应该是 --host mongo 而不是 mongodb

我还建议查看在Mongo中以Docker推荐的方式预填充数据,通过在 /docker-entrypoint-initdb.d 下挂载一个JS脚本,Mongo会执行它。请参阅docker hub的Initializing a fresh instance

英文:

If I understand the question correctly, the depends_on should be the other way around - the mongoimport service should wait for mongo to start before trying to import. Moreover, the host should be --host mongo instead of mongodb.

I would also recommend having a look in the recommended docker way of prepopulating data in mongo, by mounting a JS script under /docker-entrypoint-initdb.d, which mongo will execute. See the Initializing a fresh instance
in docker hub.

huangapple
  • 本文由 发表于 2023年4月16日 23:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028744.html
匿名

发表评论

匿名网友

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

确定