Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to add container to host network #1597

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions php/src/Docker/DockerActionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ public function GetContainerStartingState(Container $container) : IContainerStat
}

$containerName = $container->GetIdentifier();
if ($container->GetInternalPort() !== "") {
$connection = @fsockopen($containerName, (int)$container->GetInternalPort(), $errno, $errstr, 0.1);
$internalPort = $container->GetInternalPort();
if ($internalPort !== "" && $internalPort !== 'host') {
$connection = @fsockopen($containerName, (int)$internalPort, $errno, $errstr, 0.1);
if ($connection) {
fclose($connection);
return new RunningState();
Expand Down Expand Up @@ -216,9 +217,11 @@ public function CreateContainer(Container $container) : void {
}

$exposedPorts = [];
if ($container->GetInternalPort() !== 'host') {
foreach($container->GetPorts()->GetPorts() as $port) {
$exposedPorts[$port] = null;
}
}

$requestBody = [
'Image' => $this->BuildImageName($container),
Expand Down Expand Up @@ -566,7 +569,6 @@ public function sendNotification(Container $container, string $subject, string $
true
);

// get the id from the response
$id = $response['Id'];

// start the exec
Expand Down Expand Up @@ -606,8 +608,12 @@ public function DisconnectContainerFromNetwork(Container $container) : void
}
}

private function ConnectContainerIdToNetwork(string $id) : void
private function ConnectContainerIdToNetwork(string $id, string $internalPort) : void
{
if ($internalPort === 'host') {
$network = 'host';
} else {
$network = 'nextcloud-aio';
$url = $this->BuildApiUrl('networks/create');
try {
$this->guzzleClient->request(
Expand All @@ -631,9 +637,10 @@ private function ConnectContainerIdToNetwork(string $id) : void
throw $e;
}
}
}

$url = $this->BuildApiUrl(
sprintf('networks/%s/connect', 'nextcloud-aio')
sprintf('networks/%s/connect', $network)
);
try {
$this->guzzleClient->request(
Expand All @@ -655,12 +662,12 @@ private function ConnectContainerIdToNetwork(string $id) : void

public function ConnectMasterContainerToNetwork() : void
{
$this->ConnectContainerIdToNetwork('nextcloud-aio-mastercontainer');
$this->ConnectContainerIdToNetwork('nextcloud-aio-mastercontainer', '');
}

public function ConnectContainerToNetwork(Container $container) : void
{
$this->ConnectContainerIdToNetwork($container->GetIdentifier());
$this->ConnectContainerIdToNetwork($container->GetIdentifier(), $container->GetInternalPort());
}

public function StopContainer(Container $container) : void {
Expand Down