It seems like you're trying to access a service within the same Docker network using a hostname that might not be recognized by the container's DNS resolver. When you're using Laravel Sail, services are typically accessible to each other by their service names as defined in the docker-compose.yml file.
Here's a solution that might help you:
-
Make sure that the service name in your
docker-compose.ymlfile is correct. For example, if your Laravel application service is namedlaravel.test, you should use that name to access it from another service within the same Docker network. -
Instead of using
gethostbyname('laravel.app'), directly use the service name as the hostname in your cURL request. For example:
$url = 'http://laravel.test' . self::VOIP_MONITOR_API_URL;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_HEADER, false);
$response = curl_exec($curl);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
echo "Error: " . $error_msg;
} else {
echo "Response: " . $response;
}
curl_close($curl);
-
If you're using a non-standard port, make sure to include it in the URL.
-
Ensure that your application's firewall or security groups (if any) allow for internal communication on the port you're trying to access.
-
If you're still facing issues, you can try to troubleshoot by executing a cURL command directly from within the container's shell to see if it's a PHP-specific issue or a network-related issue. You can do this by running
sail shellto get into the container's command line and then using cURL from there.
Remember that when you're working with Docker containers, you should use the internal network and service names for communication between services, rather than external DNS names or IPs.