Skip to content

Instantly share code, notes, and snippets.

@a-r-m-i-n
Created April 30, 2026 15:28
Show Gist options
  • Select an option

  • Save a-r-m-i-n/8c3b515f4e99f859f73c7e665b8a1d5b to your computer and use it in GitHub Desktop.

Select an option

Save a-r-m-i-n/8c3b515f4e99f859f73c7e665b8a1d5b to your computer and use it in GitHub Desktop.
Run PHP in WSL via Docker

Run PHP in WSL via Docker

This Gist provides a small wrapper script to run PHP inside Docker from WSL, without installing PHP locally.

Requirements

  • WSL installed
  • Docker Desktop installed and enabled for your WSL distro
  • Bash

Setup

Save the script as php:

chmod +x php

Optional: move it somewhere in your PATH:

mkdir -p ~/.local/bin
mv php ~/.local/bin/php

Make sure ~/.local/bin is in your PATH.

Usage

Run PHP files from any directory:

php script.php

Run inline PHP:

php -r 'echo PHP_VERSION . PHP_EOL;'

The script uses the official php:8.5-cli Docker image and mounts your current working directory, so files are available inside the container.

#!/usr/bin/env bash
set -euo pipefail
mounts=( -v "$HOME":"$HOME" )
docker_args=( --rm -e TERM="${TERM:-xterm-256color}" )
if [[ -t 0 && -t 1 ]]; then
docker_args+=( -it )
fi
if [[ "$PWD" != "$HOME"* ]]; then
mounts+=( -v "$PWD":"$PWD" )
fi
exec docker run \
"${docker_args[@]}" \
"${mounts[@]}" \
-w "$PWD" \
php:8.5-cli php "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment