Skip to content

Instantly share code, notes, and snippets.

@sdbondi
Last active August 29, 2015 13:58
Show Gist options
  • Save sdbondi/9930246 to your computer and use it in GitHub Desktop.
Save sdbondi/9930246 to your computer and use it in GitHub Desktop.
Use PhpPgMyAdmin to detect your docker postgres instances
<?php
# phppgmyadmin/conf/config.inc.php
(...)
// Docker postgres connections
$server_i = 1;
$containers = explode("\n", trim(`docker ps`));
array_shift($containers);
foreach ($containers as $cid) {
if (!preg_match('/postgres/', $cid)) { continue; }
$cid = substr($cid, 0, 12);
$container = json_decode(`docker inspect {$cid}`);
if ($container) {
$container = $container[0];
$port = array_keys((Array)$container->NetworkSettings->Ports)[0];
$port = +explode('/', $port, 2)[0];
$conf['servers'][$server_i]['desc'] = substr($container->Name, 1);
$conf['servers'][$server_i]['host'] = $container->NetworkSettings->IPAddress;
$conf['servers'][$server_i]['port'] = $port;
$conf['servers'][$server_i]['sslmode'] = 'allow';
$conf['servers'][$server_i]['defaultdb'] = 'template1';
$conf['servers'][$server_i]['pg_dump_path'] = '/usr/bin/pg_dump';
$conf['servers'][$server_i]['pg_dumpall_path'] = '/usr/bin/pg_dumpall';
$server_i++;
}
}
@sdbondi
Copy link
Author

sdbondi commented Apr 2, 2014

Assumes your containers are running and have 'postgres' in their image name or container name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment