Skip to content

🚀 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

  1. Prerequisites
  2. Step 1: Install PHP with Laravel Sail
  3. Step 2: Verify the Setup
  4. Step 3: Running PHP with Docker
  5. Managing Docker Services
  6. Understanding Docker PHP Environment
  7. Troubleshooting
  8. Conclusion
  9. 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:

cd /path/to/your/projects

2. Run the Laravel Sail Installation Command 🚀

Execute the following command to create a new Laravel project with MySQL and Redis:

sudo curl -s "https://laravel.build/example-app?with=mysql,redis" | sudo bash

📌 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 📁

cd example-app

5. Start the Application with Docker 🐳

Run the following command to start all necessary Docker containers:

./vendor/bin/sail up

💡 Tip: To run containers in the background, use:

./vendor/bin/sail up -d

🔍 Step 2: Verify the Setup

Ensure everything is set up correctly by accessing your Laravel application.

  1. Open Your Web Browser 🌐
  2. 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

./vendor/bin/sail up

Stopping Containers

./vendor/bin/sail down

Checking Container Status 👀

docker ps

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:

./vendor/bin/sail artisan migrate

🐳 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:
    docker ps
    
  • 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:
    sudo chown -R $USER:$USER .
    
  • Avoid Using sudo with Sail: Running Sail commands without sudo is recommended to prevent permission conflicts.

4. Missing Dependencies

  • Install Required PHP Extensions: Modify the Dockerfile to include necessary extensions and rebuild the containers:
    ./vendor/bin/sail build --no-cache
    

🎯 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