A Complete Docker based development environment for Laravel & PHP
Tags: laravel, php, docker, webdev
Published: 2021-05-16 08:08:55

**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 arifulhb/laravel-docker-app %}

## 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](https://github.com/arifulhb/laravel-docker-app) from Github 
```
git clone https://github.com/arifulhb/laravel-docker-app.git
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxrp03s03kzilg8emqpj.png)

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.md` [file](https://github.com/arifulhb/laravel-docker-app#readme).

### 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.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0e3gsq6st2e1lwn23918.png)
Run `docker stats` to see the memory and cpu usages of the containers.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0oht3pvunrmagowkwsar.png)


### 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`.
```
  environment:
    MYSQL_DATABASE: app
    MYSQL_ROOT_PASSWORD: root
    MYSQL_USER: admin
    MYSQL_PASSWORD: secret
```
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
```
## 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.