DEV Community

loading...
Cover image for A Complete Docker based development environment for Laravel & PHP

A Complete Docker based development environment for Laravel & PHP

Ariful Haque
Software Engineer | Tech Enthusiastic | Founder of LaptopList.com | Stargazer
・4 min read

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.

GitHub logo arifulhb / laravel-docker-app

A complete Laravel, PHP Docker based development environment with individual Nginx, Web app, Queue, Scheduler, Redis containers.

Laravel-Docker-App

Laravel, PHP7.3, Nginx, MySQL 5.7, Redis Docker Container

A complete Laravel, PHP Development environment that is very similar to production environment.

Docker Compose based Laravel development environment with individual App (Laravel app), Nginx, Database (MySQL), Queue, Scheduler, Redis services.

Installation

Install Laravel

Clone the repo at your development machine. Go to your Laravel Docker directory. Then Run this

sh install

A bash script will download latest Laravel zip and then unzip the Laravel project into the app directory.

Update Laravel Version

To update Laravel version before installaiton, edit the install file in root directory.

Docker Container management

Build containers

Run this to build your docker containers for each services (app, database, queue, scheduler, redis).

docker-compose build

Run the containers

docker-compose up

To run the services at background

docker-compose up -d

SSH into your app console

To install Laravel, generate app key

Why Laravel Docker App?

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

git clone https://github.com/arifulhb/laravel-docker-app.git
Enter fullscreen mode Exit fullscreen mode

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.

Alt Text

After the directory is created, need to run the command bellow to build the images.

docker-compose build
Enter fullscreen mode Exit fullscreen mode

A full set of commands to manage docker containers can be found in the Readme.md file.

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
Enter fullscreen mode Exit fullscreen mode

To run the services at background

  docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Image of running containers.

Run docker ps to see the containers running in background.
Alt Text
Run docker stats to see the memory and cpu usages of the containers.
Alt Text

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Stop running containers

docker-compose stop
Enter fullscreen mode Exit fullscreen mode

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.

  environment:
    MYSQL_DATABASE: app
    MYSQL_ROOT_PASSWORD: root
    MYSQL_USER: admin
    MYSQL_PASSWORD: secret
Enter fullscreen mode Exit fullscreen mode

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.
host: 0.0.0.0
username: root
password: root
port: 33066
Enter fullscreen mode Exit fullscreen mode

Why of Laravel-docker-app

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:

  1. This docker app images is based on alpine based PHP image, which makes it very light weight.
  2. 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.
  3. It's the most compatible with production environment. Very easily this can be converted to a production grade, scalable service.

Browse your site locally

You can find your site running at http://localhost

Conclusion

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.

Discussion (0)