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 Stephen August 7, 2026 8 min read
Dockerizing a Laravel Application: Complete Guide with Copy-Paste Config

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

# Composer
COPY--from=composer:latest /usr/bin/composer /usr/bin/composer

COPY. .

RUNcomposer install --no-dev --optimize-autoloader \
  && chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache

EXPOSE9000
CMD["php-fpm"]

The nginx config

Create docker/nginx/default.conf:

server{
  listen80;
  indexindex.php index.html;
  root/var/www/public;

  location/ {
    try_files$uri $uri/ /index.php?$query_string;
  }

  location~ \.php$ {
    fastcgi_passapp:9000;
    fastcgi_indexindex.php;
    includefastcgi_params;
    fastcgi_paramSCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

docker-compose.yml

Ties app, nginx, MySQL and Redis together:

services:
  app:
    build:.
    volumes:
      - .:/var/www
    environment:
      DB_HOST: mysql
      REDIS_HOST: redis

  nginx:
    image:nginx:alpine
    ports:
      - "8000:80"
    volumes:
      - .:/var/www
      - ./docker/nginx:/etc/nginx/conf.d
    depends_on:
      - app

  mysql:
    image:mysql:8.0
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - mysql_data:/var/lib/mysql

  redis:
    image:redis:alpine

volumes:
  mysql_data:

.dockerignore

Keep the build context small — create .dockerignore:

.git
.env
node_modules
vendor
storage/logs/*
docker

Build and run

dockercompose up -d --build
dockercompose exec app php artisan key:generate
dockercompose exec app php artisan migrate

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.
Explore DevOps Services →
© Copyright 2024 Ajish Stephen