Docker Deployments Done in a Doddle

Once you have your own docker registry set up for your containers, deployments can be made automatic, quick, and easy. I do this by having my docker hosts constantly "polling" the registry through the use of a cron and an extended version of the script below. When the registry container is different from the running continer, the update_container function is called. Alternatively, you could have the script triggered by an API request instead.

This is a very simplified script that is only meant to get you started. This example uses a container that is "pushed" to: registery.my-domain.com:port/my-awesome-project
<?php

define("REGISTRY", "registery.my-domain.com:port");


function update_container($project_name)
{
    # define your update/replacment steps here...
    # perhaps just something like:
    # shell_exec("/home/programster/$project_name/update.sh");
    # where update.sh has a call to docker stop, and docker run commands.
}


/**
 * Checks to see if the project's container is out of date and calls the
 * update function if it is.
 * @param string $project_name - the name of the project we are checking
 * @return void - calls the update function if out of date.
 */
function version_check($project_name)
{
    $json_response = file_get_contents("http://" . REGISTRY . "/v1/repositories/" . $project_name . "/tags");
    $obj = json_decode($json_response);
    $latest = $obj->latest;

    $json_response2 = shell_exec('docker inspect "' . REGISTRY . '/' . $project_name . '"');
    $obj2 = json_decode($json_response2);#
    $current = $obj2[0]->Id;

    if ($current != $latest)
    {
        print "calling update on $project_name" . PHP_EOL;
        update_container($project_name, $ip);
    }
}

version_check("my-awesome-project");

Now whenever you build and push a container to your registry, it will automatically replace the currently running container within moments.

No comments:

Post a Comment