If you are PHP developer, chance you know Laravel are high. Laravel is one of the most popular PHP Framework today. Getting started with Laravel is easy, make sure you have meet all requirements, and
composer create-project --prefer-dist laravel/laravel something .
But to meet all requirements is something else, if you are using Windows and XAMPP, it is hard to fulfil all the requirements, you have to manually install missing PHP extensions, or you are Mac person or Linux version, having installed different version of PHP, and the brand new Laravel version require you to upgrade your PHP into PHP 7.2, but you don’t want to mess with your old project that still running on PHP 5.4. What should you do?
I know, install Laravel Homestead, sounds perfect, right? Laravel Homestead powered by Vagrant, or virtual machine, so you just have to download Vagrant, download Laravel Homestead Vagrant box, run it, the VM is alive, there you go!
Yes, it will run, but I’d rather say it is overkill! You don’t have to emulate the whole machine to run just one project. You can actually share your computer resource to run your project, rather than you have to allocate some amount of your computer resource. Use Docker instead !
What will we do?
If you haven’t already know Docker, think Docker like a smart virtual machine. Docker will create our development image (container) and run it like a virtual machine. Our container will contains OS, PHP, and Nginx.
Building the Container
First of all, we will make a script to provision our container, create a new file called Dockerfile in one of your folder e.g. /home/yourname/dockerfiles. Copy-paste the following script to your Dockerfile file. We will use Alpine Linux as our base image because of its small size (~5 MB, compared with ~80 MB as in ubuntu base image).
Then create a new file called default.conf in the same folder as our previous Dockerfile (/home/yourname/dockerfiles), which it is the Nginx configuration for our Nginx server. Copy-paste the following config to your default.conf file. We will replace default Nginx configuration in our container with our Laravel Nginx configuration.
Make sure Docker is installed and running, then hit following command to create our container image, we named it homestead
docker build -t homestead .
*) Notice the dot (.) at the end of the command, dot represents current directory
Wait for a moment until the image creation completed. You can check the already created images with following command, you will see homestead images within the list.
Install Laravel with Composer, make sure you already installed Composer in your computer.
composer create-project --prefer-dist laravel/laravel homestead
Wait until Laravel finished installing.
Share Project Folder with Container
Our container is finished, our environment is ready, yeaaay 🎉 . Now we are ready to code, but connect our newly created Laravel project to our container first. What we will do is as follow :
- Start homestead container
- Open port 5000 in our computer, connect to port 80 in our container
- Share our project folder (/home/yourname/projects/homestead) with our container’s default nginx project folder (/var/www/html)
Hit your terminal with this command :
docker run -d \
-p 5000:80 \
-v /home/yourname/projects/homestead:/var/www/html \
Our container will run in background and now you can check http://localhost:5000.
Start Making Changes
You can now edit routes/web.php, change
return ‘Hello World’;
Save changes and reload your web browser
Nice, now we are ready to develop !
If this post is helpful, show your support by clicking 👏 a few times as many as you like.