XAMPP Errors Too Often? Here's the Docker Solution
Tired of XAMPP errors? Learn how to migrate to Docker for a more stable, lightweight, and professional PHP development environment.
- Common XAMPP Problems
- Why Does XAMPP Error So Often?
- Docker: A Modern Solution
- Docker vs XAMPP Advantages
- Setting Up Docker for PHP Development
- Preparation
- Project Structure
- docker-compose.yml
- php/Dockerfile
- nginx/default.conf
- src/index.php
- Running the Project
- Build and Start
- Accessing Services
- Common Commands
- Switching PHP Versions
- How to Switch PHP Versions
- Multi-Version PHP
- Troubleshooting
- Error: Port Already in Use
- Error: Permission Denied
- Error: Container Won’t Start
- Error: MySQL Cannot Connect
- Development Tips
- 1. Use Bind Mount
- 2. Set Up Development Tools
- 3. Use a .env File
- 4. Back Up the Database
- Migrating from XAMPP to Docker
- Migration Steps
- Migration Checklist
- Conclusion
XAMPP has long been the go-to choice for PHP developers working on local development. But over time, many have complained about errors that appear with no clear solution. From port conflicts, Apache refusing to start, to MySQL suddenly corrupting. Docker arrives as a more modern and stable solution.
Common XAMPP Problems
Before jumping into the solution, let’s first look at the problems that commonly occur:
| Error | Cause | Frequency |
|---|---|---|
| Port 80/443 already in use | Skype, IIS, or another service is using the same port | Very common |
| Apache won’t start | Port conflict, incompatible module | Common |
| MySQL crashes unexpectedly | Corrupt database, memory leak | Common |
| PHP version locked | Cannot easily switch PHP versions | Sometimes |
| XAMPP won’t install | Windows permissions, antivirus blocking | Sometimes |
Why Does XAMPP Error So Often?
XAMPP runs all services (Apache, MySQL, PHP) in a single bundle. The problem:
- Port conflict - Ports 80 and 443 are often used by other services
- No isolation - All services run in the same environment
- Locked PHP version - Hard to switch PHP versions for different projects
- Windows-centric - Not optimal on Linux/Mac
- Not reproducible - Hard to set up again on another computer
Docker: A Modern Solution
Docker isolates each service in a separate container. Each container has its own environment and doesn’t interfere with the others.
Docker vs XAMPP Advantages
| Aspect | XAMPP | Docker |
|---|---|---|
| Isolation | All services combined | Each service separate |
| Port | Must use ports 80/443 | Can use any port |
| PHP version | Locked | Can switch versions |
| Reproducibility | Difficult | Very easy |
| Performance | Heavy | Lightweight |
| Multi-version | Not possible | Can run multiple versions at once |
| Deployment | Manual | Can deploy directly to server |
Setting Up Docker for PHP Development
Preparation
-
Install Docker Desktop
-
Docker Compose is already included in Docker Desktop
Project Structure
Create a project folder with a structure like this:
my-project/
├── docker-compose.yml
├── php/
│ └── Dockerfile
├── nginx/
│ └── default.conf
├── src/
│ ├── index.php
│ └── ...
└── mysql/
└── init.sql
docker-compose.yml
Create a docker-compose.yml file:
services:
php:
build:
context: .
dockerfile: php/Dockerfile
volumes:
- ./src:/var/www/html
networks:
- app-network
nginx:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
networks:
- app-network
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydb
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
- ./mysql/init.sql:/docker-entrypoint-initdb.d/init.sql
networks:
- app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- "8081:80"
depends_on:
- mysql
networks:
- app-network
networks:
app-network:
volumes:
mysql-data:
php/Dockerfile
Create a php/Dockerfile file:
FROM php:8.2-fpm
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
&& docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
nginx/default.conf
Create an nginx/default.conf file:
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
src/index.php
Create a src/index.php file for testing:
<?php
$host = 'mysql';
$db = 'mydb';
$user = 'user';
$pass = 'password';
try {
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "<h1>Docker PHP Development Works!</h1>";
echo "<p>PHP Version: " . phpversion() . "</p>";
echo "<p>MySQL Connection: OK</p>";
$stmt = $pdo->query("SELECT * FROM users");
echo "<h2>Users:</h2>";
echo "<ul>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<li>{$row['name']} ({$row['email']})</li>";
}
echo "</ul>";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Running the Project
Build and Start
docker compose build
docker compose up -d
docker compose ps
Accessing Services
| Service | URL | Description |
|---|---|---|
| Website | http://localhost:8080 | PHP application |
| phpMyAdmin | http://localhost:8081 | Database management |
| MySQL | localhost:3306 | Direct database access |
Common Commands
docker compose down
docker compose down -v
docker compose logs -f
docker compose exec php bash
docker compose exec php php artisan serve
docker compose exec php composer install
Switching PHP Versions
One of Docker’s advantages is that it’s easy to switch PHP versions.
How to Switch PHP Versions
- Edit the
php/Dockerfilefile:
FROM php:8.1-fpm
# or
FROM php:8.3-fpm
- Rebuild:
docker compose build --no-cache
docker compose up -d
Multi-Version PHP
Want to run several PHP versions at once? You can.
services:
php81:
build:
context: .
dockerfile: php/Dockerfile81
volumes:
- ./src:/var/www/html
php82:
build:
context: .
dockerfile: php/Dockerfile82
volumes:
- ./src:/var/www/html
Troubleshooting
Error: Port Already in Use
sudo lsof -i :8080
sudo kill -9 <PID>
Error: Permission Denied
sudo chown -R $USER:$USER .
chmod -R 755 .
Error: Container Won’t Start
docker compose logs php
docker compose logs nginx
docker compose logs mysql
docker compose restart
Error: MySQL Cannot Connect
docker compose exec mysql mysqladmin ping -h localhost -u root -p
docker compose down -v
docker compose up -d
Development Tips
1. Use Bind Mount
Bind mounts let file changes appear immediately without a rebuild:
volumes:
- ./src:/var/www/html
2. Set Up Development Tools
Add containers for debugging:
mailhog:
image: mailhog/mailhog
ports:
- "8025:8025"
redis:
image: redis:alpine
ports:
- "6379:6379"
3. Use a .env File
Store environment variables in .env:
DB_ROOT_PASSWORD=root
DB_NAME=mydb
DB_USER=user
DB_PASSWORD=password
Then use them in docker-compose.yml:
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
4. Back Up the Database
docker compose exec mysql mysqldump -u root -p mydb > backup.sql
docker compose exec -T mysql mysql -u root -p mydb < backup.sql
Migrating from XAMPP to Docker
Migration Steps
- Back up the XAMPP database - Open XAMPP’s phpMyAdmin, export all databases
- Install Docker Desktop - Download and install from the official website
- Create docker-compose.yml - Copy the template from above
- Copy source code - Move project files to the
src/folder - Import database - Copy the SQL backup to the
mysql/folder - Start containers - Run
docker compose up -d
Migration Checklist
- Docker Desktop installed
- docker-compose.yml created
- Source code moved
- Database imported
- Website accessible at localhost:8080
- phpMyAdmin accessible at localhost:8081
Conclusion
Docker offers a better solution than XAMPP for PHP development. With container isolation, easy PHP version switching, and high reproducibility, Docker is the best choice for modern developers.
Don’t hesitate to migrate from XAMPP to Docker. Start with a small project, and feel the difference.
Related Articles
Download YouTube Videos & MP3s via the Windows Terminal
A complete guide to using yt-dlp on Windows to download videos and music from YouTube directly from the command line.
Tech TipsComplete iPhone Jailbreak Guide 2026: Methods, Risks, and Benefits
Learn everything about iPhone jailbreaking in 2026, including the latest tools, how it works, the risks, and what benefits you can gain.
HomelabSelf-Hosted Analytics with Umami: A Privacy-Friendly Google Analytics Alternative
A complete guide to setting up self-hosted Umami analytics as a Google Analytics alternative that is privacy-friendly, fast, and keeps your data entirely yours.
Your ideas deserve to be read by thousands of people.
ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.
Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.