Deploying a Laravel application using Docker can be a great way to ensure consistency across different environments. Typically, you would use multiple containers to separate concerns (e.g., one for the web server, one for the database, etc.). Below is a step-by-step guide to deploying Laravel using Docker on a DigitalOcean droplet.
Step 1: Install Docker and Docker Compose
First, ensure Docker and Docker Compose are installed on your DigitalOcean droplet. You can follow the official Docker installation guide for your specific operating system.
Step 2: Create a Docker Compose File
Create a docker-compose.yml file in the root of your Laravel project. This file will define the services required for your application.
version: '3.8'
services:
app:
image: php:7.4-fpm
container_name: laravel_app
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- laravel
webserver:
image: nginx:alpine
container_name: laravel_webserver
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/nginx/conf.d:/etc/nginx/conf.d
ports:
- "80:80"
networks:
- laravel
db:
image: mysql:5.7
container_name: laravel_db
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
volumes:
- dbdata:/var/lib/mysql
networks:
- laravel
networks:
laravel:
volumes:
dbdata:
Step 3: Configure Nginx and PHP
Create the necessary configuration files for Nginx and PHP.
Nginx Configuration
Create a directory docker/nginx/conf.d and add a file default.conf:
server {
listen 80;
index index.php index.html;
server_name localhost;
root /var/www/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
PHP Configuration
Create a directory docker/php and add a file local.ini:
memory_limit = 512M
upload_max_filesize = 50M
post_max_size = 50M
Step 4: Build and Run the Containers
Run the following command to build and start your containers:
docker-compose up -d
Step 5: Set Up Laravel Environment
Make sure your .env file is configured to connect to the MySQL container:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret
Step 6: Run Laravel Migrations
Execute the following command to run your Laravel migrations:
docker-compose exec app php artisan migrate
Troubleshooting
If you encounter issues with the MySQL container stopping, you can check the logs for more information:
docker-compose logs db
If you need to restart a specific container, you can use:
docker-compose restart <service_name>
For example, to restart the MySQL container:
docker-compose restart db
Conclusion
Using Docker Compose to manage multiple containers for your Laravel application can help you maintain a clean and organized environment. This setup separates the web server, PHP, and database into different containers, making it easier to manage and scale your application.