Skip to content

Commit f2d3e31

Browse files
Merge pull request #3 from idrislab/develop
Develop
2 parents 4e614aa + 60ffb55 commit f2d3e31

File tree

6 files changed

+437
-510
lines changed

6 files changed

+437
-510
lines changed

LICENSE

Lines changed: 0 additions & 201 deletions
This file was deleted.

README.md

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,117 @@
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+
```

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "idrislab/laravel-docker-services",
3-
"description": "Laravel docker services",
2+
"name": "idrislab/laravel-docker-containers",
3+
"description": "Automate your docker containers inside laravel",
44
"authors": [
55
{
66
"name": "Luís Pitta Grós",
7-
"email": "luis.gros@olx.com"
7+
"email": "luis@idris.pt"
88
}
99
],
1010
"keywords": ["laravel", "docker", "services"],

src/Docker.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
2-
2+
/**
3+
* @author Luís Pitta Grós <luis@idris.pt>
4+
*/
35
namespace luisgros;
46

57
use Docker\DockerClient;
@@ -79,17 +81,18 @@ public function isNamedContainerRunning($name)
7981

8082
/**
8183
* @param $name
84+
* @param $network
8285
*
8386
* @return string
8487
*/
85-
public function getNamedContainerIp($name)
88+
public function getNamedContainerIp($name, $network)
8689
{
8790
$containerManager = $this->client->getContainerManager();
8891
$containers = $containerManager->findAll();
8992

9093
foreach ($containers as $container) {
9194
if ($container->getNames()[0] == "/".$name) {
92-
return $container->getNetworkSettings()->getNetworks()['bridge']->getIPAddress();
95+
return $container->getNetworkSettings()->getNetworks()[$network]->getIPAddress();
9396
}
9497
}
9598

0 commit comments

Comments
 (0)