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.
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:
In this configuration, both services are attached to a custom network app-network, which ensures they can communicate.
2. Service Naming and Links
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:
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:
Additional Considerations
- Environment Variables: Utilize environment variables to dynamically set Kafka broker list in your
php-rdkafkaconfiguration. - 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:
| Issue | Potential Cause | Resolution |
| Network Misconfiguration | Containers on different networks | Ensure all containers are on the same Docker network. |
| Incorrect Service Naming | Kafka service name mismatch | Match service names in docker-compose.yml and PHP code. |
| Port Not Exposed/Blocked | Kafka port 9092 not exposed or blocked | Correctly expose and map ports in Kafka service definition. |
| Firewall Rules | Firewall blocking connections on Kafka ports | Adjust 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
- Failed to start rabbitmq-management plugin on Windows
- Failing to write offset data to zookeeper in kafka-storm
- Fastest way to scan for bit pattern in a stream of bits
- Fault-tolerant queue-worker architecture in Kafka?
- Fargate with Docker compose Links
- Fetching AWS instance metadata from within Docker container?
- Failed to retrieve Ignite pods IP addresses
- Failed to run install_fedora_deps when starting up local kubernetes cluster

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.