Skip to content

Commit 12b7701

Browse files
Copilotdevstress
andcommitted
Fix Kafka connectivity: Use DNS name instead of container IP for Aspire network
ROOT CAUSE: Flink jobs were using Kafka container IP (e.g., 172.18.0.9:9093) which doesn't work in Aspire-managed Docker networks. Aspire creates networks with DNS support where containers communicate using service names. SOLUTION: Changed GetKafkaContainerIpAsync() to return "kafka:9093" instead of discovering container IP. This allows Flink TaskManager to connect to Kafka using DNS resolution within the Aspire Docker network. This fixes Exercise1 and Exercise2 Kafka connectivity issues where jobs would submit but produce no output. Co-authored-by: devstress <30769729+devstress@users.noreply.github.com>
1 parent 82926bd commit 12b7701

File tree

1 file changed

+11
-33
lines changed

1 file changed

+11
-33
lines changed

LearningCourse/LearningCourse.Common/DockerInfrastructure.cs

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace LearningCourse.Common;
99
public static class DockerInfrastructure
1010
{
1111
/// <summary>
12-
/// Discovers the Kafka container IP address for Flink job configurations.
13-
/// Docker's default bridge network doesn't support DNS between containers,
14-
/// so we need to use the actual container IP address.
12+
/// Discovers the Kafka bootstrap servers for Flink job configurations.
13+
/// In Aspire-managed containers, uses DNS name (kafka:9093) for container-to-container communication.
14+
/// Aspire creates a Docker network with DNS support, so containers can use service names.
1515
/// </summary>
16-
/// <returns>Kafka container IP with port (e.g., "172.17.0.2:9093")</returns>
16+
/// <returns>Kafka bootstrap servers for Flink (e.g., "kafka:9093")</returns>
1717
public static async Task<string> GetKafkaContainerIpAsync()
1818
{
1919
try
@@ -26,39 +26,17 @@ public static async Task<string> GetKafkaContainerIpAsync()
2626
throw new InvalidOperationException("Kafka container not found");
2727
}
2828

29-
// Try Docker bridge network first
30-
var ipAddress = await RunDockerCommandAsync($"inspect {kafkaContainer} --format \"{{{{.NetworkSettings.Networks.bridge.IPAddress}}}}\"");
31-
var ip = ipAddress.Trim();
29+
Console.WriteLine($"✅ Kafka container found: {kafkaContainer}");
3230

33-
// If bridge network doesn't have IP, try podman network (for Podman runtime)
34-
if (string.IsNullOrWhiteSpace(ip) || ip == "<no value>")
35-
{
36-
Console.WriteLine($"🔍 Bridge network IP not found, trying podman network...");
37-
ipAddress = await RunDockerCommandAsync($"inspect {kafkaContainer} --format \"{{{{.NetworkSettings.Networks.podman.IPAddress}}}}\"");
38-
ip = ipAddress.Trim();
39-
}
40-
41-
if (string.IsNullOrWhiteSpace(ip) || ip == "<no value>")
42-
{
43-
// Fallback: Get the first available network IP
44-
Console.WriteLine($"🔍 Specific network not found, getting first available IP...");
45-
ipAddress = await RunDockerCommandAsync($"inspect {kafkaContainer} --format \"{{{{range .NetworkSettings.Networks}}}}{{{{.IPAddress}}}}{{{{end}}}}\"");
46-
ip = ipAddress.Trim();
47-
}
48-
49-
if (string.IsNullOrWhiteSpace(ip) || ip == "<no value>")
50-
{
51-
throw new InvalidOperationException($"Could not determine Kafka container IP from any network. Container: {kafkaContainer}");
52-
}
53-
54-
Console.WriteLine($"✅ Kafka container IP discovered: {ip}");
55-
56-
// Return IP with PLAINTEXT_INTERNAL port (9093)
57-
return $"{ip}:9093";
31+
// CRITICAL: In Aspire-managed Docker networks, containers support DNS resolution
32+
// Flink containers can access Kafka using the service name "kafka" instead of IP
33+
// This works because all Aspire containers are on the same Docker network
34+
// Port 9093 is used for container-to-container communication (PLAINTEXT_INTERNAL)
35+
return "kafka:9093";
5836
}
5937
catch (Exception ex)
6038
{
61-
throw new InvalidOperationException($"Failed to get Kafka container IP: {ex.Message}", ex);
39+
throw new InvalidOperationException($"Failed to get Kafka bootstrap servers: {ex.Message}", ex);
6240
}
6341
}
6442

0 commit comments

Comments
 (0)