Install Laravel Framework 2024 with Docker Compose
Hello Fellow Human Being ...
There are a lot of development Frameworks in the Software landscape. At the core of each and everyone of them; is the ability to quickly and easily develop a project to completion without the heavy lifting involved with building from scratch.
Laravel is one of those popular Frameworks that varied companies and developers use to build large projects.
In this tutorial we'll be setting up a Laravel project using containerization. Docker will be our containerisation tool and we'll use Docker Compose as our container orchestrator.
Docker and Docker Compose Setup:
This setup will be for Unix based system, in our case we'll be using MacOs.
We start by creating our folder structure for our project.
Let's create our main folder. We'll name our our project laraset
mkdir lara
cd laraset
mkdir DevApp
mkdir DevSQL
touch docker-compose.yml
Let's create the Dockerfiles for the app and the DB separately.
cd DevApp
touch Dockerfile
cd ../
cd DevSql
touch Dockerfile
The App Dockerfile within the DevApp folder should look like this:
FROM amd64/ubuntu:latest
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y
RUN apt-get upgrade -y
RUN apt-get install -y apache2
RUN apt-get install -y php
RUN apt-get install -y php-dev
RUN apt-get install -y php-mysql
RUN apt-get install -y libapache2-mod-php
RUN apt-get install -y php-curl
RUN apt-get install -y php-json
RUN apt-get install -y php-common
RUN apt-get install -y php-mbstring
RUN apt-get install -y composer
RUN curl -s "https://packagecloud.io/install/repositories/phalcon/stable/script.deb.sh" | /bin/bash
RUN apt-get install -y software-properties-common
COPY /DevApp/vhost.conf /etc/apache2/sites-available/000-default.conf
CMD ["apachectl","-D","FOREGROUND"]
RUN a2enmod rewrite
EXPOSE 80
EXPOSE 443
The MySQL Dockerfile within the DevSQL folder should look something this:
FROM mysql:latest
ENV MYSQL_ALLOW_EMPTY_PASSWORD='yes'
EXPOSE 3307
Now for the final touch, We'll complete our docker-compose.yml file the root directory:
version: '3'
services:
devapp:
platform: linux/amd64
build:
context: ./
dockerfile: DevApp/Dockerfile
ports:
- "80:80"
- "443:443"
volumes:
- .:/var/www/html/
environment:
- VIRTUAL_HOST=laraset.local
devsql:
platform: linux/amd64
build:
context: ./
dockerfile: DevSQL/Dockerfile
ports:
- "3307:3307"
environment:
MYSQL_ROOT_PASSWORD: ''
MYSQL_ALLOW_EMPTY_PASSWORD : 'yes'
restart: always
volumes:
- ./db_docker:/var/lib/mysql
We are now ready to witness the magic of Docker Compose and Docker in action. When you're ready get into the root direcrtory where the docker-compose file is and run this command:
docker-compose build
This command will tell docker-compose to build your project as well connect the two containers to each other.
This will take a little while as it'll be fetching the images and building them in the correct spaces for your software.
Now let's get your project up:
docker-compose up
This will bring up your project and run the docker containers you've build.
Before we move forward, let's ensure that we have added our planned URL to our systems hosts
cd ~
nano /etc/hosts
Add the host laratest.local in there and close
The next step we'll do within your containers . Let's start within our DevAPP container and setup our Laravel
docker exec -it <pod-id>
Installing Laravel in the /var/www/html/ is very easy from here on whilst inside the pod:
To Install Laravel you can follow the given commands from the Laravel site: https://laravel.com/docs/11.x/installation#creating-an-application
We have already installed composer in our containers, so installing Laravel is made a lot easier: https://laravel.com/docs/11.x/installation#installing-php
The documentation will take you through creating the application, ans setting the DB configs needed to connect to your SQL DB:
Now we enter the sql pod and create the DB we mentioned on our .env settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
docker exec -it <sql-pod-id>
You can create your DB here with the settings chosen for Laravel app.
Once this is set. You can run run easy
php artisan migrate
And your app will be complete.
From this , you can just load laratest.local in your favourite browser and you are done.
"Laravel is one of those popular Frameworks that varied companies and developers use to build large projects."
The Village Geek
"Alright, alright, alright ..." - Matthew McConaughey
Well there you have it. Docker Compose working together to get your app setup very quickly. Affording you the opportunity to maintain the same codebase build across multiple machines when developing.
Go Crazy!!
Talk to you next time ...
Cape Town, South Africa