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

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

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

问题

以下是您要翻译的内容:

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

Dockerfile

FROM python:3.8

WORKDIR /salary_data_ggregation

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "run.py"]

docker-compose.yml

services:
  bot:
    build: .
    depends_on:
      - mongo

  mongoimport:
    image: mongo
    container_name: mongo_import
    volumes:
      - ./bot/database/static_collection/sample_collection.bson:/data/sample_collection.bson
    command: mongoimport --host mongodb --db aggregation --collection sample_collection --file /data/sample_collection.bson

  mongo:
    image: mongo
    container_name: mongo_db
    ports:
      - 27017:27017
    depends_on:
      - 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

FROM python:3.8

WORKDIR /salary_data_ggregation

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "run.py"]

docker-compose.yml

services:
  bot:
    build: .
    depends_on:
      - mongo

  mongoimport:
    image: mongo
    container_name: mongo_import
    volumes:
      - ./bot/database/static_collection/sample_collection.bson:/data/sample_collection.bson
    command: mongoimport --host mongodb --db aggregation --collection sample_collection --file /data/sample_collection.bson

  mongo:
    image: mongo
    container_name: mongo_db
    ports:
      - 27017:27017
    depends_on:
      - 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:

确定