Tooling2 min read

Docker for an existing Laravel app

Most Docker tutorials start with a fresh app, which is the one situation nobody is in. This series moves an app your team already runs into Docker without rewriting it, then shows what the team's day looks like afterwards.

By Abhishek Fouzdar · Updated

Most Docker tutorials start with a fresh application, which is the one situation nobody is in. The app you want to containerise already exists. It has a database with data in it, queued jobs, a scheduled command or two, a Vite build, and three teammates whose machines all differ slightly. This series takes an app like that and moves it into Docker without rewriting any of it, then shows what the team's day looks like afterwards.

Why bother

Three problems, all familiar.

Onboarding takes a day. A new teammate installs PHP, picks the wrong version, installs MySQL, fights with its root password, installs Node, and reads a wiki page that was accurate last spring. With Docker the instructions are "install Docker, clone, run one command".

It works on my machine. PHP 8.3 here, 8.4 there, a MySQL 8.0 that behaves differently from the 8.4 in production, a Redis someone forgot to start. Docker makes every machine run the same versions from the same files, committed in the repo.

Half the app never runs locally. The queue worker and the scheduler are things people start by hand when they remember. In the stack below they run all the time, the same way they do in production, so the job that only fails under the worker fails on your laptop first.

What you will end up with

A docker/ folder, a compose.yaml, a bin/dev script, and a few changed lines in .env.example. Nothing in app/, routes/ or resources/ changes. The stack:

Service What it runs
app PHP-FPM 8.4 with the extensions Laravel needs, plus Composer
web nginx, serving public/ and passing PHP to app
db MySQL 8.4, data in a named volume
redis cache, sessions and the queue
queue php artisan queue:work, same image as app
scheduler php artisan schedule:work, same image as app
node the Vite dev server with hot reload
mailpit catches every mail the app sends, with a web inbox

The host needs Docker and nothing else. No PHP, no Composer, no Node, no MySQL installed natively, and nothing that was installed natively gets in the way, because every port the stack publishes can be moved.

The example app

To keep every command in the series checkable, the posts use a small Laravel 12 app that stands in for yours. It has one route that dispatches a queued job, which sends a mail, and one scheduled command that writes a line to the log every minute. That is enough to prove the worker, the scheduler and the mail catcher are all working, and it is the shape most real apps have. Everything applies unchanged to an app with a hundred routes.

What you need

  • Docker Engine on Linux, or Docker Desktop on macOS or Windows (with WSL 2). Check with docker compose version; you want the docker compose plugin, the one that answers to docker compose with a space (v2 or newer; v5 at the time of writing), not the retired standalone docker-compose v1. Both Docker Engine and Docker Desktop include it.
  • An existing Laravel app on Git. Laravel 10 or newer works; the posts use 12.
  • A terminal. Every command runs from the project root.

The parts

  1. The app image and the minimum stack. A PHP image built for your app, nginx, MySQL, and a Compose file that starts them. By the end the existing app answers at http://localhost:8080, with its database in a container and its files still owned by you.
  2. Redis, the worker, the scheduler, Vite and Mailpit. Redis, the queue worker, the scheduler, the Vite dev server with working hot reload, and Mailpit. A queued mail goes from a route to an inbox you can open.
  3. Daily use for a team. One script for every command, onboarding a new teammate in one command, file ownership, tests, logs, Xdebug when you need it, a variant for macOS, and the failures people actually hit, with what each one means.

Each part shows every file in full, so there is nothing to download. Start with part 1.

New posts by email

Get new posts by email. Nothing else, no digests, unsubscribe in one click.

Comments

Comments are GitHub Discussions in shipbytes/discussions. Sign in with GitHub to post.