Creating a new Laravel application with Docker is streamlined using Laravel Sail, which provides a default Docker development environment.
Install Docker Desktop:
Ensure Docker Desktop is installed and running on your system (macOS, Linux, or Windows with WSL2).
Create a New Laravel Project with Sail:
Open your terminal or command prompt and execute the following command to create a new Laravel project named my-laravel-app with Sail integrated:
curl -s "https://laravel.build/my-laravel-app" | bash
Replace my-laravel-app with your desired project name. This command downloads and executes a script that sets up the Laravel project and its associated Docker configuration (including docker-compose.yml and the sail script).
cd my-laravel-app
./vendor/bin/sail up
Alternatively, you can add sail as an alias to your shell's configuration file (e.g., .bashrc, .zshrc) for easier access:
alias sail='bash vendor/bin/sail'
After setting the alias, you can simply run:
sail up
This command will build the necessary Docker images (if they don't exist) and start the services (PHP, MySQL, Nginx, etc.) in the background.
Access Your Application:
Once the containers are running, you can access your Laravel application in your web browser by navigating to http://localhost.
Run Artisan Commands:
You can execute Laravel Artisan commands within the Docker environment using sail:
sail artisan migrate
sail artisan make:controller MyController
This setup provides a fully isolated and consistent development environment for your Laravel application using Docker.