筆記目錄

Skip to content

使用 Docker Compose 安裝 Elasticsearch 與 Kibana

TLDR

  • 部署 Elasticsearch 與 Kibana 時,務必將資料目錄權限設定為 UID 1000:1000,否則容器將無法啟動。
  • 建議不要將 logs 目錄掛載為 Volume,以避免升級或遷移時因權限衝突導致啟動失敗。
  • 使用 discovery.type=single-node 可快速建立開發環境。
  • 透過 setup 服務容器,可自動化處理 kibana_system 使用者的密碼設定。
  • 記憶體不足時,應調整 .env 中的 MEM_LIMIT 與 JVM Heap 設定。

環境設定與權限管理

在部署 Elasticsearch 容器時,最常見的問題是資料目錄權限不足,導致容器無法寫入資料而啟動失敗。

建立資料目錄

什麼情況下會遇到這個問題:在 Linux 主機上掛載 Volume 到容器內時。

執行以下指令建立目錄並賦予正確權限(1000 為 Elasticsearch 容器內預設的使用者 ID):

bash
# 建立資料目錄
mkdir -p volumes/elasticsearch/data

# 設定權限
sudo chown -R 1000:1000 volumes

TIP

  • 建議不要掛載 logs 目錄,因為在升級或遷移 Elasticsearch 時,log 檔案的權限問題極易造成啟動失敗。
  • Kibana 本身不儲存重要資料,無需建立 data 的 Volume。

設定檔案範例

環境變數設定 (.env)

透過 .env 檔案集中管理設定,方便後續維護。

env
# Password for the 'elastic' user (至少 6 個字元)
ELASTIC_PASSWORD=YourPassword

# Password for the 'kibana_system' user (至少 6 個字元)
KIBANA_PASSWORD=Wing1205

# Version of Elastic products
STACK_VERSION=9.1.4

# Set the cluster name
CLUSTER_NAME=docker-cluster

# Set to 'basic' or 'trial' to automatically start the 30-day trial
LICENSE=basic

# Port to expose Elasticsearch HTTP API to the host
ES_PORT=9200

# Port to expose Kibana to the host
KIBANA_PORT=5601

# Increase or decrease based on the available host memory (in bytes)
# 1GB = 1073741824 bytes
MEM_LIMIT=2147483648

Docker Compose 設定

建立 docker-compose.yml 檔案,整合 Elasticsearch、Kibana 與自動化設定服務。

yaml
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    container_name: elasticsearch
    restart: always
    environment:
      - node.name=elasticsearch
      - cluster.name=${CLUSTER_NAME}
      - discovery.type=single-node
      - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
      - bootstrap.memory_lock=true
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=false
      - xpack.security.transport.ssl.enabled=false
      - xpack.license.self_generated.type=${LICENSE}
      - ES_JAVA_OPTS=-Xms512m -Xmx512m -Xlog:gc:stdout:time,level,tags
    volumes:
      - ./volumes/elasticsearch/data:/usr/share/elasticsearch/data
    ports:
      - ${ES_PORT}:9200
    mem_limit: ${MEM_LIMIT}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    healthcheck:
      test: ["CMD-SHELL", "curl -s -u elastic:${ELASTIC_PASSWORD} http://localhost:9200/_cluster/health | grep -q 'yellow\\|green'"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

  kibana:
    depends_on:
      elasticsearch:
        condition: service_healthy
    image: docker.elastic.co/kibana/kibana:${STACK_VERSION}
    container_name: kibana
    restart: always
    environment:
      - SERVER_NAME=kibana
      - ELASTICSEARCH_HOSTS=http://elasticsearch:9200
      - ELASTICSEARCH_USERNAME=kibana_system
      - ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD}
    ports:
      - ${KIBANA_PORT}:5601
    mem_limit: ${MEM_LIMIT}
    healthcheck:
      test: ["CMD-SHELL", "curl -s -I http://localhost:5601 | grep -q 'HTTP/1.1 302 Found'"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s

  setup:
    image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
    container_name: elasticsearch-setup
    user: "0"
    command: >
      bash -c '
        echo "等待 Elasticsearch 啟動...";
        until curl -s -u elastic:${ELASTIC_PASSWORD} http://elasticsearch:9200/_cluster/health?pretty | grep -q "yellow\|green"; do
          echo "Elasticsearch 尚未就緒,等待中...";
          sleep 10;
        done;
        echo "設定 kibana_system 使用者密碼...";
        curl -s -X POST -u elastic:${ELASTIC_PASSWORD} -H "Content-Type: application/json" \
          http://elasticsearch:9200/_security/user/kibana_system/_password \
          -d "{\"password\":\"${KIBANA_PASSWORD}\"}";
        echo "初始化完成!";
      '
    depends_on:
      elasticsearch:
        condition: service_healthy

常見問題排除

記憶體不足

什麼情況下會遇到這個問題:當主機記憶體資源受限,導致 Elasticsearch 容器無法啟動或頻繁重啟。

  • 建議調整 .env 中的 MEM_LIMIT
  • 調整 ES_JAVA_OPTS 中的 JVM Heap 大小,建議設定為主機記憶體的 50% 左右。

權限問題

什麼情況下會遇到這個問題:容器啟動後顯示 Permission Denied 錯誤。

  • 請再次確認 volumes/elasticsearch/data 的擁有者為 UID 1000:1000。
  • 若仍無法解決,可嘗試執行 sudo chmod -R 777 volumes/elasticsearch/data 進行測試(僅限開發環境使用)。

異動歷程

  • 2025-09-24 初版文件建立。
  • 2025-11-04 修正 YAML 檔遺漏 restart 設定導致重開機會自動啟動之問題。