Kritim Yantra
Apr 13, 2025
In this guide, we’ll walk through how to:
✅ Run PHP 8.4 in a Docker container
✅ Mount local PHP files for real-time development
✅ Set up Apache to serve PHP files
Create a project folder with the following files:
index.php
<?php
echo "Hello PHP World!";
?>
Dockerfile
FROM php:8.4-apache
COPY index.php /var/www/html/
EXPOSE 80
docker build -t php-app .
docker run -d -p 8000:80 -v "%cd%":/var/www/html php-app
%cd%
(CMD) or ${PWD}
(PowerShell) represents the current directory.docker run -d -p 8000:80 -v $(pwd):/var/www/html php-app
$(pwd)
automatically resolves to the current working directory.If using WSL2 (Windows Subsystem for Linux):
docker run -d -p 8000:80 -v $(pwd):/var/www/html php-app
-d
→ Run in detached mode (background). -p 8000:80
→ Map host port 8000
to container port 80
. -v ${PWD}:/var/www/html
→ Mount the current directory to Apache’s web root.Open your browser and visit:
http://localhost:8000
You should see:
Hello PHP World!
Test Live Reloading:
index.php
on your local machine. docker ps
docker logs <container_id>
docker exec -it <container_id> bash
✔ Fast Development – Changes reflect instantly due to volume mounting.
✔ Consistent Environment – No "works on my machine" issues.
✔ Easy Deployment – Same image works in production.
For better management, create a docker-compose.yml
:
version: "3.8"
services:
php:
build: .
ports:
- "8000:80"
volumes:
- ./:/var/www/html
Then run:
docker-compose up -d
This setup is perfect for:
Try modifying index.php
and see the changes live! 🚀
Need help? Drop a comment below! 😊
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google