The easiest way to run a PHP script / website
The easiest way to run a standard PHP script or website instantly using Docker. Just one docker run command and your code is live for quick testing.
Changelog

This article shows the fastest way to run a standard PHP script or site locally: drop the code into a folder and run docker run -d -p 8080:80 -u ${UID}:${GID} -v "$PWD":/var/www/html php:8.3-apache, then browse to http://127.0.0.1:8080 — no local PHP or Apache install needed, and swapping the image tag switches PHP versions instantly.
Imagine this situation: you wish to run a piece of standard PHP code (no dependency) like the one below:
<?php
echo "<h2>Incorrect, silent bug</h2>";
print_r(json_decode(utf8_decode("Ipso lorem"), true));
echo "<h2>Incorrect, we got an exception</h2>";
print_r(json_decode(utf8_decode("Ipso lorem"), true, 512, JSON_THROW_ON_ERROR));This snippet demonstrates the difference between two ways of handling JSON decoding errors in PHP. The first attempt silently fails and returns null, while the second attempt throws an exception when the JSON is invalid, making the error explicit and catchable.
The easiest way is to:
- Create a temporary directory with
mkdir /tmp/snippet && cd $_, - Create an
index.phpfile there (with your snippet like above), - Run this command in the console:
docker run -d -p 8080:80 -u ${UID}:${GID} -v "$PWD":/var/www/html php:8.3-apacheand, - Browse to
http://127.0.0.1:8080.
It's done.
Explanations for the docker run -d -p 8080:80 -u ${UID}:${GID} -v "$PWD":/var/www/html php:8.3-apache command:
- We wish to make the local website accessible on port 8080 (so
http://127.0.0.1:8080), - We wish to synchronize the current folder (i.e.
/tmp/snippet) with the Docker container so changes in any file in/tmp/snippetwill be immediately reflected in Docker and thus in your browser, - And we wish to use
php:8.3-apache. Just replace with e.g.php:8.4-apacheto switch to a newer version.
Three things you'll want next: Update php.ini when using a Docker image when the default settings are too tight, Docker init now supports PHP to turn this one-liner into a real Dockerfile + compose.yaml, and jakzal/phpqa to run every PHP quality tool on that code — still without installing anything.
For a site with no PHP at all, Running an HTML site in seconds using Docker is the even lighter version.





