English | Русский
DOCKER FOR LARAVEL
Description
Build Laravel's development environment using docker LEMP. Uses a persistant database store and stack PHP, MySQL, Redis, Nginx (http, https).
Info
Project link
- Laravel application HTTP: http://localhost:5101
- Laravel application HTTPS: http://localhost:5102
- MySQL: http://localhost:5103
- XDebug: http://192.168.220.1:5104
Laravel project path
src/
Logs path
src/logs
Usage
Install
Git
Clone this repository locally:
git clone https://github.com/cs-eliseev/docker-for-laravel.git
Download
Download the latest release here.
Install developments tools
- Install docker
- Install docker-compose
Create laravel project
-
Create a new Laravel project to Docker
composer create-project --prefer-dist laravel/laravel src
-
Git clone project to Docker
git clone <link> src
Build application
-
Go to the Laravel project
cd src
-
Add composer Laravel dependency
composer require predis/predis
-
Update Composer Laravel dependency
composer update
-
Install NPM Larave dependency
npm i
-
Add needs write permissions to Laravel project
sudo chmod 777 -R storage bootstrap/cache
-
Add group to Laravel project
sudo chown -R 1000:1000 storage bootstrap/cache
-
Build all Docker containers
docker-compose up --build
-
Create Laravel application key
docker exec -it laravel-container php artisan key:generate
-
Use project
HTTP - http://localhost:5101
HTTPS - https://localhost:5102
XDebug - http://192.168.220.1:5104
MySQL connection
-
Get MySQL network info in a Docker container
docker inspect datebase-container
-
Settings MySQL to Laravel
Edit
src/.env
fileDB_CONNECTION=mysql DB_HOST=datebase-container DB_PORT=3306 DB_DATABASE=laravel_project DB_USERNAME=root DB_PASSWORD=123456
-
Use MySQL in a Docker container
docker-compose exec database bash -c 'mysql -u root -p 123456 laravel_project'
-
Run Laravel Migration to Docker container
docker-compose exec laravel php artisan migrate
-
Clear database to Docker container
docker-compose down --volumes --rmi all docker-compose up -d --build docker-compose exec laravel php artisan migrate
Redis
-
Settings Redis to Laravel
Edit
src/.env
fileREDIS_HOST=redis-container REDIS_PASSWORD=null REDIS_PORT=6379
-
Test Redis
docker-compose exec laravel php artisan tinker Illuminate\Support\Facades\Redis::set('name', 'hoge'); Illuminate\Support\Facades\Redis::get('name'); exit
-
Use Redis cli
docker-compose exec redis redis-cli
XDebug
-
Change XDebug config for Mac or Windows
Change file
docker/laravelxdebug.ini
xdebug.remote_host=host.docker.internal
Use UnitTest
PHPUnit is used for unit testing. Unit tests ensure that class and methods does exactly what it is meant to do.
General PHPUnit documentation can be found at https://phpunit.de/documentation.html.
-
Add unit test Laravel application, db connection to Docker container
Create a new PHPUnit Test class
DbConnectivityTest
in foldersrc/tests/Feature
<?php namespace Tests\Feature; use Illuminate\Database\Connection; use Tests\TestCase; class DbConnectivityTest extends TestCase { public function testDbConnectivity() { /** @var Connection $db */ $db = $this->app->make("db"); $row = $db->selectOne("SELECT 1 AS one"); $this->assertEquals(1, $row->one); } }
-
To run the PHPUnit unit tests, execute in a Docker container:
docker-compose exec laravel ./vendor/bin/phpunit
Create SSL cert
-
Running OpenSSL command create ssl certs
sudo openssl req -x509 -nodes -days 999999 -newkey rsa:2048 -keyout docker/laravel/nginx/cert/nginx.key -out docker/laravel/nginx/cert/nginx.crt
Project managment
Git
Please see Git File for information.
Composer
Please see Composer File for information.
NPM
Please see NPM File for information.
PHPStan
Please see PHPStan File for information.
PHPCPD
Please see PHPCPD File for information.
PHP CS Fixer
Please see PHP CS Fixer File for information.
PHPUnit
Please see PHPUnit File for information.
XDebug
Please see Xdebug File for information.
Docker
Please see Docker File for information.
Laravel
Please see Laravel File for information.
Use command to Docker container: docker exec -it laravel-container <command>
Donating
You can support this project here. You can also help out by contributing to the project, or reporting bugs. Even voicing your suggestions for features is great. Anything to help is much appreciated.
License
The DOCKER FOR LARAVEL set of settings and configurations licensed under the MIT license. Please see License File for more information.
GitHub @cs-eliseev