Local Docker dev environment via Laravel Sail
Suggestion: Add Docker local dev environment
Why
In order to make project development on local machine faster, more effective, and easier for people who want to run it, as well as existing developers, so nobody has to install all the dependencies on their local machines and bloat the application space.
What
Laravel Sail is a light-weight command-line interface for interacting with Laravel's default Docker development environment. Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience.
How
The first Sail setup is super easy. I already set it up and tested. So far, all the features I have tested work, but I did not do any deep testing. Part of the bundle is a local Mailhog setup, which works flawlessly and I can register and verify my e-mail.
For running the project, only Docker and Docker Compose will be required.
The rest will require just a few commands:
- Copy
.env.exampleto.env - Install composer dependencies (in order to pull the Laravel Sail bash files, needed only once)
docker run -it -v "$PWD:/app" composer composer install --ignore-platform-reqs
- Run the project
./vendor/bin/sail up -d
- Run DB migrations (needed only once or on migration addition)
./vendor/bin/sail artisan migrate
- Link storage (needed only once)
./vendor/bin/sail artisan storage:link
- Install NPM dependencies (needed only once or on package changes)
./vendor/bin/sail npm install
- Run the NPM dev build
./vendor/bin/sail npm run dev
Output for versions
PHP:
PHP 8.1.5 (cli) (built: Apr 21 2022 10:15:06) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies
with Xdebug v3.1.2, Copyright (c) 2002-2021, by Derick Rethans
MySQL:
Server version: 8.0.27 MySQL Community Server - GPL
NPM:
8.10.0
TODO:
- [x] Introduce Laravel Sail into the project (see #21)
- [ ] Fix known issues
- [x] Modify documentation in README.md
- [ ] Make sure this setup works on other machines aswell (please, whoever can try, leave a comment)
Known issues
- When Vite WS losts connection, it tries to ping Vite for availability, and fails, because it calls
http://localhost/__vite_pingwhereas it should callhttp://localhost:5173/__vite_ping.
URLs
- App available at http://localhost
- Mailhog available at http://localhost:8025
- DB available at localhost:3306 (default user/password found in
.env)
Reminder
This environment setup is not meant or recommended for production purposes. If it is ever needed to use Docker for production, I recommend creating a separate docker-compose.prod.yml file and deploy the app using that file rather than the default docker-compose.yml. If the app is meant to be deployed to a shared hosting, this setup does not collide with that idea.