This project demonstrates how to set up a Laravel application with Docker, integrating essential services like MySQL, Redis, and Nginx. It uses Docker Compose to orchestrate the environment, making development, testing, and deployment consistent and straightforward.
- Docker and Docker Compose installed on your machine.
- A
.env
file configured for the Laravel application. - Basic familiarity with Docker and Laravel.
git clone <repository_url>
cd <repository_folder>
Ensure the .env
file is properly configured for Laravel and Docker:
.env Example:
APP_ENV=local
APP_DEBUG=true
APP_KEY=base64:YOUR_APP_KEY
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=secure_password
QUEUE_CONNECTION=redis
REDIS_HOST=redis
REDIS_PORT=6379
Run the following command to build and start the services:
docker-compose up --build
Service | Container Name | Port |
---|---|---|
Laravel API | api |
N/A |
Nginx | api-web |
8080 |
MySQL | api-db |
3306 |
Redis | api-redis |
6379 |
phpMyAdmin | api-phpmyadmin |
8081 |
.
WorkSpace Directory
├── docker-setup # Custom Docker setup files
│ ├── php # PHP-specific configurations
│ │ ├── Dockerfile # PHP Dockerfile
│ │ └── local.ini # Custom PHP configuration
│ ├── mysql # MySQL setup
│ │ ├── init.sql # SQL script for database initialization
│ │ └── my.cnf # Custom MySQL configurations
│ └── nginx # Nginx setup
│ │ └── api.conf # Nginx virtual host configuration
│ ├── docker-compose.yml # Defines services and configurations
├── emr-php # any Laravel application code (mounted as a volume)
└── README.md # Documentation (this file)
- Environment Variables: Configure application settings without modifying code.
- Health Checks: Ensures services are running correctly using Docker's
HEALTHCHECK
. - Volumes: Persist data for MySQL and mount application code for live development.
- Service Isolation: Separate containers for each service (API, database, cache, web server).
docker-compose down
docker-compose logs -f
docker exec -it <container_name> bash
docker exec -it api php artisan cache:clear
- Use Secrets for Sensitive Data: Store credentials securely using Docker secrets.
- Database Backups: Regularly back up your MySQL data from the
db-data
volume. - Resource Optimization: Tune PHP-FPM, Nginx, and MySQL configurations for better performance.
- Health Checks: Ensure services are healthy using Docker's
HEALTHCHECK
directives. - Logs and Monitoring: Use tools like Datadog or ELK stack to monitor container logs and performance.
- Ensure the
.env
file matches the MySQL container credentials. - Check the MySQL container logs for errors:
docker logs api-db
- Ensure proper permissions for the mounted Laravel code (
emr-php
). - Set ownership to
www-data
for Laravel files:docker exec -it api chown -R www-data:www-data /var/www/html
Feel free to submit pull requests for improvements or report issues on the GitHub repository.
This project is licensed under the MIT License. See the LICENSE
file for details.