Getting Started with Laravel and Docker

January 4th, 2018

After reading the Laravel docs and a few blog posts, and even watching a Laracast or five, I got tired of trying to implement Homestead, Valet, or any other system.

I decided Docker would be the better option to start my Laravel journey. I mean, why use Homestead when it's basically Vagrant, and we all know how resource intensive it is compared to Docker. Rather than installing Vagrant, VirtualBox, and Homestead -- we'll get up and running with Laravel only using Docker.

These instructions may slightly differ for Windows or Unix based systems.

Install Composer

Download Composer, the instructions are on their site. Then follow the instructions on making composer global.

Install Laravel

You can install Laravel two ways, the simple way and the marriage route. The simple way uses a long Composer command, the second way involves making a commitment and installing Laravel globally on your computer.

If you're just testing the waters, I'd recommend the first option.

With Composer

You can install a new Laravel project with the following command:
composer create-project --prefer-dist laravel/laravel project-name

'project-name' is the name of the new project folder you want created.

Going Global

You can also install Laravel globally to easily access the CLI. Pop open Terminal and let's get started:

  1. composer global require "laravel/installer"
  2. vim ~/.bash_profile
  3. Add this line to the bottom of the document: export PATH="~/.composer/vendor/bin:$PATH"
    3a. If you can't type, try copy and pasting. Once you're in the 'typing mode', you'll be able to type and delete.
  4. Press escape to exit typing mode, then type :wq to save and quit vim.
  5. Refresh your file using the following command: source ~/.bash_profile
  6. Try running laravel and see if it's installed globally (if not, go back to step 3 and try again)

Once Laravel is available globally, you should be able to run laravel new project-name to make a new Laravel project called 'project-name'.

Stop! Do you need Docker?

%[https://gfycat.com/WastefulFirsthandColt]

Is this a simple project? Are you just testing the water with Laravel, and haven't dived deep into Docker?

Try running this in Terminal:
php artisan serve

Your website will be live at http://localhost:8000/. Try this out and see if this is enough for you. Unless you use special PHP packages (and don't install them locally), you might not need to make the jump to Docker.

But if you find yourself in need of a certain package, or you simply want a contained ecosystem for all your server interactions (from PHP to Redis to MariaDB) -- we've got you covered.

Install Docker

  1. Go to Docker and download the community edition.
  2. Install Docker.
  3. Download this docker-compose.yml and place it in your project root
  4. Open up the docker-compose.yml and change 'app-name' to your desired project name. This is just the name of the container, it doesn't have to match the folder.
  5. Run docker-compose up

Docker should install everything from the server images and PHP, to running Composer's install. Once it's finished, your site should be live at http://localhost:3000/

Working with Docker

The only caveat of working within a docker container is running command lines. Working with Laravel, you'll find yourself running a lot of commands. So if you'd like to run a command within your newly created Docker container, type docker-compose exec project-name before each command.

Try some of these out (or save as snippets!):

  • Run SSH commands: docker-compose exec project-name <command>
  • List all artisan commands: docker-compose exec project-name artisan list
  • List all registered routes: docker-compose exec project-name php artisan route:list
  • Make a controller: docker-compose exec project-name php artisan make:controller ShopController

Simple as that

It's hard to go back once you learn the simplicity and efficiency of Docker vs solutions like Vagrant or even LAMPs.


Keep Reading: