Perspective Unspoken

My perspective on Git, Docker, Python, Django, PHP and other stuff

A simple way to setup Laravel with Docker

Laravel, from what I’ve read is a great framework for PHP. I’ve heard it’s fast, small and many other things. I decided to give it a try on my foray back into the PHP world. On visiting the Laravel website and scrolling to the Installation I realize they’ve got a nice virtual machine available for you to download and get started developing for Lavarel.

Well, I’m a lil old school and probably ought to be blamed for trying to re-invent the wheel or just plain ol’ lazy… I’ve been using Docker and frankly, I’m sold… I abs love Docker! Hate the idea of having to boot up a machine after booting up my machine so I can start working. Too much precious time wasted. And… since recently, even Vagrant via Virtual Box has been too slow for my liking.

Next, I realized that Laravel has setup for Docker! Yup, they’ve got a nice fat repo called LaraDock that comes with Laravel and Dockerfiles and PostgreSQL and PHP and Nginx and Apache and Beanstalk and Elasticsearch and MariaDB… you get the point. I was a little overloaded and didn’t want to take the “trust me… it works” approach. I wanted to start with a very basic Docker setup for Laravel without all the bells and whistles.

If you’re like me… coming from another discipline, back to PHP but know how Docker works and you’re perhaps vaguely familiar with PHP’s Composer, then this guide is for you. I’m going to show you how to setup a Dockerfile and docker-compose.yml file for a basic Laravel setup. You can also clone the config from my online at https://github.com/jaywhy13/laravel-docker.

Ok, so let’s look at the Dockerfile first.

I’m using a PHP 7 docker official image for the installation. Laravel will work with PHP >= 5.6.4, of which 7 is a fine candidate. You can always use a different image version of PHP, just check the PHP page in the Docker Hub.

First we install some libraries that Composer needs to do it’s installation. Composer is PHP’s new package manager. It can be used to install several packages, and frameworks. Laravel happens to be one of the frameworks that are installable using Composer. In order to install packages Composer uses the zip libraries to uncompress downloaded packages. So we start by installing the apt requirement and enabling the PECL extension using docker-php-ext-enable .

The docker-php-ext-enable  is a binary provided by the PHP docker image that can be used to manage PECL extensions. Then we use PHP to download a PHP that is used to install Composer itself. When we pass --filename=composer  we’re telling the script to call the Composer binary “composer”.

The global require  call updates our composer.json  file with all the required packages for Lavarel. Every time require is run it installs the added/modified requirements. Except, if we supply the --no-updates  flag, which we did… and I’ll tell you why in the next step.

Finally, the Dockerfile copies over our code to /code . /code  is the working directory. If we previously installed the requirements, this line would overwrite all that hard work. So, we opt not to do any installations just yet. Let’s move on to the compose file.

 

This file is pretty simple, and has three containers (web, db and redis). The redis and db containers are straightforward. They just use an image. For the db container we supply the username, password and name of the database we want to access in the image. Finally, our web containers exports port 8000, creates a volume from our current directory, mounts it at /code  then runs the php artisan serve  command to get our server running.

Before we bring up the machine we need to run composer’s installation. In order to do this, bring up the web machine and run composer.

That’s it! Remember my repo has this config at https://github.com/jaywhy13/laravel-docker.

Happy coding!

 

jaywhy13 • October 18, 2016


Previous Post

Next Post

000webhost logo