On this page
How to Deploy Laravel Apps with Docker: A Step-by-Step Guide
Deploying Laravel applications can be tricky, but Docker simplifies the process by containerizing your app and its dependencies. In this guide, I'll show you how to deploy a production-ready Laravel application using Docker.
Why Use Docker for Laravel Deployment?
Before we dive in, let's understand why Docker is worth considering:
- Consistent environments across development, staging, and production
- Isolated dependencies that won't interfere with your host system
- Easy scaling with container orchestration tools
- Reproducible builds for better CI/CD pipelines
Prerequisites
- Docker and Docker Compose installed on your system
- A Laravel application (or create a new one)
- Basic understanding of command line operations
Step 1: Setting Up Your Docker Environment
First, create a docker-compose.yml file in your project root:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
container_name: laravel_app
restart: unless-stopped
volumes:
- .:/var/www/html
networks:
- laravel
webserver:
image: nginx:alpine
container_name: nginx_server
restart: unless-stopped
ports:
- "8080:80"
volumes:
- .:/var/www/html
- ./docker/nginx:/etc/nginx/conf.d/
networks:
- laravel
db:
image: mysql:8.0
container_name: mysql_db
restart: unless-stopped
environment:
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: root
volumes:
- dbdata:/var/lib/mysql
networks:
- laravel
networks:
laravel:
driver: bridge
volumes:
dbdata:
driver: local
Step 2: Creating the Dockerfile
Create a Dockerfile in your project root:
FROM php:8.1-fpm
# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www/html
# Copy existing application directory contents
COPY . /var/www/html
# Install PHP dependencies
RUN composer install --no-interaction --optimize-autoloader --no-dev
# Set permissions
RUN chown -R www-data:www-data /var/www/html/storage
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
Step 3: Configuring Nginx
Create a docker/nginx directory and add an app.conf file:
server {
listen 80;
server_name localhost;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Step 4: Building and Running Containers
Build and start your containers:
docker-compose up -d --build
Step 5: Configuring Laravel for Production
Update your .env file with production settings:
APP_ENV=production
APP_DEBUG=false
APP_URL=http://localhost:8080
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret
Run migrations and optimize the application:
docker-compose exec app php artisan migrate --force
docker-compose exec app php artisan optimize
Docker vs Traditional Deployment
| Feature | Docker | Traditional |
|---|---|---|
| Setup Time | Longer initial setup | Faster initial setup |
| Consistency | High (identical environments) | Varies (dependencies may differ) |
| Resource Usage | Higher (container overhead) | Lower (direct host usage) |
| Scaling | Easy with orchestration | More complex |
| Learning Curve | Steeper | Gentler |
Production Considerations
- Use .dockerignore: Exclude unnecessary files from your Docker context
- Security: Regularly update base images and dependencies
- Performance: Tune PHP-FPM and Nginx configurations
- Logging: Set up proper logging with Docker's logging drivers
- Backup: Implement regular database backups
FAQ
1. Can I use this setup for production?
Yes, but make sure to add proper security measures like SSL certificates and configure your firewall.
2. How do I handle file uploads?
Mount a volume to persist uploaded files:
volumes:
- uploads:/var/www/html/storage/app/public
3. What about environment variables?
Use Docker secrets or environment files for sensitive information in production.
4. How to scale the application?
You can use Docker Swarm or Kubernetes for container orchestration.
5. What's the best way to monitor containers?
Use tools like Prometheus with Grafana or the built-in Docker stats API.
Conclusion
Dockerizing your Laravel application might seem complex at first, but the benefits of consistent environments and easier deployment make it worth the effort. This guide should give you a solid foundation for deploying your Laravel applications with Docker. For more Laravel tips and tutorials, visit mahbuburriad.com.