ENGINEERING JOURNAL

Docker Compose for Local Laravel Development: A Practical Setup

A practical Docker Compose setup for local Laravel development: PHP, database, Redis, and queue workers running the same way on every machine.

THE ARTICLE 01 — 02

Every developer has lived through the "works on my machine" moment. On my team we hit it with a Laravel app where queues ran fine on one machine, timed out on another, and refused to run at all on a third. The fix was to stop depending on whatever happened to be installed on each machine and run the whole stack the same way everywhere. Docker Compose is the simplest way I know to do that for Laravel development.

What Docker Compose gives you for Laravel

A typical Laravel app needs PHP, a web server, a database, Redis, and maybe a queue worker and scheduler. Without containers, each developer installs all of that by hand and each machine drifts. With Compose, the stack is defined in one docker-compose.yml file, and docker compose up starts the same services for everyone. New team members are productive in minutes, not hours.

A minimal compose file for Laravel

Here is a starting point I have used on Laravel projects. It runs the app with PHP's built-in server through a PHP 8.2 image, plus MySQL and Redis:

services:
  app:
    image: php:8.2-cli
    working_dir: /app
    volumes:
      - ./:/app
    ports:
      - "8000:8000"
    command: ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_started
    environment:
      DB_HOST: db
      DB_PORT: 3306
      DB_DATABASE: laravel
      DB_USERNAME: laravel
      DB_PASSWORD: secret
      REDIS_HOST: redis

  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: rootsecret
    ports:
      - "3306:3306"
    volumes:
      - dbdata:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

volumes:
  dbdata:

A few things worth noting. The app's database host is db, the service name, because Compose gives every service a DNS name on its own network. That is the detail people miss first: your .env uses DB_HOST=db inside the container, even though from your laptop the database is at localhost:3306.

Match your production PHP extensions

The stock php:8.2-cli image does not include extensions Laravel commonly needs, like pdo_mysql, mbstring, bcmath, and redis. When a page works locally but dies on the server with "could not find driver", a missing extension is the usual cause. For anything beyond a quick experiment, write a small Dockerfile instead of using the stock image:

FROM php:8.2-cli
RUN apt-get update && apt-get install -y \
        libzip-dev unzip git \
    && docker-php-ext-install pdo_mysql mbstring bcmath zip \
    && pecl install redis && docker-php-ext-enable redis \
    && apt-get clean && rm -rf /var/lib/apt/lists
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /app

Then change the compose service to build: . instead of image: php:8.2-cli. Now every developer builds the same PHP with the same extensions, and it is one less difference between local and production to debug later.

Run queues and the scheduler in Compose too

If your app uses queues, do not run the worker on your laptop while everything else runs in containers. Add a worker service that shares the app image and runs php artisan queue:work, and another for php artisan schedule:work. That way the worker sees the exact same code, extensions, and environment variables as the web process. Silent queue failures, which I have written about in why Laravel queue jobs fail silently, are much easier to reproduce when the worker environment matches what developers actually run.

Common problems and their fixes

Three issues come up again and again. First, file permissions: files created inside the container may be owned by root, which makes your editor complain. Run the container with your user id by adding user: "${UID:-1000}:${GID:-1000}" to the app service, or chown the directory once after setup. Second, slow file sync on macOS and Windows: bind mounts are noticeably slower there, so keep vendor and node_modules out of the mount or use a named volume for them. Third, the database healthcheck: without it, the app container starts before MySQL is ready and Laravel throws a connection error on boot. The depends_on with service_healthy in the example above handles exactly that.

Keep secrets out of the compose file

Do not hardcode real passwords in docker-compose.yml if the file is committed to a repo. Use an .env file next to it and reference variables like ${DB_PASSWORD}. Compose reads .env automatically for variable substitution. It is a small habit, but it keeps credentials out of git history, where they are painful to remove.

The takeaway

Docker Compose will not fix a broken app, but it removes an entire class of environment problems: missing extensions, wrong database versions, workers running different code than the web process. Define the stack once, commit the file, and make docker compose up the documented way to start the project. Your future self, debugging a queue failure at midnight, will be glad every developer is running the same thing.

Muhammad Aqib ← ALL ARTICLES
KEEP EXPLORING

More ideas.
Better engineering.

Explore the journal ↗