Skip to content
Advertisement

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name does not resolve

I am running a php:7-fpm-alpine Docker container and have a Symfony command that calls the following function (simplified a bit for display here):

protected function getNextEndpointAsObj()
{
    $asObj = json_decode(file_get_contents('https://www.muckrock.com/api_v1/jurisdiction/?format=json&page=1'));

    return $asObj;
}

… and when I run the command from within my Docker container, I get this error message:

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name does not resolve

… but when I run a short test.php script …

<?php

echo file_get_contents('https://www.muckrock.com/api_v1/jurisdiction/?format=json&page=1');

?>

… from either inside or outside the container, I get correct output from the API I’m testing against.

What’s going on here?

Advertisement

Answer

Did you run a short test.php inside the container too?

It is not a Symfony or PHP problem. In short container’s network cannot resolve the IP address of the domain muckrock.com. Look for DNS settings for your container network. Tell more about your infrastructure. It is Kubernetes or something another?

For raw docker container running you can use something like this:

docker run -it --dns=8.8.8.8 php:7-fpm-alpine /bin/sh

Or for docker-compose something like this (or for version 3 as more actual)

version: 2
services:
 application:
  dns:
   - 8.8.8.8
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement