Blog

September 16, 2016

From Homestead to LaraDock

  • #php
  • #laravel
  • #docker
From Homestead to LaraDock

I had the recent pleasure of having to spend half a day figuring out why my Vagrant box decided to stop working after a software update. Unfortunately, it is not the first time it happened, and it left me feeling annoyed, so I thought: this is it, I'm using Docker.

Our Laravel stack is the last that was to remain for the forceable future on Vagrant, every other new project is to be using Docker.

Now our current project runs on Homestead, the choice Vagrant box for Laravel. Homestead makes the arduous configuration of Vagrant much simpler. If you are dead set on continuing to use Vagrant, I highly recommend it.

While searching for a suitable replacement, it quickly became apparent that LaraDock is the best available option.

LaraDock comes bundled with 12+ images you may need with Laravel, all preconfigured for a very simple initial setup. An important feature that might help brownfield projects transition is that it not only supports PHP 7.0 but also some of the older PHP 5.X versions.

The following section shows the installation and configuration of LaraDock inside our project. If you haven't used Docker yet, the Docker Tutorial should get you up to date.

Install LaraDock

# Installation
git submodule add https://github.com/LaraDock/laradock.git

cd laradock

# Download (if required), build and run the Nginx and MySQL images
docker-compose up -d nginx mysql

Edit the Laravel '.env' file in your project folder

DB_HOST=mysql
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

Note: The config above is the default for LaraDock, you may edit the configuration in laradock/docker-compose.yml to suit your needs.

Setup the database

cd laradock

# Use the bash shell in the 'mysql' container
docker-compose exec mysql bash

# Create our database
mysql -u homestead -psecret -e "CREATE DATABASE homestead;"

# Exit the container
exit

Setup Laravel

# Use the bash shell in the 'workspace' container
docker-compose exec workspace bash

composer install

php artisan migrate

php artisan db:seed

# Exit the container
exit

Et voilĂ ! Our application is running and can now be accessed through 127.0.0.1.

The Laravel commands will have to be ran from the workspace container instead of from within the Vagrant box but otherwise there should be no visible difference between the LaraDock and Homestead setups.

Deployment and production for Docker Compose is very much architecture specific, Using Compose in Production should give you the right pointers.

For a simple non-scalable deployment guide, the Laradock deployment guide for Digital Ocean should do the trick.

After having used this setup for a few months and through a major version change (from 1.11 to 1.12), I can honestly say that LaraDock has made running our project painless in every environment and has significantly simplified keeping our production environment the same as our development environment.

Philippe Trépanier