Home/Blog/Dockerizing a Laravel Application: Complete Guide with Copy-Paste Config
DevOps & Cloud
Dockerizing a Laravel Application: Complete Guide with Copy-Paste Config
AJAjish StephenAugust 7, 20268 min read
Containerizing a Laravel application makes your setup portable and consistent — the same environment runs on your machine, your teammate's machine, and production. Here's a complete, working setup: a Dockerfile for PHP-FPM, an nginx config to serve it, and a docker-compose file tying it together with MySQL and Redis.
The Dockerfile
Create this as Dockerfile in your project root:
# Dockerfile FROMphp:8.3-fpm
WORKDIR/var/www
# System dependencies RUNapt-get update && apt-get install -y \ git curl libpng-dev libonig-dev libxml2-dev zip unzip libzip-dev \ && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip
Your app is now live at http://localhost:8000, running across four containers that mirror a real production topology.
Need help containerizing a real production app, or setting up CI/CD around a setup like this? This is exactly the kind of work I do as part of DevOps services engagements.
Common questions
Do I need Docker for local Laravel development?
Not strictly — tools like Laravel Herd or Valet work fine for solo local development. Docker earns its place when you need your team's environments to match exactly, when you're deploying to a containerized production environment, or when your app depends on specific versions of PHP, MySQL, or Redis that you don't want installed directly on your machine.
Should I use the same Dockerfile for development and production?
Generally no. Development benefits from bind-mounted volumes for live code reloading and relaxed settings like Xdebug. Production wants an optimized, immutable image with code baked in, opcache enabled, and no dev dependencies. Multi-stage Dockerfiles or separate compose files for each environment handle this cleanly.
Why use PHP-FPM with a separate nginx container instead of Apache in one container?
Separating PHP-FPM and nginx into distinct containers follows Docker's one-process-per-container principle, making each easier to scale, monitor, and replace independently. It also matches how most production Laravel deployments are actually architected, so your local setup mirrors reality.
How do I run artisan commands inside the Docker container?
Use docker compose exec app php artisan <command>, replacing app with whatever your PHP service is named in docker-compose.yml. This runs the command inside the running container rather than on your host machine, where PHP may not even be installed.
Ready to containerize your production stack properly?
I set up Docker, CI/CD, and cloud infrastructure that's built to run, not just build.