++ed by:
ROMANF

1 PAUSE user(s)

Javier Gomez
and 1 contributors

NAME

Eixo::Docker - Suite of Perl modules to interact with Docker

SYNOPSIS

First we need docker api server running in a tcp socket

- For example, to run api in localhost:4243, add this line to /etc/default/docker

    DOCKER_OPTS="-H 127.0.0.1:4243 -d"

- And restart docker service

    $ sudo service docker restart

Now to interact with it, instantiate a docker api client with the tcp socket url of the docker API:

    use Eixo::Docker::Api;
    
    my $a = Eixo::Docker::Api->new('http://127.0.0.1:4243');

From now on you can call all the Docker api methods throught api products (containers and images), passing the args indicated in api documentation:

Interacting with containers

Getting a container

    #
    # CONTAINERS
    #
    
    ## get (by id)
    my $c = $a->containers->get(id => "340f03a2c2cfxx");
    
    ## getByName
    my $c = $a->containers->getByName("testing123");

Creating a container

    # create
    # to see all available params 
    # http://docs.docker.io/en/latest/api/docker_remote_api_v1.10/#create-a-container
    my $c = $a->container->create(
        Hostname => 'test',
        Memory   => 128,
        Cmd => ["ls","-l"],
        Image => "ubuntu",
        Name => "testing123"
    );

Creating a container (asynchronous version)

    #
    #  Closure to get the created container
    # 
    my $new_container;

    $api->containers->createAsync(
        
        %args_creation,

        onSuccess=> sub {

                $new_container = $_[0];

        }

    );

    #
    # Waiting for the job to complete
    #
    $api->waitForJobs;

Start/stop/restart/kill a container

   $container->start();

   $container->stop();

   $container->restart();

   $container->kill();

Binding a container port to a host interface

The container must be created with 'NetworkDisabled' = 'false' and 'ExposedPorts' defined

    my $c = $a->containers->create(
        Hostname => 'test',
        Cmd => ["nc", "-l", '0.0.0.0', '5555'],
        Image => "ubuntu",
        Name => "testing123",
        NetworkDisabled => "false",
        ExposedPorts => {
                 "5555/tcp" =>  {}  
         },
    );

When starting the container the 'PortBindings' must be defined

    $c->start(
         "PortBindings" => { 
                 "5555/tcp" =>  [{"HostIp" =>  "0.0.0.0", "HostPort" =>  "11022" }] 
         },
         "PublishAllPorts" => "false", # true by default ¿?
         "Privileged" => "false",
    );

Attaching volumes to containers

Similar to ports, 'Volumes' must be specified in creation

    my $c = $a->containers->create(
        Hostname => 'test',
        Cmd => ["nc", "-l", '0.0.0.0', '5555'],
        Image => "ubuntu",
        Name => $name,
        Volumes => {
            "/mnt" => {},
            "/usr" => {}
        },
    );

When starting, 'Binds' config must be provided

        $c->start(
            "Binds" => [
                "/mnt:/tmp",
                "/usr:/usr:ro"
            ],
        );

Deleting a container

    ## delete
    $container->delete();

Deleting a container (asynchronous version)

    my $job_id = $container->deleteAsync(

        onSuccess=>sub {

                # 
                # Container has been deleted
                #
        }

    );

    # Others things to do...
    #

    #
    # We wait for the specific job to finish
    #   
    $api->waitForJob($job_id);

Interacting with images

Getting an image

    ## get
    my $image = $a->images->get(id => "busybox");

Getting an image history

    ## history 
    print Dumper($image->history);

Create an image pulling it from registry

    ## create
    my $image = $a->images->create(
    
        fromImage=>'busybox',
    
        onSuccess=>sub {
            
            print "FINISHED\n";     
    
        },
    
        onProgress=>sub{
    
            print $_[0] . "\n";
        }
    );
    

Building images (from a Dockerfile)

    ## build
    my $image = $a->images->build(
        t => "my_image",
        Dockerfile => join("\n", <DATA>),
    );
    
    ## build_from_dir
    my $image = $a->images->build_from_dir(
        t => "my_image",
        DIR => "/tmp/directory_with_a_Dockerfile"
    );

Deleting an image

    ## delete
    $image->delete();

DESCRIPTION

The purpose of this library is to provide a set of modules to interact in a simple, but powerful way, with the Docker remote api

Containers supported methods:

  • get

  • getByName

  • getAll

  • create

  • delete

  • status

  • start

  • stop

  • restart

  • kill

  • copy

  • attach

  • top

Images supported methods:

  • get

  • getAll

  • history

  • create

  • build

  • build_from_dir

  • delete

DEPENDENCIES

Currently this module has the following dependencies:

  • Eixo::Base >= 1.0,

  • Eixo::Rest >= 1.0,

  • JSON >= 2.50,

  • Net::HTTP >= 6.06,

  • HTTP::Server::Simple::CGI (for testing purpose)

CAVEATS

- No unix socket support, currently only supports tcp connections to Docker API.

DEVELOPMENT

To setup development environment:

Ubuntu 12.04 64bits:

Install kernel 3.8

    # install the backported kernel
    sudo apt-get update
    sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring
    
    # reboot
    sudo reboot

Install docker:

    curl -s https://get.docker.io/ubuntu/ | sudo sh

Setup docker daemon listening in localhost:4243 tcp port:

  • Add this line to /etc/default/docker

        DOCKER_OPTS="-H 127.0.0.1:4243 -d"
  • restart docker service:

        $ sudo service docker restart
  • export DOCKER_TEST_HOST var to run integration tests

        export DOCKER_TEST_HOST=http://127.0.0.1:4243

More info at http://docs.docker.io

AUTHOR

Francisco Maseda, <frmadem@gmail.com>

Javier Gomez, <alambike@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2014, Francisco Maseda

Copyright (C) 2014, Javier Gómez

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.