33

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?

6
  • 8
    The container doesn't have a web server. You need to use something like FROM php:7.2-apache Commented Jan 14, 2019 at 13:10
  • 2
    Can you be more specific while referring "your application" and "does not work". Commented Jan 14, 2019 at 13:12
  • When I try to open the website I have: ERR_CONNECTION_REFUSED, for php:7.2-apache still the same problem Commented Jan 14, 2019 at 13:14
  • Just search in the doc... docker run -d -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html php:7.2-apache Commented Jan 14, 2019 at 13:18
  • I would like to run this using Dockerfile. Commented Jan 14, 2019 at 13:25

5 Answers 5

51

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

Sign up to request clarification or add additional context in comments.

3 Comments

Just what I was looking for. Perfect for testing simple scripts and behaviour of old PHP versions. Thank you.
Just stumbled upon this, solved a problem I was having perfectly. Thanks, very useful!
Wonder, why this answer was not accepted - I tryed and all works. This answer should be accepted
24

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.

1 Comment

But I would like to run web server because I need to display my website in browser.
12

 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/

Comments

3

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

1 Comment

To avoid a "php_network_getaddresses: getaddrinfo failed..." error, I had to modify the command to the following: CMD [ "php", "-S", "0.0.0.0:80", "./index.php" ]
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.