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.
- Click
Use this templatethenCreate a new repository - Clone your newly created repository
- Copy the env file by running
cp .env.example .env - Create and run the docker containers by
docker-compose up -d - Navigate in the created container
docker exec -it php sh - Run the commands below to start your Laravel application
composer install
php artisan key:generate
php artisan migrate:fresh --seed
- Clone the repository by running
git clone git@github.com:RinMinase/laravel-docker-template.git - Remove instances of the original repository on your project
- Open your terminal and run these commands below:
git remote remove originrm -rf .git/git init
- Copy the env file by running
cp .env.example .env - Create and run the docker containers by
docker-compose up -d - Navigate in the created container
docker exec -it php sh - Run the commands below to start your Laravel application
composer install
php artisan key:generate
php artisan migrate:fresh --seed
- Download the repository as a zip file
- Copy the env file by running
cp .env.example .env - Create and run the docker containers by
docker-compose up -d - Navigate in the created container
docker exec -it php sh - Run the commands below to start your Laravel application
composer install
php artisan key:generate
php artisan migrate:fresh --seed
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
- container name:
nginx(which houses the webserver that runs your code in a browser)- container name:
webserver
- container name:
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
- container name:
The database by default uses these default parameters to let you get started without even touching any configuration settings
DB_DATABASEor your default database name:laravelDB_USERNAMEor your default database username:postgresDB_PASSWORDor your default database password:postgres
Return to the table of contents
In cases there are any updates to:
- Jobs (found on
app/Jobs) - Commands (found on
app/Commands) - Schedules (found on
bootstrap/app.phpunderwithSchedule)
Please run the following:
-
Navigate inside the
phpdocker container [how] -
Run the command to restart the group (
queue-workerandschedule-worker)supervisorctl restart worker:
Return to the table of contents
This application runs supervisor on the php container. Supervisor runs these tasks:
| Task Name | Group | Command | Description |
|---|---|---|---|
| php-fpm | - | php-fpm |
Runs FastCGI Process Manager |
| queue-worker | worker | php artisan queue:work |
Runs Laravel's Queue worker |
| schedule-worker | worker | php artisan schedule:work |
Runs Laravel's Schedule worker |
To manage the supervisor the commands below can be used:
| Command | Description |
|---|---|
| supervisorctl reread | Re-reads changes in supervisor config files |
| supervisorctl update | Updates supervisor with changes after reread |
| supervisorctl status | Check status of all running tasks |
| supervisorctl start | Starts the task |
| supervisorctl stop | Stops the task |
| supervisorctl restart | Restarts the task |
Please note: Supervisor logs are kept in ./docker/logs/supervisor.log
- Updating
PHP- Modify the version text under
docker-compose.ymlunderservices : php : build : args,PHP_VERSION - Example here:
- You can find the php versions possible here
- Modify the version text under
- Updating
caddy- Modify the version text under
docker-compose.ymlunderservices : caddy : image, and change the version of the image name - Example here: https://github.com/RinMinase/laravel-docker-template/blob/15c9c74cb13e085fd0db2efa83bf9986fded29c5/docker-compose.yml#L328
- You can find the caddy versions possible here
- Modify the version text under
- Updating
db- Modify the version text under
docker-compose.ymlunderservices : db : image, and change the version of the image name - Example here: https://github.com/RinMinase/laravel-docker-template/blob/15c9c74cb13e085fd0db2efa83bf9986fded29c5/docker-compose.yml#L65
- You can find the postgres versions possible here
- Modify the version text under
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.
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
glibcsince alpine usesmusl-libcand requires a sad and annoying workaround for it to work
Caddy is often preferred over Nginx for its simplicity and modern design. Unlike Nginx, Caddy automatically handles HTTPS with free TLS certificates via Letβs Encrypt, removing the need for manual certificate setup or cron jobs (although this is not used in this repository as it was deemed unnecessary).
Additionally, its configuration syntax is way cleaner and extremely easier to understand, making it ideal for quick deployments and maintainable setups.
Regardless, if you do prefer nginx, please refer to the previous setup under this commit:
The files needed for it is likewise under ./docker/nginx-backup/* for the time being. The docker-compose setup is likewise commented as well for now.
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).