So, I’ve been using Laravel with docker-compose for development for a while, and now I’m going to make a simple yet functional project skeleton based on a classic laravel/laravel package, and I’m going to document the whole process here.
This article is targeted at PHP devs that use or want to use Laravel, and are also using or willing to use containerized environments. I will also provide some tips & tricks for PHPStorm users, and in a follow-up post I will show how you can deploy this app to Google Kubernetes Engine in Google Cloud.
I will suppose you have docker installed, as well as docker-compose. Some acquaintance with command line, Git and the wonderful Composer package manager is also a must. I will also suppose you have PHP installed locally. I am using a Linux laptop, things are going to be pretty much the same on Mac, and hopefully on Windows but I’m not sure.
Docker & docker-compose have been around for a while already, but are still rapidly growing in terms of adoption and features. For PHP and LAMP devs, containers allow for running different versions of PHP, MySQL, Apache/Nginx for different projects, as well as any other databases, caching servers etc. Running your project with docker-compose makes it isolated from the rest of your machine, and gives your the power to start/stop the whole project with a simple shell command. Read more about benefits of docker-compose for development in this blog post.
First thing we’ll do is create a Git repository. For that, I will use Gitlab project hosting. As soon as the project is created (I created an empty project, without even a README), I can clone the repository from the terminal. Then I am going to pull everything from laravel/laravel, to my repo:
# SHELL (TERMINAL) #
$ git clone firstname.lastname@example.org:crocodile2u/laravel-docker.git
$ cd laravel-docker
$ git remote add upstream https://github.com/laravel/laravel.git
$ git pull upstream master
$ git push origin master
Now the contents of the project folder should be familiar to you, if you have ever used Laravel.
We’re going to need composer dependencies and perform the basic setup for a Laravel app:
# SHELL (TERMINAL) #
$ composer install --ignore-platform-reqs
$ cp .env.example .env
$ php artisan key:generate