Docker php : file_get_contents(): php_network_getaddresses: getaddrinfo failed

  docker, laravel, php

I’ve a docker php:8.0-apache where my laravel website must call an external url with the php file_get_contents() function :

$content = file_get_contents("http://external-url.com");

But it gets this error :

ErrorException file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known

In my docker php container a curl http://external-url.com is working great.

My docker-compose.yml

version: '3.5'

services:
  web:
    build: ./docker
    restart: always
    container_name: $DOCKER_NAME-web
    volumes:
      - ./:/var/www/html
    networks:
      - shared
      - default
networks:
  shared:
    external:
      name: shared

My Dockerfile

FROM php:8.0-apache

# Use the default develoment configuration
#RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

RUN apt-get update -y && apt-get install -y libmcrypt-dev libldap2-dev libpng-dev openssl libzip-dev cron rsyslog 
    libxml2-dev libjpeg62-turbo-dev libfreetype6-dev vim git libonig-dev
RUN docker-php-ext-install pdo mbstring mysqli pdo_mysql gd soap bcmath
RUN docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu/ && docker-php-ext-install ldap zip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
#RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
#RUN docker-php-ext-install -j$(nproc) gd

#RUN pecl install mcrypt-1.0.2 && docker-php-ext-enable mcrypt
WORKDIR /var/www/html

#set our application folder as an environment variable
ENV APP_HOME=/var/www/html
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public

  
# enable apache module rewrite
RUN a2enmod rewrite && service apache2 restart

COPY ./script.sh /var/www/script.sh
RUN chmod 0700 /var/www/script.sh

CMD /var/www/script.sh
EXPOSE 80

Thanks for any suggestion

Source: Laravel

Leave a Reply