PHP 8.5 in Docker
| The php:8-cli image | |
| Run one-liners | |
| Mount your code | |
| Serve a page |
The php:8-cli image
The official image php:8-cli bundles the PHP 8.5 command line with nothing else to configure. The tag has two parts: 8 pins the major version and cli picks the variant without a web server.
Pulling the image once is enough - Docker reuses it for every run below, so each example starts fast.
Run one-liners
Containers are throwaway by design: the --rm flag removes the container when the command ends while the image stays cached. Run the version check through the image with one line:
Check the PHP version inside Docker
docker run --rm php:8-cli php -v
PHP 8.5.0 (cli) (built: Nov 2026) (NTS) Copyright (c) The PHP Group
The output is the same version report you get from a local install. Any php command works this way: put it after the image name and Docker runs it inside the container.
Mount your code
Your files live on the host, so mount the project folder into the container. The -v flag maps the current folder to /app inside the container and -w makes it the working folder. With the mount in place the container sees hello.php exactly as saved on disk:
Run a local file inside the container
docker run --rm -v "$PWD":/app -w /app php:8-cli php hello.php
Hello from Docker.
Serve a page
The built-in server works from a container too. Publish port 8000 so the browser on the host can reach it, keep the folder mount from the previous step, and start the server bound to all interfaces. Open port 8000 in a browser to see the project, and stop the container with Ctrl+C when done.
Serve the project from Docker
docker run --rm -p 8000:8000 -v "$PWD":/app -w /app php:8-cli php -S 0.0.0.0:8000
Document root is /app
Next: PHP course
Article author: Arthur Isaev