Powerful Applications with Laravel — Setting up Docker

Gabriel Meireles
dev.meireles
Published in
3 min readMar 9, 2023
Photo by Ishant Mishra on Unsplash

Hello there! I’m really delighted to start talking about Laravel through a series of articles where we’ll cover some important points about this amazing PHP Framework.
First of all, make sure to really know at least the basics of MVC and OOP using PHP, I recommend recapping an overview on these initial subjects.

Throughout this series of articles we’ll learn how to build a robust Laravel application from zero. Let’s cover initial setups as basic MVC, authentication, databases, validations, email services, code refactor, patterns, tests, and a bit more.

A quickly Introduction

Built by Taylor Otwell in June 2011 and currently on version 10, Laravel is a web application framework that aims to provide a great, simple and readable skeleton for your application with features such as Eloquent ORM that offers an interactive object-relational mapper or even the Artisan which provides a number of helpful commands that can assist you while building your application through a command line interface; We also have some important features like Blade as template engine, the Schema facade to deal with your database migrations and Vite to build progressive user interfaces.

Starting with Docker

Shall we start by creating the necessary structure to have an initial executable Laravel setup, back in the days we were used to have local environments provides by EasyPHP, the classical XAMPP (depends on your OS it could be LAMPP or MAMPP) or even WampServer, anyway, let’s use Docker to create some containers to keep our development process fast, consistent and strong allowing us to take advantage of a powerful standardized local environments, so without any furthermore, let’s start!

Make sure to have the Docker setup:

docker --version

If you don’t have it, please follow the Docker installation guide:

You can get a Laravel + Docker setup over here:

In this repository you can found a docker-compose.yml file which is responsible for build the PHP + Ngnix containers

version: '3'

networks:
laravel:

services:
app:
build:
context: ./docker
dockerfile: nginx.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
ports:
- "80:80"
volumes:
- ./src:/var/www/html:delegated
depends_on:
- php
networks:
- laravel

php:
build:
context: ./docker
dockerfile: php.dockerfile
args:
- UID=${UID:-1000}
- GID=${GID:-1000}
ports:
- "9000:9000"
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel

After cloning the repository you must have the following file structure:

├── .
├── docker/
│ └── nginx
│ └── default.conf
│ ├── nginx.dockerfile
│ └── php.dockerfile
├── src/
│ └── LARAVEL FOLDER
└── docker-compose.yml

Briefly, docker folder contains dockerfiles responsible for building the container images installing dependencies and apk necessaries to maintain the runtime, lastly, src contains the Laravel installation where we’ll work building our application.

Wrapping all together

Now you have and know how the setup works, let’s start executing some commands necessaries to run the application, first of all, build and up the containers on mainly folder (where you cloned the repository from git) executing the following command:

docker-compose up --build

It’ll probably take a while to install all dependencies, when done, copy the .env file:

cp src/.env.example src/.env

Then get the current PHP container with docker ps command to list your current containers:

docker ps

In my case the container with PHP is laravel-powerful-apps-php-1, so it’s time to install the Laravel dependencies with Composer:

docker exec -it laravel-powerful-apps-php-1 composer install

And finally generate a key:

php artisan key:generate

After that you must be able to access the application on your browser http://localhost and see the your brand new Laravel setup.

--

--