Laradhoc - Dockerize your Laravel app. Be a happy dev
Tags: laravel, docker, php, github
Published: 2020-03-31 20:48:24

![Laradhoc](https://user-images.githubusercontent.com/6959298/78071513-f83c9100-739d-11ea-9ff5-c63c1843bb3b.png)

⚠️ This is my first post *ever*. Any suggestion will be highly appreciated ♥️

# tl;dr

[Laradhoc](https://github.com/eleftrik/laradhoc), hosted on **GitHub**, is my basic **Docker**-based development environment for running **Laravel** applications.

Check it out and feel free to try it, do a fork, open an issue or a pull-request. I'll be happy to hear your suggestions, fix my mistakes and learn something new 😄

# Before Laravel
I'm a **Laravel** developer and enthusiast since version 4.
I love it. 
Before Laravel, there was **CodeIgniter**... And even before there was pure PHP ⚠️. If I go back up to the origins (speaking of PHP development), I remember a lot of procedural code, big tons of spaghetti 🍝, manual `include`, countless unaware vulnerabilities, dirty mixtures of PHP / HTML and Javascript, etc. 🙈
No OOP, no Composer, no PSR-2, no frameworks, no SOLID, design patterns, unit tests. 🤦🏻‍♂️
Dependency injection, service container, zero-downtime deploy, caching, CLI tools, queue workers... What? 🤨
A looooot has been changing during these years and it's still changing, with increasing speed.

> *Y A W N* 🥱 You're talking about code, not development environments

You're right. Let's go back in topic.
Basically, during my PHP developer life, I went through these stages. I try to summarize them.

# A quick (?) history of (my) dev environments

### 1 - Manual / WAMP

Install everything on your host, literally "by hand".
I used to work on a Windows pc and I manually downloaded a zip file with the PHP version I needed. Same for Apache. No CLI. Just a Windows service and a bunch of config file to edit.

Need to update? Repeat the same steps. And hope nothing is gonna break 😵

To be honest, the technology stack wasn't still changing so fast. So it wasn't such a big deal.

Oh, of course there were those "packaged solutions" like WAMP, but honestly I wasn't a big fan. They could make your life a bit easier, but I preferred to understand a bit deeply the "building blocks". 

### 2 - Vagrant / Homestead

When I discovered Vagrant, Homestead and these *VM-based* solutions, I remember it was like "WOW 😱 Different configurations for different applications? This is magic!".
Another advantage: the ability to share, in the same dev team, the same virtual machine with its settings, doesn't matter if your host is Windows or MacOS based (*even if I remember it wasn't exactly always like this...*).
But these VMs were pretty huge, heavy and not-so-performant.

### 3 - Docker!

And then there was Docker!
Which is the current solution my team and I use for local development.

# Why Docker?

In my opinion, **Docker** is one of those **life-changing inventions**, such as **GIT**, for example. It's pure genius 🎓.

Don't want to explain here what Docker is, which great benefits it brings and why you should adopt it as soon as you can: there are **tons** of articles which explain all these concepts, much better than I would do.

Let me write only a few words: along with your application code (e.g. your private GIT repository), you also provide (via one or a handful of configuration files) the exact **infrastructure** to run it: which **services** and which **versions**, how are they **configured** and how they **interact** between theirselves. It doesn't matter if you're using **Windows**, **Linux** or **MacOS**. It's almost like *download this, press play, wait... And you're ready to code* 🤓 

The latter aspect is very import for me: I mainly develop on **MacOS** workstations, but also on **Windows 10** (with **WSL**). So, it's very important for me to have a solutions which works **everywhere**.
 
# How I approached Docker?

When I heard about it, at first, I was a bit *scared* 😓.
It was like "it sounds great, but not so easy to learn... And it's another topic to learn, I've no time, Homestead works pretty well, bla bla bla". You know... `excuses`.

During my spare time, I started to read some article, watch some video and try to become acquainted with Docker terminology: images, containers, docker-compose, volumes, etc.
But still I couldn't find a concrete reason to revise my workflow (`laziness`)

Then I started to use some **Docker-based solutions / products** (let me call them Docker *supersets*, *wrappers*), which are **quite easy to setup** and they completely (or almost) hide the underlying complexity (even if it's not so complex) 🚀.

Just a few names:
- [Local by Flywheel](https://localwp.com/)
  Need a quick way to run your **WordPress** blog / application locally? This is a pretty, easy and straightforward solution 
- [Laradock](https://laradock.io)
  A full and highly configurable solution for running **Laravel** applications. But I soon found it to be pretty *intricate* to setup
- [Lando](https://lando.dev)
  Until now, my *favorite Docker-based tool*. You can you use it for **Laravel**, **WordPress** and many other applications. It requires only to define your stack (services, versions, configurations, etc.) within a `.yaml` file, which you're going to commit in your repository. Et voilà! You definitely want to try it.

# Docker the *manual way*, then Laradhoc

What's wrong with the aforementioned products? Nothing!
Ok, almost nothing 😅
For example **Lando**, which I keep on loving, it's not so fast in bootstrapping.
Another downside is this: Lando requires a specific locked version of Docker. I remember that one time, after upgrading Docker (via automatic updates), Lando suddenly broke and I couldn't understand what was going on. After many tests and some searching, a GitHub issue came up so I finally understood I had to downgrade Docker. 
Same for **Laradock**: these products are meant to work out the of box, but soon or later you'll experience a configuration which is working on MacOS but not on Windows (or viceversa), or a particular setup or combination of services / versions which is leading to unexpected behaviours. 

But this isn't the **main reason**. We all know the perfect software doesn't exist (especially the one you've written and you're so proud of 😝) and every product soon or later is going to meet some problem, bug, unexpected behaviour and so no.

So, what's the matter? 🤷🏻‍♂️

After starting to use these products, I became **curious** 👀 and wanted to learn more about Docker. I wanted to experiment by myself, to understand and put into practice all the concepts I've read about in the previous months.
I read more articles, took a look at several open source repositories, then I tried to sum up everything in a project I could use in my everyday developer life.
After some initial doubt, I said to myself:

> Ok, let's try to be "shameless". I want to put my project on GitHub, even if I have little or no experience with open source. What can be wrong? At worst, someone will say "this is wrong!" or "this is how it's done": in both cases, I will learn something.

# Laradhoc

**Laravel *ad hoc* environment**. I just wanted something tailored for my needs.
Of course it's not as complete as, for example, Lando.
But it's **fast** in bootstrapping (after downloading all the images and after the first build, of course) and **enough complete** to run my projects.
I didn't want to be too *strict*, so I included even some service I usually don't need (e.g. phpMyAdmin - I prefer to use a SQL client).

I tried to explain everything in the [README.md](https://github.com/eleftrik/laradhoc/blob/master/README.md); anyway, a very quick overview.

#### Features
- Nginx
- PHP 7.4 with OPCache
- MySql 8
- phpMyAdmin
- Mailhog
- Redis
- Custom domain name (es. http://laradhoc.test or https://laradhoc.test)
HTTP or HTTPS (with self-signed SSL certificate)   

It comes with some useful bash **scripts** to start/stop the containers, run `composer`, `php artisan` and all the main Laravel-related *stuff*. 

Give it a try!

https://github.com/eleftrik/laradhoc

As I already said, I would be happy to know what you think about!
Feel free to ask for some new feature or to suggest some better implementation. 

Thanks for reading and good coding! 🤓