Skip to content

πŸ“¦πŸž An extremely simple and lightweight Laravel Docker Template

Notifications You must be signed in to change notification settings

RinMinase/laravel-docker-template

Repository files navigation

Laravel Logo

Introduction / Motivation of this project

I'm sick of other extremely complicated and lengthy guides and tutorials on how to create a Dockerized Laravel (see here, here, here and here), so I created one myself. Using this is as simple as cloning or downloading the whole template then running docker-compose up -d, setup can be finished in more-or-less 5-10 minutes.

Using the template

Using Git template

  1. Click Use this template then Create a new repository
  2. Clone your newly created repository
  3. Copy the env file by running cp .env.example .env
  4. Create and run the docker containers by docker-compose up -d
  5. Navigate in the created container docker exec -it php sh
  6. Run the commands below to start your Laravel application
composer install
php artisan key:generate
php artisan migrate:fresh --seed

Using Git clone

  1. Clone the repository by running git clone [email protected]:RinMinase/laravel-docker-template.git
  2. Remove instances of the original repository on your project
  • Open your terminal and run these commands below:
    • git remote remove origin
    • rm -rf .git/
    • git init
  1. Copy the env file by running cp .env.example .env
  2. Create and run the docker containers by docker-compose up -d
  3. Navigate in the created container docker exec -it php sh
  4. Run the commands below to start your Laravel application
composer install
php artisan key:generate
php artisan migrate:fresh --seed

Downloading the repository

  1. Download the repository as a zip file
  2. Copy the env file by running cp .env.example .env
  3. Create and run the docker containers by docker-compose up -d
  4. Navigate in the created container docker exec -it php sh
  5. Run the commands below to start your Laravel application
composer install
php artisan key:generate
php artisan migrate:fresh --seed

Default Values

This project contains 3 docker containers, all of which uses the official docker images in Alpine linux

  • php (which houses your source code)
    • container name: php
  • nginx (which houses the webserver that runs your code in a browser)
    • container name: webserver
  • db (which contains the database of your project)
    • container name: database
    • by default this uses PostgreSQL, but feel free to change it to your desired database

The database by default uses these default parameters to let you get started without even touching any configuration settings

  • DB_DATABASE or your default database name: laravel
  • DB_USERNAME or your default database username: postgres
  • DB_PASSWORD or your default database password: postgres

FAQ

How to update PHP / nginx / PostgreSQL?

  • Updating PHP
  • Updating nginx
  • Updating db
    • Modify the version text under docker-compose.yml under services : db : image, and change the version of the image name
    • Example here:
      image: postgres:15.1-alpine3.17
    • You can find the postgres versions possible here

Why use official images?

Other tutorials or guides uses customized images (see here for the guide -- actual line of code), these images are so hard to update manually compared to just editing a value then rebuilding the whole container again, see the question above on how to update each docker container.

Why use alpine linux?

Alpine linux is very lightweight and secure enough. Since the image used is lightweight, you can quickly download it compared to fully-fledged linux distros such as Ubuntu. (Some references for these: here and here).

Nonetheless, it also has its downsides, (references here and here) I've even encountered a few of them, these are:

  • using other package managers aside from apk
  • some libraries which you may possibly use are not present such as glibc since alpine uses musl-libc and requires a sad and annoying workaround for it to work

Why use static versions for docker containers?

Why not? A variable version might mean that it works on my machine but not on yours because yours has a different (possibly newer) version than mine (references here and here).