Tools & Hosting Reviews

Docker for Beginners: Run PHP and MySQL Projects the Same Way on Every Computer

"It works on my computer" problems disappear with Docker. Here's a beginner-friendly guide to running PHP and MySQL projects in containers.

In this article · 8 sections
  1. Key concepts
  2. A simple PHP + MySQL setup
  3. Useful commands
  4. Benefits
  5. When Docker isn't needed
  6. Security notes
  7. Next steps
  8. FAQs
  9. Is Docker hard to learn?
  10. Does Docker work on Windows?

Every developer has heard "but it works on my machine!" Different PHP versions, missing extensions and database settings cause bugs that waste hours. Docker packages an app and its environment into containers that run the same everywhere. This guide covers Docker for beginners working on PHP/MySQL websites.

Key concepts

  • Image: a template (e.g. PHP 8.3 with Apache)
  • Container: a running instance of an image
  • Volume: storage that persists outside the container (e.g. database data)
  • Docker Compose: a file describing multiple containers that work together

A simple PHP + MySQL setup

docker-compose.yml:

services:
  web:
    image: php:8.3-apache
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www/html
    depends_on:
      - db
  db:
    image: mariadb:11
    environment:
      MARIADB_ROOT_PASSWORD: devroot
      MARIADB_DATABASE: app
      MARIADB_USER: app
      MARIADB_PASSWORD: devpass
    volumes:
      - dbdata:/var/lib/mysql
volumes:
  dbdata:

Run:

docker compose up -d

Open http://localhost:8080. Your project folder is mounted into the container, so edits appear instantly.

For PHP extensions like mysqli or pdo_mysql, create a small Dockerfile based on the PHP image and install them.

Useful commands

docker compose ps          # running containers
docker compose logs -f web # follow logs
docker compose exec web bash
docker compose down        # stop

Benefits

  • Same PHP/MySQL versions for every developer
  • Match production versions to catch problems early
  • Easy onboarding: new developers run one command
  • Test PHP upgrades safely. See upgrading PHP

When Docker isn't needed

  • Small brochure sites edited occasionally
  • Deploying to shared hosting (which doesn't run Docker); Docker is still useful locally, but production uses the host's environment

Security notes

  • Don't commit real passwords in compose files. Use .env files excluded from Git. See Git basics
  • Use official images and keep them updated
  • Don't expose database ports publicly on servers

Next steps

Learn Dockerfiles, multi-stage builds and deploying containers to a VPS. See VPS and cloud hosting.

FAQs

Is Docker hard to learn?

The basics (compose files and a few commands) can be learned in a day. Advanced topics take longer.

Does Docker work on Windows?

Yes, with Docker Desktop (using WSL 2) on supported Windows versions.

Joboo Web uses consistent development environments to ship reliable PHP/MySQL websites. Talk to us.

Share this article

Joboo Web

Web design, SEO & digital marketing team · Ludhiana, Punjab

Joboo Web has helped Indian businesses grow online since 2019 — building fast websites and running SEO, Google Ads and social campaigns for clients in 80+ cities. About us →

Keep reading

💬