You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Docker Compose启动Elasticsearch时自动创建索引

如何在Docker Compose启动Elasticsearch时自动创建products索引

下面提供两种可靠的实现方式,可根据需求选择:

方法一:在Elasticsearch容器内嵌入初始化脚本

  1. 创建初始化脚本文件 create_products_index.sh,内容如下:
#!/bin/bash

# 等待Elasticsearch服务就绪(单节点状态为yellow或green均正常)
until curl -s http://localhost:9200/_cluster/health | grep -q '"status":"green"' || curl -s http://localhost:9200/_cluster/health | grep -q '"status":"yellow"'; do
  echo "等待Elasticsearch启动..."
  sleep 5
done

# 发送请求创建products索引,可根据需求自定义settings和mappings
curl -X PUT http://localhost:9200/products -H "Content-Type: application/json" -d '{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "name": {"type": "text"},
      "price": {"type": "float"},
      "category": {"type": "keyword"}
    }
  }
}'

echo "products索引创建完成"
  1. 修改你的 docker-compose.yaml,挂载脚本并调整启动命令:
version: '3.1'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.0
    container_name: es
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
    deploy:
      resources:
        limits:
          memory: 2GB
    volumes:
      - ./create_products_index.sh:/usr/share/elasticsearch/create_products_index.sh
    command: >
      bash -c "chmod +x /usr/share/elasticsearch/create_products_index.sh &&
               /usr/share/elasticsearch/create_products_index.sh &
               /usr/local/bin/docker-entrypoint.sh eswrapper"

方法二:使用独立初始化服务(推荐)

这种方式无需修改Elasticsearch容器的核心启动逻辑,通过一个临时容器完成索引创建:

修改 docker-compose.yaml 如下:

version: '3.1'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.0
    container_name: es
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
    deploy:
      resources:
        limits:
          memory: 2GB
    healthcheck:
      test: ["CMD-SHELL", "curl -s http://localhost:9200/_cluster/health | grep -q '\"status\":\"green\"' || curl -s http://localhost:9200/_cluster/health | grep -q '\"status\":\"yellow\"'"]
      interval: 5s
      timeout: 10s
      retries: 10

  es-init:
    image: curlimages/curl:latest
    depends_on:
      elasticsearch:
        condition: service_healthy
    command: >
      curl -X PUT http://elasticsearch:9200/products -H "Content-Type: application/json" -d '{
        "settings": {
          "number_of_shards": 1,
          "number_of_replicas": 0
        },
        "mappings": {
          "properties": {
            "name": {"type": "text"},
            "price": {"type": "float"},
            "category": {"type": "keyword"}
          }
        }
      }'

说明

  • 方法二中的healthcheck用于检测Elasticsearch是否就绪,确保索引创建操作在服务可用后执行。
  • 两种方法中的索引settings和mappings可根据业务需求调整,若不需要自定义配置,可简化为curl -X PUT http://elasticsearch:9200/products。

内容的提问来源于stack exchange,提问作者mikolaj semeniuk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 04:05:35