A Complete Docker based development environment for Laravel & PHP
Ariful Haque
Posted on May 16, 2021
Laravel Docker App is a complete Laravel, PHP Development environment that is very similar to production environment. This has separate nginx, app, MySQL, Redis, queue and scheduler services to make the development environment most similar to a production environment.
There are many other docker images are available in GitHub for Laravel app development. But most of them are either too complicated, very heavy image size, or doesn't allow to run Queue in Redis or manually need to test scheduler by running php artisan command. To overcome these issues, I worked on this Docker Compose structure to run the App, Queue and scheduler in different services where Nginx, MySQL and Redis services are shared where needed.
How to start
For new project.
For new project, first you need to clone the repo from Github
and then need to run sh install from the rood directory of the cloned project. It will download Laravel and unzip in a new directory which is called app.
After the directory is created, need to run the command bellow to build the images.
docker-compose build
A full set of commands to manage docker containers can be found in the Readme.mdfile.
For existing project
For existing project, you need to copy the project directory into the newly cloned directory and rename the project folder name app. If you want to rename the project directory to something else, you need to update the docker-compose file and Dockerfile.dev files in the docker/images/* directory.
Run the containers
docker-compose up
To run the services at background
docker-compose up -d
Image of running containers.
Run docker ps to see the containers running in background.
Run docker stats to see the memory and cpu usages of the containers.
SSH into your app console
To install Laravel, generate app key and run migrations, you need to ssh to your app container service. To ssh to your app container, run this.
docker-compose exec app sh
This will take you to /var/www/app path inside your app service container. Now you can run your composer install and other Laravel or PHP specific commands there.
To ssh into other containers, replace the app with other container name as scheduler, queue. e.g.
docker-compose exec queue sh
Stop running containers
docker-compose stop
Environment Variables
PHP
If you want to change, enable/disable any PHP settings, you can change them in ./docker/config/php/php-ini-development.ini file and then build and up the container again.
MySQL
MySQL username, password can be changed from docker-compose.yml file. Fin the environment section under mysql. Change the value and build the mysql image again with docker-compose build mysql.
After you change the value and build, you need to restart the mysql service docker-compose up mysql to make it affective.
Nginx
If you want to change Nginx web host configurations, you can find the file at ./docker/images/web/sites/default.dev.conf
Storage and Logs
This Laravel-docker-app leverage the power of docker volume and store some the docker container data at your host machine. Here is the details of folder structure and what data it contain:
./docker/data/mysql contain mysql database files
./docker/data/redis contain redis database file
./docker/data/web contain nginx webserver logs
#### Connecting MySQL with local desktop client.
At the moment, there are many other Laravel Docker github repo or packages available, so why this new package again? Here I'll list few of my points:
This docker app images is based on alpine based PHP image, which makes it very light weight.
This is built on service based architecture. Usually for Laravel application, there should be Queue and cron running on behind. In this system, you can run Queue, Scheduler container separately and Queue will listen to a separate Redis database.
It's the most compatible with production environment. Very easily this can be converted to a production grade, scalable service.
If you have any question, you can ask me in the comment or create an issue in the repo. If you have any improvement suggestion, a PR in the repo is most welcome.
I'd be really encouraged if I receive Star and Forks in the repo and Like in the article.