Docker – PHP cURL by container name in the same network doesn’t work

Long story short, I have dockerized the following (running on the same docker network)

  • PHP app
  • MySQL
  • Spring Boot app

Link to docker-compose file

Problem

Using PHP cURL by providing the container name doesn’t work, but it works if I use the IPv4 address instead.

What works: http://172.19.0.3:8080/api/test/ (IPv4 from the docker network)

What doesn’t:

  • http://spring:8080/api/test/
  • http://spring/api/test/
  • spring:8080/api/test/
  • spring/api/test/

Expected

To work by using the container name

Made sure…

  • They are running on the same docker network
  • I spelled correctly the container name in the curl
  • I provided the correct port

cURL code sample

$curl = curl_init("http://spring_api:8080/api/test/");

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/json'
));

$response = json_decode(curl_exec($curl), true);
curl_close($curl);

return $response;

Source: StackOverflow