In this article · 8 sections
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
.envfiles 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.