Skip to content

Commit e5c07cf

Browse files
committed
Allow configuring top-level networks
1 parent cb8fde0 commit e5c07cf

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

src/Console/Concerns/InteractsWithDockerComposeServices.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ protected function buildDockerCompose(array $services)
7979
}
8080
});
8181

82+
// Merge networks
83+
$compose['networks'] = collect(Sail::networks())->merge($compose['networks'] ?? [])->toArray();
84+
85+
foreach ($compose['networks'] ?? [] as $name => $network) {
86+
if ($network['external'] ?? false) {
87+
exec("docker network ls --filter name=^" . escapeshellarg($name) . "$ -q", $check);
88+
if (empty($check)) {
89+
exec("docker network create --driver bridge " . escapeshellarg($name), $output);
90+
$this->components->info("$name network has been created.");
91+
}
92+
}
93+
}
94+
8295
// Merge volumes...
8396
collect($services)
8497
->filter(function ($service) {

src/Sail.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
/**
99
* @method static self setBaseTemplate(string $stub)
1010
* @method static self addService(string $service, ?string $stubPath = null, ?bool $persistent = null, ?bool $default = null, ?bool $dependency = null, ?Closure $configuringEnv = null, ?Closure $afterInstall = null)
11+
* @method static self addNetwork(array $network)
1112
* @method static self registerInstallHook(Closure $closure)
1213
* @method static self registerPublishHook(Closure $closure)
1314
* @method static string baseTemplate()
1415
* @method static array availableServices(bool $default = false)
16+
* @method static array networks()
1517
* @method static string stub(string $service)
1618
* @method static bool isPersistent(string $service)
1719
* @method static bool isDependedOn(string $service)

src/Services.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ class Services
5252
'soketi',
5353
];
5454

55+
/**
56+
* The networks services communicating on
57+
*
58+
* @var array<string, array<string, string|bool>>
59+
*/
60+
protected array $networks = [
61+
'sail' => [
62+
'driver' => 'bridge',
63+
]
64+
];
65+
5566
/**
5667
* Path to the base docker compose template
5768
*
@@ -252,6 +263,17 @@ public function addService(string $service,
252263
return $this;
253264
}
254265

266+
/**
267+
* @param array<string, string|bool> $network
268+
* @return $this
269+
*/
270+
public function addNetwork(array $network): self
271+
{
272+
$this->networks = array_merge($this->networks, $network);
273+
274+
return $this;
275+
}
276+
255277
/**
256278
* Add a hook to the pipeline executed during the installation command.
257279
*
@@ -304,6 +326,16 @@ public function availableServices(bool $default = false): array
304326
return $services;
305327
}
306328

329+
/**
330+
* Get the list of networks defined for the docker-compose file
331+
*
332+
* @return array
333+
*/
334+
public function networks(): array
335+
{
336+
return $this->networks;
337+
}
338+
307339
/**
308340
* Get the stub path for a given service.
309341
*

stubs/docker-compose.stub

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,3 @@ services:
2222
- '.:/var/www/html'
2323
networks:
2424
- sail
25-
networks:
26-
sail:
27-
driver: bridge

0 commit comments

Comments
 (0)