The perfect kit starter for a Laravel project with Docker and PHP 8.2
This blog post is an introduction to devs who want to start using Docker with Laravel. It will guide you through creating a Laravel project running on Docker.
Before we start, you’ll need to install Docker CE, Docker Machine, Docker Compose in your machine.
Install the latest Docker CE here.
Install the latest Docker Compose here.
However, if you’re lazy like me, just use this command to make sure it’s installed.
docker --version
Docker version 23.0.1, build a5ee5b1
docker-compose --version
Docker Compose version v2.12.2
Once all of that is out of the way, we can start with the Laravel project and the Docker environment that will run it.
I’m creating a Laravel project from scratch and I want to set up a development environment with Docker.
Step 1: Create a new folder for your new project.
mkdir docker-laravel
Step 2: Download the latest D4D docker version from our repository.
A couple of years ago, we created a D4D project that allows you to quickly launch a Laravel project, which we will use in our Laravel project too.
It can be downloaded from here.
wget https://github.com/StaffNowa/docker-symfony/releases/download/v2.0.12/d4d_linux_amd64.tar.gz && tar xzvf d4d_linux_amd64.tar.gz && rm d4d_linux_amd64.tar.gz
Step 3: Create the Laravel project.
First off, let’s start docker containers with the project we just downloaded.
# cd to the location where you cloned the project
cd docker-laravel
# create and start a container
./d4d start
This command starts the containers.
Docker should start building (if it’s the first time for these images) and running with the containers in the background.
We will need to create the Laravel project inside the php image bash.
./d4d exec php
Once in there, we’re in a PHP8 image, so we should be able to create a new Laravel project via the composer create-project command.
# inside php bash
composer create-project laravel/laravel .
Step 4: Open the project in your web browser.
Your application will be accessible in your web browser at http://symfony.local.
Finally, let’s restart the containers via the command line.
./d4d stop && ./d4d start