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

如何通过Testcontainers为Solr容器传递-Dsolr.disable.allowUrls=true配置?

问题

本地环境中通过以下Docker命令启动Solr容器时,可正常传递-Dsolr.disable.allowUrls=true配置:

docker run -p 8983:8983 -v $PWD/:/var/solr/data -v $PWD/configsets:/opt/solr-9.1.1/server/solr/configsets/ -t solr -a "-Dsolr.disable.allowUrls=true"    

但使用Testcontainers部署Solr测试容器时,尝试多种方式均无法成功传递该配置:

  • 尝试withCommand的多种写法:
    // solrContainer.withCommand("/bin/sh", "-c", "#(nop) ", "CMD [\\\"solr-foreground\\\"]" , "-a", "-Dsolr.disable.allowUrls=true" );
    // solrContainer.withCommand("/bin/solr",  "-Dsolr.disable.allowUrls=true" );
    solrContainer.withCommand("-Dsolr.disable.allowUrls=true" );
    // solrContainer.withCreateContainerCmdModifier(it -> {
    //    System.out.println(it.getCmd());
    //    System.out.println(it.getEntrypoint());
    // });
    
  • 尝试设置环境变量:
    solrContainer.withEnv("SOLR_OPTS", "-Dsolr.disable.allowUrls=true");
    

设置环境变量时出现报错,请问如何通过Testcontainers实现该配置的传递?

解决方案

方法一:正确追加SOLR_OPTS环境变量

直接覆盖SOLR_OPTS会丢失镜像默认的必要配置,导致报错。正确方式是在原有配置基础上追加参数:

solrContainer.withEnv("SOLR_OPTS", "${SOLR_OPTS} -Dsolr.disable.allowUrls=true");

这样既保留镜像原有的JVM参数,又添加了自定义配置。

方法二:调整启动命令

Solr官方镜像默认以solr-foreground命令启动,你可以通过withCommand直接传递参数:

solrContainer.withCommand("solr-foreground", "-a", "-Dsolr.disable.allowUrls=true");

或者用withCreateContainerCmdModifier更灵活地修改命令:

solrContainer.withCreateContainerCmdModifier(cmd -> {
    cmd.withCmd("solr-foreground", "-a", "-Dsolr.disable.allowUrls=true");
});

方法三:使用Testcontainers Solr模块专属方法(推荐)

若使用Testcontainers的Solr专用容器类(而非通用Docker容器),可以直接用withSolrOptions方法添加参数:

SolrContainer solrContainer = new SolrContainer("solr:9.1.1")
    .withSolrOptions("-Dsolr.disable.allowUrls=true");

该方法会自动适配Solr容器的启动逻辑,无需手动处理命令结构。


内容的提问来源于stack exchange,提问作者Usman Sajid

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 14:12:44