Skip to content
Back to Blog
Tech Tips

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.

A
Alif Afriza Author
· · 6 min read
XAMPP Errors Too Often? Here's the Docker Solution

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:

ErrorCauseFrequency
Port 80/443 already in useSkype, IIS, or another service is using the same portVery common
Apache won’t startPort conflict, incompatible moduleCommon
MySQL crashes unexpectedlyCorrupt database, memory leakCommon
PHP version lockedCannot easily switch PHP versionsSometimes
XAMPP won’t installWindows permissions, antivirus blockingSometimes

Why Does XAMPP Error So Often?

XAMPP runs all services (Apache, MySQL, PHP) in a single bundle. The problem:

  1. Port conflict - Ports 80 and 443 are often used by other services
  2. No isolation - All services run in the same environment
  3. Locked PHP version - Hard to switch PHP versions for different projects
  4. Windows-centric - Not optimal on Linux/Mac
  5. 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

AspectXAMPPDocker
IsolationAll services combinedEach service separate
PortMust use ports 80/443Can use any port
PHP versionLockedCan switch versions
ReproducibilityDifficultVery easy
PerformanceHeavyLightweight
Multi-versionNot possibleCan run multiple versions at once
DeploymentManualCan deploy directly to server

Setting Up Docker for PHP Development

Preparation

  1. Install Docker Desktop

  2. 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

ServiceURLDescription
Websitehttp://localhost:8080PHP application
phpMyAdminhttp://localhost:8081Database management
MySQLlocalhost:3306Direct 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

  1. Edit the php/Dockerfile file:
FROM php:8.1-fpm
# or
FROM php:8.3-fpm
  1. 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

  1. Back up the XAMPP database - Open XAMPP’s phpMyAdmin, export all databases
  2. Install Docker Desktop - Download and install from the official website
  3. Create docker-compose.yml - Copy the template from above
  4. Copy source code - Move project files to the src/ folder
  5. Import database - Copy the SQL backup to the mysql/ folder
  6. 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.

Open for Contributors

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.

Discussion & Comments