composebuilder

Docker Compose: Laravel + PostgreSQL + Redis

Run a Laravel app on Apache with PostgreSQL 16 and Redis 7 in Docker Compose, with a Dockerfile, queue and cache settings, volumes and a production checklist.

When to use this stack

This stack runs a Laravel application on Apache and PHP 8.3, with PostgreSQL 16 as the main database and Redis 7 for cache, sessions and queues. It removes the usual pain of installing PHP extensions, matching a Composer version and configuring Apache rewrites on every machine. Each developer or server gets the same image, the same database engine and the same environment variables, which makes Laravel migrations and queue behaviour predictable across environments.

Choose it when your Laravel project needs a real relational database and a shared cache or queue backend, and you want everything in one repository that can move to a VPS. It is a good fit for small business applications, internal tools, APIs and content sites built with Laravel. Do not choose this layout if you need a dedicated queue worker fleet, horizontal scaling across several hosts, or a managed database with automated failover. You can run a queue worker from the same image later, as shown in the variations.

The Dockerfile starts from the official php:8.3-apache image, adds the pdo_pgsql extension, enables Apache rewrites and points the document root to the public folder. Composer then installs production dependencies. Because the app folder is copied into the image, the Laravel .env file must not be included in the image; add it to .dockerignore and pass the settings through compose.

docker-compose.yml

services:
  app:
    build: ./app
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    ports:
      - "8000:80"
    environment:
      APP_NAME: ${APP_NAME}
      APP_ENV: production
      APP_DEBUG: "false"
      APP_KEY: ${APP_KEY}
      APP_URL: ${APP_URL}
      LOG_CHANNEL: stderr
      DB_CONNECTION: pgsql
      DB_HOST: db
      DB_PORT: 5432
      DB_DATABASE: ${POSTGRES_DB}
      DB_USERNAME: ${POSTGRES_USER}
      DB_PASSWORD: ${POSTGRES_PASSWORD}
      REDIS_CLIENT: predis
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD}
      CACHE_STORE: redis
      SESSION_DRIVER: redis
      QUEUE_CONNECTION: redis
    volumes:
      - app_storage:/var/www/html/storage/app/public

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    restart: unless-stopped
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD}
    command: ["redis-server", "--requirepass", "${REDIS_PASSWORD}", "--appendonly", "yes"]
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD-SHELL", "redis-cli -a \"$$REDIS_PASSWORD\" --no-auth-warning ping | grep PONG"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:
  app_storage:
  redis_data:

Dockerfile (app/Dockerfile)

The build context is the ./app folder, which holds the Laravel project. Add a .dockerignore file there with .env, vendor and node_modules.

Dockerfile
FROM php:8.3-apache

RUN apt-get update \
 && apt-get install -y --no-install-recommends libpq-dev unzip git \
 && docker-php-ext-install pdo_pgsql \
 && rm -rf /var/lib/apt/lists/* \
 && a2enmod rewrite \
 && sed -ri -e 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/000-default.conf \
 && sed -ri -e 's!AllowOverride None!AllowOverride All!g' /etc/apache2/apache2.conf

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

WORKDIR /var/www/html
COPY . .
RUN composer install --no-dev --optimize-autoloader --no-interaction \
 && chown -R www-data:www-data storage bootstrap/cache

EXPOSE 80

.env

.env
# Application name shown by Laravel, without spaces
APP_NAME=MyApp
# Generate with: php artisan key:generate --show (the value starts with base64:)
APP_KEY=CHANGE_ME_run_php_artisan_key_generate_show
# Public URL of the site
APP_URL=http://localhost:8000
# PostgreSQL credentials and database name
POSTGRES_DB=app
POSTGRES_USER=app
POSTGRES_PASSWORD=CHANGE_ME_postgres_password
# Redis password
REDIS_PASSWORD=CHANGE_ME_redis_password

Start and verify

Shell
docker compose config --quiet

# Build and start the stack
docker compose up -d --build

# db and redis must be healthy before app starts
docker compose ps

# Run the database migrations once, after the first start
docker compose exec app php artisan migrate --force

# Check the status of the framework and the database connection
docker compose exec app php artisan about

# Confirm the site answers
curl -I http://localhost:8000

Production checklist

  • Keep APP_DEBUG set to false. Debug pages expose environment variables and file paths.
  • Put the app behind HTTPS with a reverse proxy, and set APP_URL to the public address so generated links use HTTPS.
  • Generate a new APP_KEY for each environment and store it only in .env or your secret manager.
  • Run php artisan config:cache and php artisan route:cache during the image build or at deploy time, after the environment variables exist.
  • Keep .env and the vendor folder out of the image with .dockerignore.
  • Protect Redis with the password and do not publish port 6379. This stack keeps it on the internal network.
  • Back up postgres_data with pg_dump and the app_storage volume with a file copy, and test the restore.
  • Run a queue worker from the same image if you use queued jobs, and restart it on every deploy.

Common problems

  • Symptom: “No application encryption key has been specified.” Cause: APP_KEY is empty or still has the placeholder value. Fix: Generate a real key with php artisan key:generate –show, put it in .env, and run docker compose up -d again.
  • Symptom: “Class Predis\Client not found.” Cause: The predis package is not installed in composer.json. Fix: Run composer require predis/predis in ./app, rebuild with docker compose build app, and restart the stack.
  • Symptom: Every route except the home page returns 404. Cause: Apache does not allow .htaccess overrides. Fix: Check that the Dockerfile sed command changed AllowOverride to All in apache2.conf, then rebuild the image.
  • Symptom: “Permission denied” when writing logs or uploads. Cause: The storage folder is owned by root. Fix: Run chown -R www-data:www-data storage bootstrap/cache inside the container or rebuild the image with the Dockerfile above.
  • Symptom: Cache or session data disappears after a Redis restart. Cause: Redis was started without persistence. Fix: Keep the –appendonly yes option in the command and confirm the redis_data volume exists.

Variations

  • Queue worker: Add a worker service with the same build and environment, and set its command to php artisan queue:work –tries=3. Run it with the same QUEUE_CONNECTION=redis so jobs are shared with the web app.
  • Scheduler: Add a service that runs php artisan schedule:work or a cron job calling php artisan schedule:run every minute. Use only one scheduler instance to avoid duplicate jobs.
  • Nginx with PHP-FPM: Split the image into php:8.3-fpm-alpine and an nginx:1.27-alpine service that forwards PHP requests to port 9000. This is a common production layout, but it needs a matching nginx configuration file.

Questions

Which folder contains my Laravel code?

The app service builds from ./app, which must contain artisan, composer.json and the public folder. The code is copied into the image, and the Apache document root is set to public.

Why do I need predis?

The Dockerfile does not install the phpredis extension, so this stack uses the pure PHP predis client. Install it with composer require predis/predis and keep REDIS_CLIENT=predis.

How do I generate APP_KEY?

Run php artisan key:generate --show inside a local copy of the project, then paste the printed base64 value into .env. Never reuse a key from another project.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • Next.js + PostgreSQL + Redis

    Containerize a Next.js app with PostgreSQL 16 and Redis 7 in Docker Compose, using a minimal Dockerfile, healthchecks, named volumes and a production checklist.

    • app
    • db
    • redis
    Difficulty: Intermediate
  • Django + PostgreSQL + Celery

    Run a Django app with Gunicorn, PostgreSQL 16, Redis 7 as the Celery broker and a Celery worker in Docker Compose, with healthchecks and a production checklist.

    • web
    • worker
    • db
    • redis
    Difficulty: Advanced
  • Nuxt + PostgreSQL

    Run a Nuxt 3 app built with Nitro and PostgreSQL 16 in Docker Compose, with a Node 22 Dockerfile, a database healthcheck, persistent data and a checklist.

    • app
    • db
    Difficulty: Intermediate
  • Rails + PostgreSQL + Redis

    Run a Ruby on Rails app with PostgreSQL 16 and Redis 7 in Docker Compose, with a slim Ruby 3.3 Dockerfile, database setup, persistent data and a checklist.

    • web
    • db
    • redis
    Difficulty: Intermediate

Browse more Web & Frameworks stacks or the full library.