如何通过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
相关产品推荐
相关产品推荐

