DEV Community

Jorge Santos
Jorge Santos

Posted on • Updated on

How I deploy Laravel apps in Docker with just two commands

Hi there! It's Jorge. It's been a while since my last post, and this one might sound the same as one that you might have read around here. But come along and I promise you that you never again will have to build docker images from scratch to your Laravel projects!

⚡TL;DR

Tired of configuring Docker from scratch for every Laravel project? This is the link to the Github repository for the Laradocker project, a brand new composer package that sets up your Laravel project with Docker with just these two simple commands:


# Run these commands inside your Laravel project:

composer require --dev jdsantos/laradocker

php artisan laradocker:install

# That's it! Follow the instructions and you are done!

Enter fullscreen mode Exit fullscreen mode

On your way there, please leave a star ⭐ on the repo to show your support.

Thanks in advance ❤️

📜The backstory

In my previous post, I brought you LANPOD a Laravel/Docker deployment strategy that allows you to deliver a Laravel app on-premise or cloud environments relying on a battle tested tech recipe consisting of laravel + alpine linux + nginx + php-fpm + opcache + docker you absolutely loved.

This recipe allows you to deploy your app in a redistributable, virtualized, os agnostic, self-contained and self-configured software image and run it in virtualization engines such as Docker or Podman. It even includes things out of the box like the supervisor's tidy configuration for handling your queues, nice defaults for php, opcache and php-fpm, nginx, etc.

All good, but...

😵The pain

Something was off.

While LANPOD as a template repository on Github was a helpful starting point for a brand new Laravel app, and a LOT better than having to go about wiring virtualization over and over again on the same structured apps, it was completely useless to an already existing/legacy project. You would have to MANUALLY extract the recipe’s files and directories of the bunch of other stuff, and adapt them to your needs. As it was meant to be a template-only repository, it didn’t include right out of the box any native database support (because if it did, it would have to include all of them in the same image), and if an update to the configuration was needed, it would be hell on earth just to know how and what to change in your project.

💊The solution

Having felt this pain myself enough times already, especially when having to add by hand the database dependencies to the Dockerfile/Containerfile over and over again, I realized: how cool would it be to have this recipe as a dependency in my projects, in such a way that is easy peasy to install, uninstall and update in the future?

"That's it! I'm going to create a Laravel package that I can install in my projects! Let's do this!"

Adventure gif

So, the adventure began!

I started to scaffold my brand new composer project with all required dependencies to develop and test Laravel packages including orchestra/testbench, laravel/pint and larastan/larastan.

After that, I set up right from the start a Github repository for the project, auto-publish capabilities to Packagist, the PHP composer package repository, and spawned Github actions for a super simple CI/CD pipeline to run my tests at each push.

With all this in place, the rest was peanuts: I used all of LANPOD's files as mere stubs for this project, and used the plain Artisan Console to develop a pretty silly and basic UI for installing this recipe:

Terminal UI

After launching Laradocker inside your project, the installer will guide you through the necessary steps to build the proper dependencies in the image, such as database connectivity support and you are done!

Databases

All files get generated in an instant and copied into your project!

👨‍💻The code

This package is completely free & open-source and you can find the source code here!. On your way there, please leave a star ⭐ on the repo to show your support.

Thanks in advance ❤️

💻 Environment

This was the environment that I used:

Hardware

  • Intel i7-8750H
  • 24 GB of RAM
  • SSD 250GB

Software

  • Windows 11 Enterprise
  • Docker Desktop with WSL backend
  • Visual Studio Code

Hope this helped you in any way.

See you soon! 👋

Top comments (0)