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

Docker中如何创建存储于挂载卷的Solr分布式集合

Fixing Solr Cloud Core Directory Placement in Docker Compose

Hey there! Let's tackle this Solr Cloud volume mounting issue you're facing. I see you've got a solid foundation with 3 ZooKeeper nodes and 4 Solr instances, but the collection cores aren't landing in your mounted host directory as expected. Let's break down what's going wrong and walk through the fixes step by step.

First, Fix Mounting & Configuration Inconsistencies

Looking at your docker-compose.yml, I spotted a few small issues that might be throwing things off:

  • Mismatched Volume Mounts: Your solr1 container mounts /root/solr/mycores to /opt/solr/server/solr/mycores, while the other Solr nodes mount /root/solr/cores to the same container path. Let's standardize this across all nodes to avoid confusion.
  • Incorrect ZooKeeper Address: In solr1's startup command, you have zookeeper2:2183 – that should be zookeeper3:2183.
  • Missing Port Specification: For solr2, solr3, and solr4, you mapped host ports 8984-8986, but didn't tell Solr to use those ports in the startup command. This will cause port conflicts inside the containers.
  • Unnecessary Swarm Config: The deploy section is for Docker Swarm mode, which you probably don't need for a single-node setup. Let's remove those to keep things clean.

Here's a corrected snippet of your docker-compose.yml with these fixes applied (full version at the end):

solr1:
  image: solr:latest
  environment:
    - JVM_OPTS=-Xmx12g -Xms12g -XX:MaxPermSize=1024m
  ports:
    - "8983:8983"
  restart: always
  container_name: solr1
  volumes:
    - /root/solr/cores:/opt/solr/server/solr/cores  # Standardized mount
  links:
    - zookeeper1
    - zookeeper2
    - zookeeper3
  command: bash -c '/opt/solr/bin/solr start -f -z zookeeper1:2181,zookeeper2:2182,zookeeper3:2183 -m 1g'

solr2:
  image: solr:latest
  ports:
    - "8984:8984"
  restart: always
  container_name: solr2
  volumes:
    - /root/solr/cores:/opt/solr/server/solr/cores
  links:
    - zookeeper1
    - zookeeper2
    - zookeeper3
  command: bash -c '/opt/solr/bin/solr start -f -z zookeeper1:2181,zookeeper2:2182,zookeeper3:2183 -m 1g -p 8984'  # Added -p for port

Set Correct Permissions on Host Directory

Solr runs as the solr user inside the container (UID 8983). Make sure your host directory has the right permissions so Solr can write to it:

chown -R 8983:8983 /root/solr/cores

Configure Solr to Use the Mounted Directory for Cores

The core issue here is that Solr Cloud defaults to storing core data in the default Solr home directory (/opt/solr/server/solr). To redirect this to your mounted volume, you need to define a custom dataDir in your Solr configuration and upload that config to ZooKeeper.

Step 1: Prepare the Custom Configuration

  1. Copy the default Solr configset from one of your containers to the host:
    docker cp solr1:/opt/solr/server/solr/configsets/_default /root/solr/configsets
    
  2. Open /root/solr/configsets/_default/conf/solrconfig.xml and modify the <dataDir> tag to point to your mounted directory:
    <dataDir>${solr.data.dir:cores/${solr.core.name}}</dataDir>
    
    This tells Solr to store each core's data in a subdirectory of your mounted cores folder, named after the core itself.

Step 2: Upload the Config to ZooKeeper

Run this command from your host to push the custom config to your ZooKeeper cluster:

docker exec -it solr1 /opt/solr/bin/solr zk upconfig -n custom_solr_config -d /root/solr/configsets/_default/conf -z zookeeper1:2181,zookeeper2:2182,zookeeper3:2183

Step 3: Create the Collection with the Custom Config

Now when you create your collection, reference the custom config you just uploaded:

docker exec -it solr1 /opt/solr/bin/solr create_collection -c publications -s 4 -rf 2 -p 8983 -n custom_solr_config

Alternative: Quick Command-Line Override

If you want a faster (but less persistent) fix, you can specify the dataDir directly in the create_collection command using the -D flag:

docker exec -it solr1 /opt/solr/bin/solr create_collection -c publications -s 4 -rf 2 -p 8983 -Dsolr.data.dir=/opt/solr/server/solr/cores/${solr.core.name}

Note: This won't save the dataDir setting to ZooKeeper, so if you recreate the collection later, you'll need to re-add the flag. Using the custom configset is the better long-term solution.

Full Corrected docker-compose.yml

version: "3.1"
services:
  solr1:
    image: solr:latest
    environment:
      - JVM_OPTS=-Xmx12g -Xms12g -XX:MaxPermSize=1024m
    ports:
      - "8983:8983"
    restart: always
    container_name: solr1
    volumes:
      - /root/solr/cores:/opt/solr/server/solr/cores
      - /root/solr/configsets:/opt/solr/configsets
    links:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    command: bash -c '/opt/solr/bin/solr start -f -z zookeeper1:2181,zookeeper2:2182,zookeeper3:2183 -m 1g'

  solr2:
    image: solr:latest
    ports:
      - "8984:8984"
    restart: always
    container_name: solr2
    volumes:
      - /root/solr/cores:/opt/solr/server/solr/cores
    links:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    command: bash -c '/opt/solr/bin/solr start -f -z zookeeper1:2181,zookeeper2:2182,zookeeper3:2183 -m 1g -p 8984'

  solr3:
    image: solr:latest
    ports:
      - "8985:8985"
    restart: always
    container_name: solr3
    volumes:
      - /root/solr/cores:/opt/solr/server/solr/cores
    links:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    command: bash -c '/opt/solr/bin/solr start -f -z zookeeper1:2181,zookeeper2:2182,zookeeper3:2183 -m 1g -p 8985'

  solr4:
    image: solr:latest
    ports:
      - "8986:8986"
    restart: always
    container_name: solr4
    volumes:
      - /root/solr/cores:/opt/solr/server/solr/cores
    links:
      - zookeeper1
      - zookeeper2
      - zookeeper3
    command: bash -c '/opt/solr/bin/solr start -f -z zookeeper1:2181,zookeeper2:2182,zookeeper3:2183 -m 1g -p 8986'

  zookeeper1:
    image: jplock/zookeeper:latest
    container_name: zookeeper1
    ports:
      - "2181:2181"
      - "2888:2888"
      - "3888:3888"
    restart: always

  zookeeper2:
    image: jplock/zookeeper:latest
    container_name: zookeeper2
    ports:
      - "2182:2182"
      - "2889:2889"
      - "3889:3889"
    restart: always

  zookeeper3:
    image: jplock/zookeeper:latest
    container_name: zookeeper3
    ports:
      - "2183:2183"
      - "2890:2890"
      - "3890:3890"
    restart: always

After making these changes, restart your Docker Compose stack, run the config upload and collection creation commands, and your core data should now be stored in /root/solr/cores on your host.

内容的提问来源于stack exchange,提问作者Szymon Roziewski

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:41:52