-
Notifications
You must be signed in to change notification settings - Fork 30
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
Add DOMAIN_NAME for networks #34
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,8 @@ private function getHostsLines($container) | |
// Networks | ||
if (isset($container['NetworkSettings']['Networks']) && is_array($container['NetworkSettings']['Networks'])) { | ||
foreach ($container['NetworkSettings']['Networks'] as $networkName => $conf) { | ||
preg_match(sprintf('/^%s_(.+)$/', $this->getProjectName($container)), $networkName, $matches); | ||
$networkShortName = isset($matches[1]) ? $matches[1] : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
should allow to not require setting the name as |
||
$ip = $conf['IPAddress']; | ||
|
||
$aliases = isset($conf['Aliases']) && is_array($conf['Aliases']) ? $conf['Aliases'] : []; | ||
|
@@ -136,6 +138,12 @@ private function getHostsLines($container) | |
$hosts[] = $alias.'.'.$networkName; | ||
} | ||
|
||
foreach ($this->getAdditionalContainerHosts($container) as $host) { | ||
if (preg_match('/^(.+):(.+)$/', $host, $matches) && $matches[1] === $networkShortName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this check and not directly add the host in the array ? |
||
$hosts[] = $matches[2]; | ||
} | ||
} | ||
|
||
$lines[$ip] = sprintf('%s%s', isset($lines[$ip]) ? $lines[$ip].' ' : '', implode(' ', $hosts)); | ||
} | ||
} | ||
|
@@ -148,22 +156,67 @@ private function getHostsLines($container) | |
} | ||
|
||
/** | ||
* @param Container $container | ||
* @param array $container | ||
* | ||
* @return string | ||
*/ | ||
private function getProjectName($container) | ||
{ | ||
if (isset($container['Config']['Labels']['com.docker.compose.project'])) { | ||
return $container['Config']['Labels']['com.docker.compose.project']; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
/** | ||
* @param array $container | ||
* | ||
* @return array | ||
*/ | ||
private function getContainerHosts($container) | ||
{ | ||
$hosts = [substr($container['Name'], 1).$this->tld]; | ||
return array_merge( | ||
[substr($container['Name'], 1).$this->tld], | ||
$this->getAdditionalContainerHosts($container) | ||
); | ||
} | ||
|
||
/** | ||
* @param array $container | ||
* | ||
* @return array | ||
*/ | ||
private function getAdditionalContainerHosts($container) | ||
{ | ||
$hosts = []; | ||
|
||
$domainName = $this->getEnv($container, 'DOMAIN_NAME'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to suggest that |
||
if ($domainName) { | ||
$hosts = array_merge($hosts, explode(',', $domainName)); | ||
} | ||
|
||
return $hosts; | ||
} | ||
|
||
/** | ||
* @param array $container | ||
* @param string $name | ||
* @param string $default | ||
* | ||
* @return string | ||
*/ | ||
private function getEnv($container, $name, $default = null) | ||
{ | ||
if (isset($container['Config']['Env']) && is_array($container['Config']['Env'])) { | ||
$env = $container['Config']['Env']; | ||
foreach (preg_grep('/DOMAIN_NAME=/', $env) as $row) { | ||
$row = substr($row, strlen('DOMAIN_NAME=')); | ||
$hosts = array_merge($hosts, explode(',', $row)); | ||
foreach ($container['Config']['Env'] as $env) { | ||
if (preg_match(sprintf('/^%s=(.*)$/', $name), $env, $matches)) { | ||
return $matches[1]; | ||
} | ||
} | ||
} | ||
|
||
return $hosts; | ||
return $default; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it working with a container not created by docker compose ?
docker run ...