I heard about Docker few years ago, but wasn’t know about best practice how to use that. I’ve tried Laradock, it’s way too many stuff in the config, should I use 1 docker for multiple projects? Or better single project?
Create docker-compose.yml in root
Here’s the basic docker-compose file to run php web app
Run it (. src/.env is read .env file’s as env variables)
1
$ . src/.env && docker-compose up -d
Add extra php packages
In order to run Laravel web app, there are some php extensions required. Thus, we need to create a Dockerfile to customize from the base image (php:8.0-fpm)
Basically the idea is, if we have 2nd or 3rd Laravel project, can just use the base image, and duplicate the required extensions for all projects. It also take quite a few minutes to install everything when we run docker-compose up. Base image is like a template, everything is ready, can use in all Laravel projects. Just extends the pre-built custom base image (i.e. jslim/php8-laravel-base in my example here)
Update Dockerfile, change
1
FROM php:8.0-fpm
to
1
FROM jslim/php8-laravel-base:latest
and remove those apt install commands
In production
In production we usually don’t use database in same instance with web app, we can create another docker-compose-production.yml
Remove mysql, and perhaps need to have custom php.ini config, and may be have custom nginx config, SSL cert, etc