phpdockerfilehttp-request2

docker php:7.1-apache don't load http_request2


as first thing a wish to tell that I'm a noob with docker.
I'm trying to use docker to have a virtualized Apache on my machine.
This is my dockerfile

FROM php:7.1-apache
RUN docker-php-ext-install mysqli
RUN pecl install xdebug-2.6.0
RUN echo "VetrinaECM localhost" >> /etc/apache2/apache2.conf
RUN pear install http_request2
RUN docker-php-ext-enable xdebug
COPY . /var/www/html/
RUN echo "xdebug.remote_enable=1" >> /usr/local/etc/php/php.ini

and this is my docker-compose.yml

version: '2'
services:
  webserver:
    image: phpstorm/php-71-apache-xdebug-26
    ports:
      - "80:80"
    volumes:
      - ./:/var/www/html
    environment:
      XDEBUG_CONFIG: remote_host=host.docker.internal

But when I try to import then php http_request2 library I get this error

Warning: require_once(HTTP/Request2.php): failed to open stream: No such file or directory in /var/www/html/dadilib/get_data.php on line 4

Where am I wrong? What did I not define in the files?

Thanks

Stefano G


Solution

  • You may need to get back to docker's basic concepts a little more in depth. Not sure where you learned from, but I would recommend "That devops guy" on youtube: https://www.youtube.com/watch?v=wyjNpxLRmLg&list=PLHq1uqvAteVvqQaaIAvfIWWTL_JmmXcfg

    More in the PHP ecosystem, have a look at https://serversforhackers.com/t/containers

    In your case, you need first to understand docker, then docker-compose. On the docker side, you have a concept of "image", which describes how to create a "container". Think of the image as a sort of recipe, and when you docker run this image, you get a meal (a container).

    In your case, you want to add pecl's http request library to phpstorm/php-71-apache-xdebug-26, therefore you need to execute some more instructions on top of the existing image. This is where the Dockerfile, docker build and FROM are in play.

    FROM phpstorm/php-71-apache-xdebug-26
    
    # ...
    

    Once this is done, the build is done, you may run a container that has all the capabilities of the base image (from) + your customisations.

    Time to get docker-compose on board. Docker compose is doing the orchestration part for you, meaning it will only execute many docker commands in the right sequence do avoid having to type all of the commands yourself.

    version: "3.8"
    
    services:
      my-first-service:
        image: phpstorm/php-71-apache-xdebug-26
      my-second-service:
        build:
          context: .
          dockerfile: Dockerfile
    

    In the docker-compose.yml above, I am describing two services. What docker-compose will do when doing up is the following:

    In your case, you want to be in the same disposition as the second service, building the image first, and then running the container.

    Because you created your container with an image, you only have the specifics of said image, and never called your own dockerfile.