1- using Aspire . Hosting . Lifecycle ;
21using HealthChecks . Elasticsearch ;
32using Microsoft . Extensions . DependencyInjection ;
43using Microsoft . Extensions . Diagnostics . HealthChecks ;
@@ -11,7 +10,6 @@ namespace Aspire.Hosting;
1110public static class ElasticsearchBuilderExtensions
1211{
1312 private const int ElasticsearchPort = 9200 ;
14- private const int ElasticsearchInternalPort = 9300 ;
1513 private const int KibanaPort = 5601 ;
1614
1715 /// <summary>
@@ -80,15 +78,23 @@ public static IResourceBuilder<ElasticsearchResource> WithKibana(this IResourceB
8078 {
8179 containerName ??= $ "{ builder . Resource . Name } -kibana";
8280
83- builder . ApplicationBuilder . Services . TryAddEventingSubscriber < KibanaConfigWriterHook > ( ) ;
84-
81+ var elasticsearch = builder . Resource ;
8582 var resource = new KibanaResource ( containerName ) ;
8683 var resourceBuilder = builder . ApplicationBuilder . AddResource ( resource )
8784 . WithImage ( ElasticsearchContainerImageTags . KibanaImage , ElasticsearchContainerImageTags . Tag )
8885 . WithImageRegistry ( ElasticsearchContainerImageTags . KibanaRegistry )
8986 . WithHttpEndpoint ( targetPort : KibanaPort , name : containerName )
9087 . WithUrlForEndpoint ( containerName , u => u . DisplayText = "Kibana" )
9188 . WithEnvironment ( "xpack.security.enabled" , "false" )
89+ . WithEnvironment ( ctx =>
90+ {
91+ // Use ReferenceExpression for proper container-to-container networking
92+ // The Host property will resolve to the container name when used this way
93+ var endpoint = elasticsearch . PrimaryEndpoint ;
94+ var hostsExpr = ReferenceExpression . Create ( $ "http://{ endpoint . Property ( EndpointProperty . Host ) } :{ endpoint . Property ( EndpointProperty . Port ) } ") ;
95+ ctx . EnvironmentVariables [ "ELASTICSEARCH_HOSTS" ] = hostsExpr ;
96+ } )
97+ . WaitFor ( builder )
9298 . ExcludeFromManifest ( ) ;
9399
94100 configureContainer ? . Invoke ( resourceBuilder ) ;
0 commit comments