In this guide I will teach you to setup your Laravel project for a dev environment. After you finish with this article you will have Laravel running in Docker, without the need to understand Docker as I have prebuild it for you.
Prerequisites
Before you are moving on with the guide, you should have installed Docker. The setup works indifferently on MacOS and Windows and should also work on Linux. You can download Docker here: https://www.docker.com/products/docker-desktop
Setup environment
Make a new project as per according to Laravel documentation:
composer new MyProject
Download my script to your project folder (feel free to open the URL and investigate it):
curl https://raw.githubusercontent.com/EmilMoe/vm/master/vm --output vm
On MacOS and Linux you might have to make the file executable:
chmod +x vm
Configure Laravel settings
Before you build your project, you must change a few things in your .env
. If you change this after you build the environment, the setup won’t work.
Open .env
in Sublime Text or any other text editor and change these variables:
# Set App name, the Docker container will use this name
APP_NAME=YOUR_APP
# Database should be configured to the Docker environment. Database name is optionally but I like to name it after my project
DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=YOUR_APP
DB_USERNAME=root
DB_PASSWORD=secret
Build environment
You are now ready to build your environment:
./vm build
Press Y
to begin the process.
After it’s done, you can browse to http://localhost
to verify that Laravel works.
Using the script
Your Docker instance is already up and running, to stop it again simply run
./vm stop
And to start it
./vm start
There are a few more commands such as composer
and npm
but that would executive them inside the container where they should generally be run outside on your host machine.
To get a list of available commands use:
./vm
You can also read this post on my blog at https://emil.moe/index.php/2019/10/21/laravel-installed-with-docker-in-just-5-minutes/