Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebuildy/27694710e8ca13f0234f to your computer and use it in GitHub Desktop.
Save ebuildy/27694710e8ca13f0234f to your computer and use it in GitHub Desktop.
PhpMyAdmin on Rancher: Auto mysql server Docker container discovery. This will use Rancher API to browse the containers list, and search for container who expose 3306 port or has label "type:mysql"
<?php
$buffer = json_decode(file_get_contents('http://*****:*******@********/v1/containers'), true);
$containers = $buffer['data'];
$mysqlContainers = [];
$i = 0;
foreach($containers as $container)
{
$valid = false;
if (!empty($container['ports']))
{
foreach ($container['ports'] as $port)
{
if ($port === '3306/tcp')
{
$valid = true;
break;
}
}
}
if (!empty($container['labels']))
{
foreach ($container['labels'] as $label => $value)
{
if ($label === 'type' && $value === 'mysql')
{
$valid = true;
break;
}
}
}
if ($valid)
{
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = $container['primaryIpAddress'];
$cfg['Servers'][$i]['verbose'] = $container['name'] . ' (' . $container['primaryIpAddress'] . ')';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment