Home > Software engineering >  Php script won't run on docker localhost
Php script won't run on docker localhost

Time:06-17

I am very new to Docker so please forgive any technical wording mistakes I make regarding this.

I am trying to get a very basic "Hello World!" php script to run through a localhost docker container. I am trying to run this script to test out this image that contains php and oracle: https://hub.docker.com/r/silencesys/php-oci8

I pulled it using this command: docker pull silencesys/php-oci8

Then I tried to run it with numerous commands, some of them giving me this:

[16-Jun-2022 17:57:12] NOTICE: fpm is running, pid 1
[16-Jun-2022 17:57:12] NOTICE: ready to handle connections

And then some of them give a random string of letters and numbers. I don't know what it refers to but I know that it means that it is running the container. When I open the container's terminal and run 'ls' I can see my index.php file. I can even run 'cat index.php' and see that it contains the proper code in there to run "Hello World!" I will even include the code in here as a sanity check:

<?php 
  echo "Hello World!";
?>

But everytime that I open up the localhost in my browser, I get this:

This page isn’t working right now
localhost didn’t send any data.
ERR_EMPTY_RESPONSE

I have tried so many variations of this base docker run command that I found here:

docker run -d -p 80:80 --rm --name temp-container -v "$PWD":/var/www/html silencesys/php-oci8

Any help is greatly appreciated!

CodePudding user response:

The docker image you are referring to does contain PHP, but it does not contain a web server.

Servers that serve web pages using PHP consist of a webserver software, that accepts HTTP-requests and communicates to PHP running in the background if needed. It can be thought of an intermediary between the Browser and PHP, and if it is missing the browser cannot access your PHP script.

I haven't found an image that could help you right away, however I stumbled upon this, and it tells that for your usecase you should go for a PHP image that has Apache included (i.e. the name will contain apache in the tag).

CodePudding user response:

The way I solved this was by using this image in conjunction with the image I was using in the Question. Here is a step by step guide of what I did:

  1. Create folder in home/[username] directory

  2. docker pull soivangoi/nginx-php-adminer-oci8

  3. docker pull silencesys/php-oci8

  4. docker run -dit -p 8080:80 soivangoi/nginx-php-adminer-oci8 -v "$PWD":/var/www/ silencesys/php-oci8

    4.1. If that doesn't work try this "docker run -d -it -p 8080:80 -v "$PWD":/var/www/ silencesys/php-oci8"

  5. docker run -it -p 8080:80 --rm --name test-container -v "$PWD":/var/www/ soivangoi/nginx-php-adminer-oci8

  6. Bash into the container. Either by command line or by clicking the terminal button on the container.

  7. cd to where your php code is stored.

  8. Run it!

Just make sure that whatever network you are on has access to the oracle database or else it will not let you run your code still.

  • Related