From 5196dfd97fc483de0f66a9ca663209ef58c0f3f9 Mon Sep 17 00:00:00 2001 From: jurgenhaas <juergen@paragon-es.de> Date: Tue, 11 Dec 2018 12:17:56 +0100 Subject: [PATCH] Fix determination of IP address from within container --- src/Handler.php | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Handler.php b/src/Handler.php index e9246dd..28ea2d0 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -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 []; } } -- GitLab