|
1 | | -# Laravel Docker Services |
| 1 | +# Laravel Docker Containers |
| 2 | + |
| 3 | +Automate your docker containers inside Laravel |
| 4 | + |
| 5 | +## Installation |
| 6 | +```sh |
| 7 | +composer require idrislab/laravel-docker-containers |
| 8 | +``` |
| 9 | + |
| 10 | +Register DockerContainers with Artisan in *app/Console/Kernel.php* |
| 11 | + |
| 12 | +```php |
| 13 | +protected $commands = [ |
| 14 | + DockerContainers::class, |
| 15 | + ]; |
| 16 | +``` |
| 17 | + |
| 18 | +Set the following environment variables (inside your .env file) |
| 19 | + |
| 20 | +```sh |
| 21 | +DOCKER_SOCKET=unix:///var/run/docker.sock |
| 22 | +DOCKER_CONTAINERS=mysql,redis |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +``` |
| 28 | +php artisan containers <start|stop|restart> [--name=] |
| 29 | +``` |
| 30 | + |
| 31 | +### Examples |
| 32 | +Starting all containers |
| 33 | +```sh |
| 34 | +php artisan containers start |
| 35 | +``` |
| 36 | + |
| 37 | +Stopping only one container |
| 38 | +```sh |
| 39 | +php artisan containers stop --name=mysql |
| 40 | +``` |
| 41 | + |
| 42 | +## Adding Containers |
| 43 | +Create a new Artisan command |
| 44 | +```sh |
| 45 | +php artisan make:command Containers |
| 46 | +``` |
| 47 | + |
| 48 | +Register the new command with Artisan in *app/Console/Kernel.php* and remove *DockerContainers::class* if registered |
| 49 | +```php |
| 50 | +protected $commands = [ |
| 51 | + Commands\Containers::class, |
| 52 | + ]; |
| 53 | +``` |
| 54 | + |
| 55 | +Update your command to look like |
| 56 | +```php |
| 57 | +<?php |
| 58 | +namespace App\Console\Commands; |
| 59 | + |
| 60 | +use luisgros\DockerContainers; |
| 61 | + |
| 62 | +/** |
| 63 | + * Class Containers |
| 64 | + * |
| 65 | + * @package App\Console\Commands |
| 66 | + */ |
| 67 | +class Containers extends DockerContainers |
| 68 | +{ |
| 69 | + /** |
| 70 | + * Execute the console command. |
| 71 | + * |
| 72 | + * @return mixed |
| 73 | + */ |
| 74 | + public function handle() |
| 75 | + { |
| 76 | + |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +Add your container(s) inside method *handle()*, in this case we're adding a [MySQL Group Replication Container](https://hub.docker.com/r/mysql/mysql-gr/) |
| 82 | +```php |
| 83 | + $containers = [ |
| 84 | + 'MySQLGr' => [ |
| 85 | + 'repo' => 'mysql/mysql-gr', |
| 86 | + 'tag' => 'latest', |
| 87 | + 'instances' => 3, |
| 88 | + 'commands' => [ |
| 89 | + 1 => |
| 90 | + '-d --net=group1 -e MYSQL_ROOT_PASSWORD=ENV[DB_PASSWORD] \\'. |
| 91 | + '-e MYSQL_REPLICATION_USER=ENV[DB_PASSWORD] -e MYSQL_REPLICATION_PASSWORD=ENV[DB_PASSWORD] \\'. |
| 92 | + 'mysql/mysql-gr --group_replication_group_seeds=\'ENV[MYSQLGR2]:6606,ENV[MYSQLGR3]:6606\' \\'. |
| 93 | + '--server-id=ENV[INSTANCE_NAME]', |
| 94 | + 2 => |
| 95 | + '-d --net=group1 -e MYSQL_ROOT_PASSWORD=ENV[DB_PASSWORD] \\'. |
| 96 | + '-e MYSQL_REPLICATION_USER=ENV[DB_PASSWORD] -e MYSQL_REPLICATION_PASSWORD=ENV[DB_PASSWORD] \\'. |
| 97 | + 'mysql/mysql-gr --group_replication_group_seeds=\'ENV[MYSQLGR1]:6606,ENV[MYSQLGR3]:6606\' \\'. |
| 98 | + '--server-id=ENV[INSTANCE_NAME]', |
| 99 | + 3 => |
| 100 | + '-d --net=group1 -e MYSQL_ROOT_PASSWORD=ENV[DB_PASSWORD] \\'. |
| 101 | + '-e MYSQL_REPLICATION_USER=ENV[DB_PASSWORD] -e MYSQL_REPLICATION_PASSWORD=ENV[DB_PASSWORD] \\'. |
| 102 | + 'mysql/mysql-gr --group_replication_group_seeds=\'ENV[MYSQLGR1]:6606,ENV[MYSQLGR2]:6606\' \\'. |
| 103 | + '--server-id=ENV[INSTANCE_NAME]', |
| 104 | + ], |
| 105 | + 'network' => 'group1', |
| 106 | + 'docker' => [ |
| 107 | + 'pre' => [ |
| 108 | + 'network create group1 &>/dev/null', |
| 109 | + ] |
| 110 | + ] |
| 111 | + ], |
| 112 | + ]; |
| 113 | + |
| 114 | + $this->addContainers($containers); |
| 115 | + |
| 116 | + parent::handle(); |
| 117 | +``` |
0 commit comments