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.
RUN apt-get update && apt-get install -y unzip zip zlib1g-dev
RUN pecl install zip && docker-php-ext-enable zip
# Install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --filename=composer
# Install Laravel
RUN php composer global require "laravel/installer" --no-update
# Mv composer and add it to path
RUN mv composer ~/.composer/vendor/bin/
ENV PATH $PATH:~/.composer/vendor/bin/
# Copy our code
COPY . /code
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.
command: php artisan serve --host 0.0.0.0 --port 8000
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.
docker-compose run web bash
That’s it! Remember my repo has this config at https://github.com/jaywhy13/laravel-docker.