function ip_pool_deallocate($ip)
{
    $state = ip_pool_get_state();
    $available_ips = $state["available_ips"];
    $used_ips = $state["used_ips"];
    $meta = $state["meta"];
    $old_meta = $meta[$ip];
    $used_ips = array_diff($used_ips, array($ip));
    array_push($available_ips, $ip);
    $mymeta = $meta[$ip];
    unset($mymeta);
    unset($meta[$ip]);
    $state["available_ips"] = $available_ips;
    $state["used_ips"] = $used_ips;
    $state["meta"] = $meta;
    ip_pool_save_state($state);
    return $old_meta;
}
function handle_status_update_event($event)
{
    dbg_log("Received event " . print_r($event, true));
    $prefix = substr($event["appId"], 1, strlen(get_config()->app_prefix));
    dbg_log("prefix is {$prefix}");
    if ($prefix == get_config()->app_prefix && $event["taskStatus"] == "TASK_RUNNING") {
        dbg_log("Handling event");
        $taskId = $event["taskId"];
        //then we need to add the pipework
        //to do that we need to identify the container
        //we have the marathon id but we need the docker id
        $host = $event["host"];
        $containers_details = docker_get_container_details_for_all($host);
        $container = $containers_details[$taskId];
        $containerID = $container["Id"];
        $ip = ip_pool_allocate(array("taskID" => "{$taskId}", "containerID" => "{$containerID}", "host" => $host));
        if ($ip == NULL) {
            throw new Exception("Could not allocate an ip address!");
        }
        dbg_log("Allocating ip {$ip} to docker id {$containerID} and mesos id {$taskId}");
        docker_add_container_public_ip($host, $containerID, $ip);
    }
    if ($prefix == get_config()->app_prefix && ($event["taskStatus"] == "TASK_KILLED" || $event["taskStatus"] == "TASK_LOST" || $event["taskStatus"] == "TASK_FAILED")) {
        dbg_log("Handling event");
        $taskID = $event["taskId"];
        $state = ip_pool_get_state();
        #var_dump($state);
        $publicIP = "none";
        $containerID = "none";
        foreach ($state["meta"] as $ip => $metadata) {
            if ($metadata["taskID"] == $taskID) {
                $containerID = $metadata["containerID"];
                $publicIP = $ip;
                break;
            }
        }
        if ($publicIP != "none") {
            dbg_log("Deallocating ip {$publicIP} from {$taskID}");
            ip_pool_deallocate($publicIP);
        } else {
            dbg_log("Could not find allocated ip for container {$taskID}");
        }
    }
}
<?php

include_once dirname(__FILE__) . "/../lib/ippool.php";
include_once dirname(__FILE__) . "/../lib/utilities.php";
var_dump(ip_pool_get_state());
$ip1 = ip_pool_allocate("ip1");
$ip2 = ip_pool_allocate("ip2");
var_dump($ip1);
var_dump($ip2);
$meta2 = ip_pool_deallocate($ip2);
$meta1 = ip_pool_deallocate($ip1);
var_dump(ip_pool_get_state());
<?php

require_once dirname(__FILE__) . "/../lib/utilities.php";
require_once dirname(__FILE__) . "/../lib/marathon_api.php";
require_once dirname(__FILE__) . "/../lib/docker_api.php";
require_once dirname(__FILE__) . "/../lib/ippool.php";
if (!array_key_exists("containerid", $_GET) || !array_key_exists("host", $_GET)) {
    echo "missing containerid or missing container_host";
}
$taskID = $_GET["containerid"];
$host = $_GET['host'];
#$container_details=docker_get_container_details_for_all($host);
#$container=$container_details[$id];
#if($container==null)
#	echo "Contaier with mesos  $id not found on host $host";
$state = ip_pool_get_state();
#var_dump($state);
$publicIP = "none";
$containerID = "none";
$host = "none";
foreach ($state["meta"] as $ip => $metadata) {
    if ($metadata["taskID"] == $taskID) {
        $containerID = $metadata["containerID"];
        $publicIP = $ip;
        if (preg_match('/(.*)\\/(\\d*)\\@(\\d)/', $ip, $match)) {
            $publicIP = $match[1];
        }
        $host = $metadata["host"];
        break;
    }
}