Docker
PHP-rdkafka
Kafka
Network Errors
Troubleshooting

Failed to resolve 'kafka9092' Name or service not known - docker / php-rdkafka

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

When using Docker to set up applications that require communication between different services like Apache Kafka and a PHP application using php-rdkafka, you may encounter issues in network configurations and service resolutions. A common error in this scenario is Failed to resolve 'kafka:9092': Name or service not known. This error typically indicates that the PHP application running in one Docker container is unable to find or connect to the Kafka service running in another Docker container or network because the Kafka broker address cannot be resolved.

Understanding the Error

The error message "Failed to resolve 'kafka:9092': Name or service not known" is essentially a network resolution failure. This means that the service name 'kafka' does not resolve to an IP address in the network settings that your PHP container is running in. In Docker environments, this issue often stems from:

  • Docker Networking: Misconfiguration in Docker networking whereby containers are unable to communicate across Docker networks.
  • Service Naming: Incorrect naming or non-existent Kafka service within the Docker compose file or the Docker network where your services are supposed to interact.
  • Port Availability: The specified port (9092 in this case, which is the default for Kafka) may not be correctly exposed or could be blocked by firewall rules.

Technical Resolutions

Here’s how you might approach solving this issue:

1. Docker Network Configuration

Ensure that all relevant services (PHP application and Kafka) are on the same Docker network. You can define this in your docker-compose.yml file:

yaml
1version: '3'
2services:
3  kafka:
4    image: confluentinc/cp-kafka
5    networks:
6      - app-network
7  php-app:
8    build: .
9    depends_on:
10      - kafka
11    networks:
12      - app-network
13
14networks:
15  app-network:
16    driver: bridge

In this configuration, both services are attached to a custom network app-network, which ensures they can communicate.

Make sure that the service name you use in your PHP application matches the name defined in your Docker configuration. If you referred to Kafka as kafka in your docker-compose.yml, you must use kafka:9092 in your PHP application. Additionally, using links can make these services discover each other by name:

yaml
1php-app:
2  build: .
3  depends_on:
4    - kafka
5  links:
6    - kafka

3. Port Exposure and Mapping

Ensure that the port needed by Kafka (9092) is exposed and mapped correctly if access from the outside network is required:

yaml
1services:
2  kafka:
3    ports:
4      - "9092:9092"

Additional Considerations

  • Environment Variables: Utilize environment variables to dynamically set Kafka broker list in your php-rdkafka configuration.
  • Error Handling in Code: Implement robust error handling in your PHP application to manage connection failures gracefully.

Summary Table

Here is a summary of potential causes and resolutions for the "Failed to resolve 'kafka:9092'" error message:

IssuePotential CauseResolution
Network MisconfigurationContainers on different networksEnsure all containers are on the same Docker network.
Incorrect Service NamingKafka service name mismatchMatch service names in docker-compose.yml and PHP code.
Port Not Exposed/BlockedKafka port 9092 not exposed or blockedCorrectly expose and map ports in Kafka service definition.
Firewall RulesFirewall blocking connections on Kafka portsAdjust firewall rules to allow traffic on port 9092.

By addressing these configurations and ensuring proper setup of both networking and service definitions in Docker, you can mitigate the "Failed to resolve 'kafka:9092'" error and establish successful communication between your PHP application and Kafka services.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design