🚀 Installing PHP with Laravel Sail and Docker
Laravel Sail is a powerful, lightweight command-line interface that provides an effortless way to set up a PHP development environment using Docker. This tutorial will guide you through installing PHP with Laravel Sail, configuring Docker, and running your setup seamlessly.
📑 Table of Contents
- Prerequisites
- Step 1: Install PHP with Laravel Sail
- Step 2: Verify the Setup
- Step 3: Running PHP with Docker
- Managing Docker Services
- Understanding Docker PHP Environment
- Troubleshooting
- Conclusion
- Additional Resources
🛠️ Prerequisites
Before you begin, ensure you have the following installed on your machine:
Tool | Description |
---|---|
🐳 Docker | Download Docker and ensure it's running. |
🌐 cURL | A tool to transfer data from or to a server. Most Unix-based systems have it pre-installed. |
🖥️ Terminal | Access to your system's command-line interface. |
🔧 Step 1: Install PHP with Laravel Sail
Laravel Sail simplifies the setup of a PHP environment with Docker. Follow these steps to get started:
1. Open Your Terminal 🖥️
Navigate to the directory where you want to install your Laravel application:
2. Run the Laravel Sail Installation Command 🚀
Execute the following command to create a new Laravel project with MySQL and Redis:
📌 Note: Replace
example-app
with your desired project name.
3. Project Structure Overview 📂
After running the command, your project directory will look like this:
example-app/
├── app/
├── bootstrap/
├── config/
├── database/
├── docker/
├── public/
├── resources/
├── routes/
├── storage/
├── tests/
├── vendor/
├── .env
├── docker-compose.yml
├── composer.json
└── ...
4. Navigate into the Project Folder 📁
5. Start the Application with Docker 🐳
Run the following command to start all necessary Docker containers:
💡 Tip: To run containers in the background, use:
🔍 Step 2: Verify the Setup
Ensure everything is set up correctly by accessing your Laravel application.
- Open Your Web Browser 🌐
- Visit
http://localhost
You should see the Laravel Welcome Page, indicating that PHP and other services are running successfully.
🏃♂️ Step 3: Running PHP with Docker
Now that your environment is set up, here's how to manage your PHP application using Docker:
Starting Containers
Stopping Containers
Checking Container Status 👀
Example Commands Table 📋
Action | Command | Description |
---|---|---|
Start containers | ./vendor/bin/sail up |
Launches all Docker containers |
Start containers in background | ./vendor/bin/sail up -d |
Runs containers in detached mode |
Stop containers | ./vendor/bin/sail down |
Stops and removes containers |
View running containers | docker ps |
Lists all active Docker containers |
Restart containers | ./vendor/bin/sail restart |
Restarts all Docker containers |
🔄 Managing Docker Services
Laravel Sail provides straightforward commands to manage your Docker services efficiently.
Common Docker Commands with Sail 🔧
Service | Command | Description |
---|---|---|
Start all services | ./vendor/bin/sail up |
Starts all Docker containers |
Start services in background | ./vendor/bin/sail up -d |
Runs containers in detached mode |
Stop all services | ./vendor/bin/sail down |
Stops and removes all containers |
Rebuild services | ./vendor/bin/sail build |
Rebuilds Docker images |
Execute a command in a service | ./vendor/bin/sail exec <service> <command> |
Runs a command inside a specific service |
📌 Example: To execute Artisan commands:
🐳 Understanding Docker PHP Environment
Your Laravel application runs within a Dockerized PHP environment, which ensures consistency across different development and production setups.
Key Components 🛠️
Component | Description |
---|---|
PHP Container | Runs the PHP application with necessary extensions. |
MySQL Container | Manages the MySQL database service. |
Redis Container | Handles Redis caching service. |
Nginx Container | Serves the application via the Nginx web server. |
Docker Compose File 📄
The docker-compose.yml
file defines and configures these services. Here's a simplified view:
version: '3'
services:
laravel.test:
build:
context: ./docker/8.3
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.3/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
...
volumes:
- '.:/var/www/html'
networks:
- sail
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
...
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
redis:
image: 'redis:alpine'
ports:
- '6379:6379'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
🛠️ Troubleshooting
Encountering issues? Here are some common problems and their solutions:
1. Docker Containers Not Starting 🚫
- Check Docker Status:
- Restart Docker: Sometimes restarting Docker resolves startup issues.
2. Port Conflicts ⚠️
- Identify Conflicting Services:
Ensure no other services are running on the same ports defined in
docker-compose.yml
. - Change Port Configuration:
Modify the
APP_PORT
or other port variables in your.env
file.
3. Permission Issues 🔒
- Set Correct Permissions:
- Avoid Using
sudo
with Sail: Running Sail commands withoutsudo
is recommended to prevent permission conflicts.
4. Missing Dependencies ❓
- Install Required PHP Extensions:
Modify the
Dockerfile
to include necessary extensions and rebuild the containers:
🎯 Conclusion
By leveraging Laravel Sail and Docker, you can effortlessly set up a robust PHP development environment complete with MySQL and Redis. This setup ensures consistency, scalability, and ease of management across different stages of development and deployment.
📚 Additional Resources
- Laravel Sail Documentation: https://laravel.com/docs/sail
- Docker Documentation: https://docs.docker.com/
- Laravel Official Website: https://laravel.com/
- Docker Compose Reference: https://docs.docker.com/compose/compose-file/
- PHP Official Documentation: https://www.php.net/docs.php