Docker for PHP Developers: Stop Fighting Local Setup
Tired of 'works on my machine'? Docker fixes that. Here's how to set it up for PHP projects without the complexity.
Table of Contents
Docker for PHP Developers: Stop Fighting Local Setup
"Works on my machine" is the worst phrase in development.
Docker fixes that. Here's how to use it without the complexity.
Why Docker?
The Problem:
- New dev joins team
- Spends 2 days setting up PHP, MySQL, Redis, Nginx
- Still doesn't work
- "What version of PHP are you using?"
- "Did you enable the mbstring extension?"
- Pain.
With Docker:
git clone project
docker-compose up
Done. Works on Windows, Mac, Linux. Same environment for everyone.
Basic Setup
Create docker-compose.yml:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:8000"
volumes:
- .:/var/www/html
depends_on:
- db
- redis
db:
image: mysql:8.0
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: secret
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
redis:
image: redis:alpine
ports:
- "6379:6379"
volumes:
db_data:
Create Dockerfile:
FROM php:8.3-fpm
# Install dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
COPY . .
RUN composer install
CMD php artisan serve --host=0.0.0.0 --port=8000
Start everything:
docker-compose up -d
Your app runs on http://localhost:8000.
Real Project Example
We dockerized a client's Laravel API:
Before Docker:
- Setup time: 4 hours per developer
- "Works on my machine" issues: Weekly
- Production bugs from env differences: Monthly
After Docker:
- Setup time: 10 minutes
- Environment issues: None
- Production bugs: 70% reduction
Common Mistakes
Mistake 1: Not Using Volumes
# Wrong - Changes don't reflect
services:
app:
image: php:8.3
# Right - Changes sync immediately
services:
app:
image: php:8.3
volumes:
- .:/var/www/html
Mistake 2: Hardcoded Credentials
# Wrong
environment:
DB_PASSWORD: supersecret123
# Right
environment:
DB_PASSWORD: ${DB_PASSWORD}
Use .env file for secrets.
Mistake 3: No .dockerignore
Create .dockerignore:
vendor/
node_modules/
.git/
.env
storage/logs/*
Speeds up builds, reduces image size.
Tips for Laravel Projects
Run Artisan Commands
docker-compose exec app php artisan migrate
docker-compose exec app php artisan queue:work
docker-compose exec app composer install
Database Access
docker-compose exec db mysql -u root -p
View Logs
docker-compose logs -f app
When to Use Docker
Use Docker for:
- Team projects (consistent environments)
- Complex setups (multiple services)
- CI/CD pipelines
- Microservices
Skip Docker for:
- Solo projects (local PHP works fine)
- Simple scripts
- Learning PHP (adds complexity)
Bottom Line
Docker solves real problems. No more environment issues. Faster onboarding. Consistent deployments.
Setup takes 30 minutes. Saves hours every week.
Need help with Docker or Laravel?
We set up Docker environments for Nigerian development teams.
📞 WhatsApp: +234 708 711 0468
📧 info@raspibtech.com
📍 Lagos Island
Related:
Need Help with Your Project?
Let's discuss how Raspib Technology can help transform your business
Related Articles
Laravel 11: What Changed and Why You Should Care
Laravel 11 is out. Slimmer structure, better performance, and features that actually save time. Here's what matters.
Read more →Laravel 12: The Upgrade You've Been Waiting For
Laravel 12 brings major improvements. Here's what changed and why it matters for your projects.
Read more →Next.js 15: The Features That Actually Matter
Next.js 15 changed a lot. Here's what affects your projects, what breaks, and when to upgrade.
Read more →