Aleksander Chelpski
Aleksander Chelpski

Reputation: 771

How to run a PHP script on docker?

I have index.php:

<?php
echo "Hello World";
?>

Dockerfile from the website: https://docs.docker.com/samples/library/php/

FROM php:7.2-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php" ]

I build image and run container:

docker build -t my-php-app .
docker run -p 7000:80 --rm --name hello-world-test my-php-app

I see only text "Hello World" but my application doesn't work in http://localhost:7000/ why?

Upvotes: 32

Views: 78644

Answers (5)

Ortomala Lokni
Ortomala Lokni

Reputation: 62466

You can keep the same base image as you have php:7.2-cli:

FROM php:7.2-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./index.php" ]

...build the image:

docker build -t my-php-app .

...run it:

docker run --rm --name hello-world-test my-php-app

You will obtain:

Hello World

Everything you did was correct except the port mapping (-p 7000:80) which is not necessary because you aren't running a web server.

== EDIT

If you want to run it as a web server, use the following Dockerfile:

FROM php:7.2-apache
COPY . /var/www/html/

...build it:

docker build -t my-php-app .

...and run it:

docker run -p 8080:80 -d my-php-app

You will then have your PHP script running on 8080.

Upvotes: 24

rbento
rbento

Reputation: 11598

Here is a quick and simple example with Docker on Windows 11, assuming you have a similar directory structure as the example below:

C:\Users\YourName\Workspace\MyProject\program.php

And program.php has the following content:

<?php echo "It works!"; ?>

Then, in the Command Prompt, navigate to the project directory:

cd C:\Users\YourName\Workspace\MyProject

Run with CLI

docker run --rm -p 8080:8080 -v %CD%:/cli php:7.4-cli php -S 0.0.0.0:8080 /cli/program.php

View: http://localhost:8080

Run with SERVER

docker run --rm -d -p 8081:80 -v %CD%:/server --mount type=bind,source="%CD%",target=/var/www/html php:apache

View: http://localhost:8081/program.php

Then feel free to modify program.php and refresh the page.

Environment

  • Docker version 20.10.16, build aa7e414
  • Windows 11 Home, Version 22H2, OS build 22622.436

Upvotes: 0

Eng lish
Eng lish

Reputation: 121

 1. Create simple php script:

echo '<?php echo "Working";' > my.php

 2. Run docker:

docker  run -p 8080:8080 --rm -v $(pwd):$(pwd) php:7.4-cli php -S 0.0.0.0:8080  $(pwd)/my.php

 3. Open in browser:

http://localhost:8080/

Upvotes: 12

Hussam
Hussam

Reputation: 510

Many answers suggest using Apache for this, but that is not required. You need to have your application in the container run continuously on a specific port. You can keep the php:7.2-cli image, but your CMD should be different:

CMD [ "php", "-S 0.0.0.0:80", "./index.php" ]

This will run the built-in PHP webserver and after that you can expose it with the docker run command you already had

Upvotes: 3

Alexander Gavazov
Alexander Gavazov

Reputation: 628

If you want to run some script "on the fly" with php-cli you can create the container and remove it immediately after the script execution.

Just go to the directory with your code and run:

Unix

docker container run --rm -v $(pwd):/app/ php:7.4-cli php /app/script.php

Windows - cmd

docker container run --rm -v %cd%:/app/ php:7.4-cli php /app/script.php

Windows - power shell

docker container run --rm -v ${PWD}:/app/ php:7.4-cli php /app/script.php

--rm will remove the container after execution

-v $(pwd):/app/ will mount current directory

php:7.4-cli is the image

and php /app/script.php is the command which will be executed after the container is created

Upvotes: 50

Related Questions