Back to Blog
Docker for Laravel in Production: A Complete Guide
Why Docker?
Docker eliminates "works on my machine" by packaging your application with its dependencies. The same container runs identically in development, staging, and production.
Production Dockerfile
FROM php:8.3-fpm-alpine as base
# Install system dependencies
RUN apk add --no-cache \
nginx \
supervisor \
libpng-dev \
libzip-dev \
&& docker-php-ext-install pdo_mysql gd zip opcache
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
# Production stage
FROM base as production
# Copy application
COPY . .
# Install dependencies (no dev)
RUN composer install --no-dev --optimize-autoloader --no-interaction
# Build assets
RUN npm ci && npm run build && rm -rf node_modules
# Set permissions
RUN chown -R www-data:www-data storage bootstrap/cache
# Copy configs
COPY docker/nginx.conf /etc/nginx/nginx.conf
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY docker/php.ini /usr/local/etc/php/conf.d/custom.ini
EXPOSE 80
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
Nginx Configuration
server {
listen 80;
root /var/www/html/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known) {
deny all;
}
}
Docker Compose for Development
version: '3.8'
services:
app:
build:
context: .
target: base
volumes:
- .:/var/www/html
depends_on:
- mysql
- redis
mysql:
image: mysql:8.0
environment:
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: secret
volumes:
- mysql_data:/var/lib/mysql
redis:
image: redis:alpine
queue:
build:
context: .
target: base
command: php artisan queue:work
depends_on:
- mysql
- redis
volumes:
mysql_data:
Health Checks
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost/health || exit 1
Multi-Stage Builds
Multi-stage builds keep production images small by excluding build tools:
# Build stage has Node.js for asset compilation
FROM node:18-alpine as assets
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage only has PHP
FROM php:8.3-fpm-alpine as production
COPY --from=assets /app/public/build /var/www/html/public/build
Conclusion
Docker provides consistency across environments and simplifies deployment. Start with a solid Dockerfile, use multi-stage builds for optimization, and implement health checks for orchestration.
Related Articles
Need Help With Your Project?
I respond to all inquiries within 24 hours. Let's discuss how I can help build your production-ready system.
Get In Touch