Skip to content

Commit

Permalink
Fix json marshal error when adding a container with no IPAMConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
austinwbest committed Jul 28, 2024
1 parent a768f61 commit 947bf4b
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions root/app/www/public/classes/traits/Docker/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ public function apiCreateContainer($inspect = [])
{
$inspect = $inspect[0] ? $inspect[0] : $inspect;
$image = $inspect['Config']['Image'];

$containerName = '';
if ($inspect['Name']) {
$containerName = str_replace('/', '', $inspect['Name']);
}

if (!$containerName && !empty($inspect['Config']['Env'])) {
foreach ($inspect['Config']['Env'] as $env) {
if (str_contains($env, 'HOST_CONTAINERNAME')) {
Expand All @@ -124,7 +124,7 @@ public function apiCreateContainer($inspect = [])
}
}
}

$payload = [
'Hostname' => $inspect['Config']['Hostname'] ? $inspect['Config']['Hostname'] : $containerName,
'Domainname' => $inspect['Config']['Domainname'],
Expand All @@ -136,7 +136,7 @@ public function apiCreateContainer($inspect = [])
'StopSignal' => $inspect['Config']['StopSignal'],
'WorkingDir' => $inspect['Config']['WorkingDir']
];

$payload['Env'] = $inspect['Config']['Env'];
$payload['Cmd'] = $inspect['Config']['Cmd'];
$payload['Healthcheck'] = $inspect['Config']['Healthcheck'];
Expand All @@ -147,41 +147,45 @@ public function apiCreateContainer($inspect = [])
$payload['NetworkingConfig'] = [
'EndpointsConfig' => $inspect['NetworkSettings']['Networks']
];

//-- API VALIDATION STUFF
if (empty($payload['HostConfig']['PortBindings'])) {
$payload['HostConfig']['PortBindings'] = null;
}

if (empty($payload['NetworkingConfig']['EndpointsConfig'])) {
$payload['NetworkingConfig']['EndpointsConfig'] = null;
} else {
if (empty($payload['NetworkingConfig']['EndpointsConfig']['IPAMConfig'])) {
$payload['NetworkingConfig']['EndpointsConfig']['IPAMConfig'] = '{}';
}
}

if (empty($payload['ExposedPorts'])) {
$payload['ExposedPorts'] = null;
} else {
foreach ($payload['ExposedPorts'] as $port => $value) {
$payload['ExposedPorts'][$port] = null;
}
}

if (empty($payload['Volumes'])) {
$payload['Volumes'] = null;
} else {
foreach ($payload['Volumes'] as $volume => $value) {
$payload['Volumes'][$volume] = null;
}
}

if (empty($payload['HostConfig']['LogConfig']['Config'])) {
$payload['HostConfig']['LogConfig']['Config'] = null;
}

//-- PART OF A CONTAINER NETWORK
if (str_contains($payload['HostConfig']['NetworkMode'], 'container:')) {
$payload['Hostname'] = null;
$payload['ExposedPorts'] = null;

//-- MAKE SURE THE ID IS UPDATED
$dependencyFile = getServerFile('dependency');
$dependencyFile = is_array($dependencyFile['file']) && !empty($dependencyFile['file']) ? $dependencyFile['file'] : [];
Expand All @@ -193,7 +197,7 @@ public function apiCreateContainer($inspect = [])
}
}
}

if (empty($payload['HostConfig']['Mounts'])) {
$payload['HostConfig']['Mounts'] = null;
} else {
Expand All @@ -203,7 +207,7 @@ public function apiCreateContainer($inspect = [])
}
}
}

if ($payload['NetworkSettings']['Networks']) {
foreach ($payload['NetworkSettings']['Networks'] as $network => $networkSettings) {
if ($networkSettings['Aliases']) {
Expand All @@ -220,7 +224,7 @@ public function apiCreateContainer($inspect = [])
}
}
}

return [
'container' => $containerName,
'endpoint' => sprintf(DockerApi::CREATE_CONTAINER, $containerName),
Expand Down

0 comments on commit 947bf4b

Please sign in to comment.