Want to run php and composer on any machine—without installing PHP or Composer locally? You can, by launching a disposable container that has everything preinstalled and points at your project folder.
Here’s the command:
docker run -it --rm --name php-shell --user=$(id -u):$(id -g) -v "$PWD":/home --workdir "/home" composer /bin/bash
This drops you into a bash shell inside the official composer image (which already includes PHP and Composer). From there you can run php, composer, framework CLIs, etc., exactly as if they were installed on your machine—except they never touch your host system.
What each flag does (and why it matters)
docker run Start a new container.
-it
Interactive + TTY. Lets you type commands and see output like a normal shell.
--rm
Auto-remove the container when you exit. No leftover containers to clean up. Also makes this command repeatable without having to manually clean up the container first.
--name php-shell
Gives the container a friendly name. Useful if you want to attach, stop, or inspect it later.
--user=$(id -u):$(id -g)
Run processes inside the container as your user and group (not root).
Why: Any files Composer writes (e.g., vendor/, lockfiles) will be owned by your user on the host, preventing annoying permission issues.
-v "$PWD":/home
Bind-mount your current directory into the container at /home.
Why: The container sees your project files; Composer can install dependencies into your working dir.
--workdir "/home"
Set /home as the working directory.
Why: When you land in the shell, you’re already in your project root.
composer
The image name. The official composer image bundles PHP + Composer and is maintained for this exact use case.
/bin/bash
The command to execute in the container.
Why: You get an interactive shell; from there you can run php -v, composer install, or anything else.
Windows users: run this from WSL2
Important: The command above uses $(id -u), $(id -g), and "$PWD", which are Linux shell features. On Windows, the smoothest experience is to:
- Install WSL2 (Ubuntu, for example).
- Open a WSL2 shell and run the Docker command from inside WSL2.
- Ensure Docker Desktop is set to integrate with your WSL2 distro.
You’ll also get significantly better filesystem performance if your project lives inside your WSL2 filesystem (e.g., under /home/…) rather than a Windows-mounted path like /mnt/c/.... Composer touches many files; file-sharing between Windows and Docker can become sluggish with large dependency trees. Running fully inside WSL2 keeps everything fast and responsive.
Quick recipes
1) Open a PHP shell with Composer available
Use the original command and then run anything you like:
php -v
composer --version
composer install
Exit with exit or Ctrl+D.
2) Run a one-off Composer command (no shell)
If you don’t need an interactive shell, just replace /bin/bash with the composer command you want:
docker run --rm --user=$(id -u):$(id -g) -v "$PWD":/home --workdir /home composer \
composer install --no-interaction --prefer-dist
3) Run plain PHP scripts
Use the same image; it already has PHP:
docker run --rm --user=$(id -u):$(id -g) -v "$PWD":/home --workdir /home composer \
php your-script.php
4) Access private repositories (SSH keys)
Mount your SSH keys read-only if you need to pull from private Git repos:
docker run -it --rm --name php-shell \
--user=$(id -u):$(id -g) \
-v "$PWD":/home \
-v "$HOME/.ssh":/home/.ssh:ro \
--workdir /home \
composer /bin/bash
5) Handy aliases
Add these to your shell profile to make it feel “installed”:
alias dcomposer='docker run --rm --user=$(id -u):$(id -g) -v "$PWD":/home -v composer-cache:/tmp --workdir /home composer composer'
alias dphp='docker run --rm --user=$(id -u):$(id -g) -v "$PWD":/home --workdir /home composer php'
Usage:
dcomposer install
dphp -v

