Skip to content
Snippets Groups Projects
Commit 5196dfd9 authored by jurgenhaas's avatar jurgenhaas
Browse files

Fix determination of IP address from within container

parent e6a06029
No related branches found
No related tags found
No related merge requests found
......@@ -210,7 +210,9 @@ class Handler extends BaseHandler {
'projectname' => $projectname,
'ci_home' => '/home/gitlab-runner',
'docker0' => [
'ip' => $this->getLocalIpv4('docker0'),
'ip' => ($this->isCiContext() || $this->isLocalDevMode()) ?
$this->getDockerGateway() :
$this->getLocalIpv4('docker0'),
],
'live' => [
'root' => '',
......@@ -334,37 +336,50 @@ class Handler extends BaseHandler {
return $local_addrs[$interface] ?? '127.0.0.1';
}
/**
* @return string
*/
private function getDockerGateway(): string {
$container = $this->readContainerConfig();
return $container['NetworkSettings']['Gateway'];
}
/**
* @param $projectRoot
*
* @return string
*/
private function getDockerMountSource($projectRoot): string {
$currentDir = getcwd();
$container = $this->readContainerConfig();
foreach ($container['Mounts'] as $mount) {
if (empty($projectRoot)) {
if ($currentDir === $mount['Destination']) {
return $mount['Source'];
}
}
else {
if (strpos($projectRoot, $mount['Destination']) === 0) {
return $mount['Source'] . substr($projectRoot, strlen($mount['Destination']));
}
}
}
return getcwd();
}
private function readContainerConfig() {
try {
$currentDir = getcwd();
$output = [];
exec('basename "$(cat /proc/1/cpuset)"', $output);
$id = reset($output);
$output = [];
exec('docker container inspect ' . $id, $output);
$container = json_decode(implode('', $output), TRUE)[0];
foreach ($container['Mounts'] as $mount) {
if (empty($projectRoot)) {
if ($currentDir === $mount['Destination']) {
return $mount['Source'];
}
}
else {
if (strpos($projectRoot, $mount['Destination']) === 0) {
return $mount['Source'] . substr($projectRoot, strlen($mount['Destination']));
}
}
}
return json_decode(implode('', $output), TRUE)[0];
}
catch (\Exception $ex) {
// Ignore.
}
return getcwd();
return [];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment