Once upon a time…
… I used to develop my applications in a Vagrant/Homestead development environment. It worked pretty well, I remember using it for a lot of projects, but soon performances decreased, especially in large projects/codebases.
Sure, I was able to make some optimizations and tweaks here and there, but it was mostly a palliative. I knew I needed something different.
Then, Docker arrived. So I started my research of a cool Laravel application development environment.
I loved the way Docker made everything easier regarding development environment organization. Sure, it has a very steep learning curve (especially at the beginning), but it was worth it.
The next step was quite obvious: find a set of scripts to make the provisioning of a new dev environment as easier as possible.
Why? Simple: I like to try/test new packages and techniques on Laravel. Well, to be more specific, I have to say that I want to explore them in a new, fresh Laravel application.
So, I discovered Laravel Vessel, an exciting project you can find here. What I love about Vessel is the minimalism of its footprint and provisioning. It just has the very basics:
- PHP 7.2
- MySQL 5.7
- Redis;
- NodeJS with NPM, Yarn, & Gulp;
However, I wanted to find a way to use it without having PHP installed locally. The dedicated documentation page helped me with a specific script. Everything worked so I asked myself: what if I try to automate it in a simple shell script?
Something that I can call with something like “myscript myproject” and:
- creates a new Laravel project in the myfolder folder;
- adds a new record to the hosts file, to reach my project with http://myfolder.test;
WITHOUT the need for anything on my local machine. Only exception: Docker and Docker Compose.
So, after a couple of hours on it, I finally made it! I put it on Github.
Here’s the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#!/usr/bin/env bash # LaraPrep, a simple script for a lazy developer. # Wrote by Francesco Malatesta - Dec 10, 2016 - Last Update Oct 27, 2018 function docker_prep { # export current user id USER_ID=$(id -u -r) USER_NAME=$(whoami) echo "127.0.0.1 ${PROJECT_HOSTNAME}" | sudo tee -a $HOSTS_PATH > /dev/null docker run -u $USER_ID --rm -it \ -v $(pwd):/opt \ -w /opt shippingdocker/php-composer:latest \ composer create-project laravel/laravel $PROJECT_NAME docker run -u $USER_ID --rm -it \ -v $(pwd):/opt \ -w /opt/$PROJECT_NAME shippingdocker/php-composer:latest \ composer require shipping-docker/vessel docker run -u $USER_ID --rm -it \ -v $(pwd):/opt \ -w /opt/$PROJECT_NAME shippingdocker/php-composer:latest \ php artisan vendor:publish --provider="Vessel\VesselServiceProvider" cd $PROJECT_NAME bash vessel init ./vessel start printf "\nConfigured ${PROJECT_HOSTNAME}! Have fun :)\n" } if [ $? -ne 0 ]; then exit 1 fi if [ -z "$1" ] then echo "ERROR: Please specify a name for the project folder." exit fi PROJECT_NAME="$1" PROJECT_HOSTNAME="$1.test" HOSTS_PATH="/etc/hosts" docker_prep |
Nothing special, after all. This shell script defines a docker_prep function that:
- initializes some variables with the user id and name;
- echoes a new line into the hosts file, to add a new .test record in it (with the same project name we chose);
- creates a new project using Docker and the shippingdocker dedicated image;
- adds Vessel as a dependency;
- calls the artisan vendor:publish Artisan command, passing the Vessel provider;
- initializes the development environment;
With a one-line instruction like
1 |
$ laraprep myapp |
you can now prepare the perfect development environment for your next application.
Enjoy it! You can find LaraPrep here on Github.
Note: if you have any feedback to improve this script, feel free to comment below or make a PR on Github 😉