如何在Docker启动时自动在Django中重建Elasticsearch的搜索索引?

xpcnnkqh  于 2023-05-22  发布在  ElasticSearch
关注(0)|答案(2)|浏览(206)

这是我第一次在Docker上使用Elasticsearch和Django,我意识到每次我启动docker-compose时,我都必须发出./manage.py search_index --rebuild来索引Elasticsearch中的所有文档。我一直在尝试自动执行此操作,但似乎仍然不起作用。我的web服务命令看起来像这样:

web:
    build: .
    command: bash -c "python manage.py runserver 0.0.0.0:8000 && sleep 60 && python manage.py search_index --rebuild"
    
    ...

我添加了sleep 60,这样它就可以等待Elasticsearch启动,然后再发出rebuild命令。即使这样,也不会发生任何事情,除非我明确地手动发出它。

mi7gmzs6

mi7gmzs61#

&&链中的第二个和第三个命令只会在第一个命令退出后运行,这意味着在Web服务器停止后。您必须为此任务添加另一个服务。

rebuild_index:
    build: .
    command: bash -c "sleep 60 && python manage.py search_index --rebuild"
    restart: on-failure

更好的是,不用睡觉,你可以depend_on Elasticsearch服务。我不是100%肯定这将工作,请张贴您的结果,如果你尝试它。

rebuild_index:
    build: .
    command: bash -c "python manage.py search_index --rebuild"
    restart: on-failure
    depends_on:
        elasticsearch:
            condition: service_started
b4lqfgs4

b4lqfgs42#

不工作

所以,使用@Alexandr Tatarinov回答我能够实现自动索引重建。Im docker-compose.yml文件:

version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    container_name: pgdb
  es:
    image: elasticsearch:7.9.2
    environment:
      - discovery.type=single-node
    ports:
      - "9200:9200"
    container_name: es
    healthcheck:
      test: "exit 0"
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres   
    container_name: drf
    depends_on:
      - db
      - es
    healthcheck:
      test: "exit 0"
  rebuild_index:
    build: .
    container_name: rebuild_index
    command: python manage.py search_index --rebuild -f
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
        web:
          condition: service_healthy
        es:
          condition: service_healthy

使用healthcheck,我们可以定义容器是否正在运行,特别是我们需要检查web容器(我的Django应用程序)和es容器(我的elasticsearch)
rebuild_index容器上的depends_on指令将等待webes容器运行。则它将执行该命令。-f将跳转提示交互,而我们必须使用environment配置使命令工作所需的数据库信息。

编辑-工作版本

由于某种原因,容器rebuild_index启动并运行is命令,没有错误,但没有正确生成索引...我认为原因是因为命令需要在同一个容器上启动。
一个更干净的工作版本:

version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    container_name: pgdb
    healthcheck:
      test: "exit 0"
  es:
    image: elasticsearch:7.9.2
    environment:
      - discovery.type=single-node
    ports:
      - "9200:9200"
    container_name: es
    healthcheck:
      test: "exit 0"
  web:
    build: .
    command: sh -c "python manage.py search_index --rebuild -f && python manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres   
    container_name: drf
    depends_on:
        db:
          condition: service_healthy
        es:
          condition: service_healthy

相关问题