17.12.19 • Published 6 years ago

docker-service-api v17.12.19

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

Docker API Swarm

Install

$ npm install docker-service-api

Usage

const Host = require('docker-service-api').Host;
const RegistryAuth = require('docker-service-api').RegistryAuth;

const Service = require('docker-service-api').Service;
const Network = require('docker-service-api').Network;
const Node = require('docker-service-api').Node;
const System = require('docker-service-api').System;
const Container = require('docker-service-api').Container;
const Image = require('docker-service-api').Image;
const Task = require('docker-service-api').Task;
const Swarm = require('docker-service-api').Swarm;

Service

Hello World

const service = new Service();

service.name = 'hello-world';
service.replicas = 5;
service.image = 'tutum/hello-world';
service.publish = ['8080:80'];
service
    .create()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Set Host Swarm

const service = new Service();
const service = new Service(new Host('host', port));

HTTPS, Cert, Key, CA

const host = new Host('IP', 'PORT', {
    key : fs.readFileSync(`${process.env.CERS}/key.pem`),
    cert : fs.readFileSync(`${process.env.CERS}/cert.pem`),
    ca : fs.readFileSync(`${process.env.CERS}/ca.pem`)
});

const swarm = new Swarm();

Set Authentication Registry

const service = new Service();

service.name = 'hello-world';
service.replicas = 5;
service.image = 'tutum/hello-world';
service.publish = ['8080:80'];

/**
* Registry username, password and servername
*/
service.registryAuth = new RegistryAuth('username', 'password', 'servername');

service
    .create()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

List Services

const service = new Service();

service
    .ls()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Inspect Services

const service = new Service();

service.id = 'ID';

service
    .inspect()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Remove Services

const service = new Service();

service.id = 'ID';

service
    .rm()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Network

Create Network

const network = new Network();

network.name = 'my_network_1';
network.driver = 'overlay';

network
    .create()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

List Network

const network = new Network();

network
    .ls()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Inspect Network

const network = new Network();

network.id = 'ID';

network
    .inspect()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Prune Network

const network = new Network();

network
    .prune()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Remove Network

const network = new Service();

network.id = 'ID';

network
    .rm()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Node

List Node

const node = new Node();

node
    .ls()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Inspect Node

const node = new Node();

node.id = 'ID';

node
    .inspect()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Swarm

Info Swarm

const swarm = new Swarm();

swarm
    .info()
    .then(res => {
        console.log(res.JoinTokens.Worker) // Token Worker
        return Promise.resolve(res);
    })
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Join Swarm

const swarm = new Swarm(new Host('IP', 4243));

swarm.JoinToken = 'SWMTKN-1-43fphmejjpl ....';
swarm.AdvertiseAddr = '10.172.79.47';
swarm
    .join()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Leave Swarm

const swarm = new Swarm(new Host('IP', 4243));

swarm
    .leave()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Init Swarm

const swarm = new Swarm();

swarm
    .init()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Container

Create Container / Run

/**
* Create Container
*/

const container = new Container();

container.name = 'hello_name';
container.image = 'tutum/hello-world';
container.publish = ['8080:80/tcp'];
container.volumes = ['/tmp:/tmp'];
container.privileged = true;
container.dns = ['8.8.8.8'];
container.dnsSearch = ['my.example.com'];
container.volumesFrom = ['datastore'];
container.links = ['mongo:mongo'];

//Labels
container.labels = {
    TOTVSIDWILLLLL : "0123456789"
}

container.logDriver = 'gelf'; // or 'syslog' or etc...

container.logOpt = ['gelf-address=udp://[IP]:5555', ...]; // or ['syslog-address=tcp+tls://192.168.1.3:514'] or etc...

container.Entrypoint = ['/bin/bash', 'sleep', '100000']

container.Cmd = ['/bin/bash', 'sleep', '100000']

container
    .run()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Inspect Container

const container = new Container();

container.name = 'hello-world';

container
    .inspect()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Kill Container

const container = new Container();

container.name = 'hello-world'; 

container
    .kill()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Logs Container

const container = new Container();

container.name = 'hello-world';

container
    .logs()
    .then(stream => {
        stream.on('data', data => console.log(data.toString()));
    })
    .catch(err => console.error(`ERRO: ${err}`));

Lista os Container

const container = new Container();

//Filters
container.filters = {
    label : ["Key=Value"]
}

container
    .ls()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

// OR

container
    .ps()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Pause Container

const container = new Container();

container.name = 'hello-world'; 

container
    .pause()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Prune Container

const container = new Container();

container
    .prune()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Remove Container

const container = new Container();

container.name = 'hello-world';

container
    .rm()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Start Container

const container = new Container();

container.name = 'hello-world'; 

container
    .start()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Stats Container

const container = new Container();

container.name = 'hello-world';

container
    .stats()
    .then(stream => {
        stream.on('data', data => console.log(JSON.parse(data)));
    })
    .catch(err => console.error(`ERRO: ${err}`));

Stop Container

const container = new Container();

container.name = 'hello-world';

container
    .stop()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Top Container

const container = new Container();

container.name = 'hello-world'; 

container
    .top()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Unpause Container

const container = new Container();

container.name = 'hello-world'; 

container
    .unpause()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Wait Container

const container = new Container();

container.name = 'hello-world'; 

container
    .wait()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Attach Container

const container = new Container();

container.name = 'hello-world';

container
    .attach()
    .then(stream => {
        stream.on('data', data => console.log(data.toString()));
    })
    .catch(err => console.error(`ERRO: ${err}`));

Task

List Task

const task = new Task();
task
    .ls()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

List Task

const task = new Task();

task.id = 'y2dvvwkr9c9uoqtjbfu5behva';

task
    .inspect()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Image

Remove Image

const image = new Image();

image.id = 'c9a8375cc12a';

image
    .rm()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

//OR

//image
//    .rmi()
//    .then(console.log)
//    .catch(err => console.error(`ERRO: ${err}`));
    

Pull Image

const image = new Image();

image.image = 'nginx:latest';

image
    .pull()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Prune Image

const image = new Image();
image
    .prune()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Lista Image

const image = new Image();
image
    .ls()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

History Image

const image = new Image();

image.id = 'c9a8375cc12a';

image
    .history()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Inspect Image

const image = new Image();

image.id = 'c9a8375cc12a';

image
    .inspect()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

System

Events System

system
    .events()
    .then(stream => {
        stream.on('data', data => console.log(JSON.parse(data)));
    })
    .catch(err => console.error(`ERRO: ${err}`));
    

Version System

const system = new System();

system
    .version()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));

Ping System

const system = new System();

system
    .ping()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Info System

const system = new System();

system
    .info()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

DF System

const system = new System();

system
    .df()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Registry

Registry Catalog

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

let registryAuth = new RegistryAuth('user', 'password', 'hostname');

let host = new Host(registryAuth.serveraddress, 443)
host.protocol = 'https';
host.versionApi = 'v2'

const registry = new Registry(host, registryAuth);

registry
    .catalog()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    

Registry Tags

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

let registryAuth = new RegistryAuth('user', 'password', 'hostname');

let host = new Host(registryAuth.serveraddress, 443)
host.protocol = 'https';
host.versionApi = 'v2'

const registry = new Registry(host, registryAuth);

registry
    .tags()
    .then(console.log)
    .catch(err => console.error(`ERRO: ${err}`));
    
17.12.19

6 years ago

17.11.17

6 years ago

17.8.24

7 years ago

17.7.5

7 years ago

17.6.271429

7 years ago

17.6.27

7 years ago

17.6.221936

7 years ago

17.6.22

7 years ago

17.6.21528

7 years ago

17.6.2

7 years ago

17.5.91508

7 years ago

17.5.91436

7 years ago

17.5.9

7 years ago

17.5.8

7 years ago

17.5.51853

7 years ago

17.5.51831

7 years ago

17.5.51812

7 years ago

17.5.5

7 years ago

17.5.4

7 years ago

17.4.272049

7 years ago

17.4.27

7 years ago

17.4.12

7 years ago

17.4.91928

7 years ago

17.4.91635

7 years ago

17.4.91559

7 years ago

17.4.91033

7 years ago

17.4.9

7 years ago

17.4.82028

7 years ago

17.4.82025

7 years ago

17.4.8

7 years ago

17.4.61

7 years ago

17.4.6-1521

7 years ago

17.4.6

7 years ago

17.4.5-1705

7 years ago

17.4.5

7 years ago