After reading the Laravel docs and a few blog posts, and even watching a Laracast or five, I got tired of trying to implement Homestead, Valet, or any other system.
I decided Docker would be the better option to start my Laravel journey. I mean, why use Homestead when it's basically Vagrant, and we all know how resource intensive it is compared to Docker. Rather than installing Vagrant, VirtualBox, and Homestead -- we'll get up and running with Laravel only using Docker.
These instructions may slightly differ for Windows or Unix based systems.
You can install Laravel two ways, the simple way and the marriage route. The simple way uses a long Composer command, the second way involves making a commitment and installing Laravel globally on your computer.
If you're just testing the waters, I'd recommend the first option.
You can install a new Laravel project with the following command:
composer create-project --prefer-dist laravel/laravel project-name
'project-name' is the name of the new project folder you want created.
You can also install Laravel globally to easily access the CLI. Pop open Terminal and let's get started:
composer global require "laravel/installer"
- Add this line to the bottom of the document:
3a. If you can't type, try copy and pasting. Once you're in the 'typing mode', you'll be able to type and delete.
- Press escape to exit typing mode, then type
:wqto save and quit vim.
- Refresh your file using the following command:
- Try running
laraveland see if it's installed globally (if not, go back to step 3 and try again)
Once Laravel is available globally, you should be able to run
laravel new project-name to make a new Laravel project called 'project-name'.
Stop! Do you need Docker?
Is this a simple project? Are you just testing the water with Laravel, and haven't dived deep into Docker?
Try running this in Terminal:
php artisan serve
Your website will be live at http://localhost:8000/. Try this out and see if this is enough for you. Unless you use special PHP packages (and don't install them locally), you might not need to make the jump to Docker.
But if you find yourself in need of a certain package, or you simply want a contained ecosystem for all your server interactions (from PHP to Redis to MariaDB) -- we've got you covered.
- Go to Docker and download the community edition.
- Install Docker.
- Download this docker-compose.yml and place it in your project root
- Open up the docker-compose.yml and change 'app-name' to your desired project name. This is just the name of the container, it doesn't have to match the folder.
Docker should install everything from the server images and PHP, to running Composer's install. Once it's finished, your site should be live at http://localhost:3000/
Working with Docker
The only caveat of working within a docker container is running command lines. Working with Laravel, you'll find yourself running a lot of commands. So if you'd like to run a command within your newly created Docker container, type
docker-compose exec project-name before each command.
Try some of these out (or save as snippets!):
- Run SSH commands:
docker-compose exec project-name <command>
- List all artisan commands:
docker-compose exec project-name artisan list
- List all registered routes:
docker-compose exec project-name php artisan route:list
- Make a controller:
docker-compose exec project-name php artisan make:controller ShopController
Simple as that
It's hard to go back once you learn the simplicity and efficiency of Docker vs solutions like Vagrant or even LAMPs.