The purpose of this post is to provide a step-by-step tutorial on how to run your Laravel application(s) using Docker. The main reason being app development today requires much more than just writing code. Docker abstracts the complexity of integrating with multiple frameworks, architectures, and interfaces through the use of containers that allow developers to isolate their app from its environment. This is extremely handy when you need to deploy your application accross different stages (dev, qa, prod) or to test against different versions of your dependencies (e.g. php8.0 vs php8.2, MySQL v13 vs MySQL v14) rapidly.
Step 0: Installing Docker on your host machine
For the purpose of this tutorial I will assume that you want to set up your development environment on a personal computer and not the cloud. You will first have to install the Docker engine by following this link to the official Docker website. I recommend you install the Docker Desktop version provided for Linux, MacOS, or Windows. The instructions are rather straightforward but if you are using Windows OS you also have to install the Windows Subsystem for Linux (WSL).
Step 1: Creating a Laravel project
If you already have an existing Laravel app feel free to jump to Step 2. Otherwise, you'll want to create a new Laravel application to work with. Before running the below command you'll want to make sure you have Composer installed. Composer is a tool for dependency management in PHP and is integral for working with Laravel. You should substitute 'docker-examle' to your own project name if warranted.
composer create-project laravel/laravel docker-example
Step 2: Adding the Laravel Sail package
Once you have created your project, go ahead and open the composer.json
file located within the root folder. At the time of this publication, Laravel automatically adds the "laravel/sail" package in all new applications created via Composer. If for some reason your composer.json
file does not have a "laravel/sail" entry you can add it with the below command. This GitHub link will help you determine the latest version for the package.
composer require laravel/sail --dev
*This package is only required in the dev environment so don't forget to include the '--dev' flag.
Step 3: Create and modify the docker-compose.yml file
The docker-compose.yml
file can be considered the starting point of your container. The file contains instructions on what images to bring into the container upon creation. In this context, images are templates or snapshots that define application code, libraries, tools, dependencies, and other files needed to make your application run. The below command will present you with a list of options on what images you may want to add. This totally depends on your application requirements but for our purposes I would only include MySQL.
php artisan sail:install
Step 4: Deploying to Docker
Once you have created the docker-compose.yml
file you are ready to create your container. You can instruct Sail to run your application with the following command.
./vendor/bin/sail up
Please take note that we are utilizing an executable file included by Composer and stored in the vendor bin folder. Additionally, if you want to run artisan
commands in the context of the Docker application you will have to use the sail
executable. You can read more about this in the official Laravel documentation.
Step 5: Viewing the application in your browser
This container will encompass your Laravel application and be accessible at your localhost port 80. If you would like to use a different port you can define it in your .env
file by including an entry for APP_PORT=8080
(where 8080 is the intended port).
Eureka
That's it! You have now successfully integrated your Laravel application with Docker. This becomes rather intuitive once you install all of the necessary tools, which is about 90% of the work.