npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cpchain-foundation/node-docker-api

v1.2.1

Published

Docker Remote API driver for node

Downloads

14

Readme

docker-api

travis-ci

Docker Remote API driver for node.js. It uses the same modem than dockerode, but the interface is promisified and with a different syntax.

Support for:

  • streams
  • stream demux
  • entities
  • run
  • tests
  • promises
  • full es6 support

The current status of the package is in beta state. This module covers the full API reference, including experimental stuff such as plugins.

Check the reference and the tests for full examples.

Installation

$ npm install node-docker-api

Usage

You can find more into the examples folder

Create, start, stop, restart and remove a container

'use strict';
const {Docker} = require('node-docker-api');

const docker = new Docker({ socketPath: '/var/run/docker.sock' });

docker.container.create({
  Image: 'ubuntu',
  name: 'test'
})
  .then(container => container.start())
  .then(container => container.stop())
  .then(container => container.restart())
  .then(container => container.delete({ force: true }))
  .catch(error => console.log(error));

List, inspect and top containers

'use strict';
const {Docker} = require('node-docker-api');

const docker = new Docker({ socketPath: '/var/run/docker.sock' });

// List
docker.container.list()
   // Inspect
  .then(containers => containers[0].status())
  .then(container => container.top())
  .then(processes => console.log(processes))
  .catch(error => console.log(error));

List, inspect and stat containers

'use strict';
const {Docker} = require('node-docker-api');

const docker = new Docker({ socketPath: '/var/run/docker.sock' });

// List
docker.container.list()
   // Inspect
  .then(containers => containers[0].status())
  .then(container => container.stats())
  .then(stats => {
    stats.on('data', stat => console.log('Stats: ', stat.toString()))
    stats.on('error', err => console.log('Error: ', err))
  })
  .catch(error => console.log(error));

Get logs of a container

'use strict';
const {Docker} = require('node-docker-api');

const docker = new Docker({ socketPath: '/var/run/docker.sock' });
let container;

docker.container.create({
  Image: 'ubuntu',
  name: 'test'
})
  .then(container => container.logs({
    follow: true,
    stdout: true,
    stderr: true
  }))
  .then(stream => {
    stream.on('data', info => console.log(info))
    stream.on('error', err => console.log(err))
  })
  .catch(error => console.log(error));

Export a container

const {Docker} = require('node-docker-api');
const fs = require('fs');

const docker = new Docker({ socketPath: '/var/run/docker.sock' });
let container;

docker.container.create({
  Image: 'ubuntu',
  name: 'test'
})
  .then(container => container.start())
  .then(container => container.export())
  .then(content => {
    const file = fs.createWriteStream("container.tar");
    file.end(content)
  })
  .catch(error => console.log(error));

Manipulate file system in a container

'use strict';
const fs = require('fs');
const {Docker} = require('node-docker-api');

const promisifyStream = stream => new Promise((resolve, reject) => {
  stream.on('data', data => console.log(data.toString()))
  stream.on('end', resolve)
  stream.on('error', reject)
});

const docker = new Docker({ socketPath: '/var/run/docker.sock' });
let container;

docker.container.create({
  Image: 'ubuntu',
  Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
  name: 'test'
})
  .then(container => container.start())
  .then(_container => {
    container = _container
    return _container.fs.put('./file.tar', {
      path: 'root'
    })
  })
  .then(stream => promisifyStream(stream))
  .then(() => container.fs.get({ path: '/var/log/dmesg' }))
  .then(stream => {
    const file = fs.createWriteStream("file.jpg");
    stream.pipe(file);
    return promisifyStream(stream);
  })
  .then(() => container.status())
  .then(container => container.stop())
  .catch(error => console.log(error));

Execute commands and kill containers

'use strict';
const {Docker} = require('node-docker-api');

const promisifyStream = stream => new Promise((resolve, reject) => {
  stream.on('data', data => console.log(data.toString()))
  stream.on('end', resolve)
  stream.on('error', reject)
});

const docker = new Docker({ socketPath: '/var/run/docker.sock' });
let _container;

docker.container.create({
  Image: 'ubuntu',
  Cmd: [ '/bin/bash', '-c', 'tail -f /var/log/dmesg' ],
  name: 'test'
})
  .then(container => container.start())
  .then(container => {
    _container = container
    return container.exec.create({
      AttachStdout: true,
      AttachStderr: true,
      Cmd: [ 'echo', 'test' ]
    })
  })
  .then(exec => {
    return exec.start({ Detach: false })
  })
  .then(stream => promisifyStream(stream))
  .then(() => _container.kill())
  .catch(error => console.log(error));

Build, inspect and remove an image

'use strict';
const {Docker} = require('node-docker-api');
const tar = require('tar-fs');

const promisifyStream = stream => new Promise((resolve, reject) => {
  stream.on('data', data => console.log(data.toString()))
  stream.on('end', resolve)
  stream.on('error', reject)
});

const docker = new Docker({ socketPath: '/var/run/docker.sock' });

var tarStream = tar.pack('/path/to/Dockerfile')
docker.image.build(tarStream, {
  t: 'testimg'
})
  .then(stream => promisifyStream(stream))
  .then(() => docker.image.get('testimg').status())
  .then(image => image.remove())
  .catch(error => console.log(error));

Pull and check history of an image

'use strict';
const {Docker} = require('node-docker-api');

const promisifyStream = (stream) => new Promise((resolve, reject) => {
  stream.on('data', (d) => console.log(d.toString()))
  stream.on('end', resolve)
  stream.on('error', reject)
})

const docker = new Docker({ socketPath: '/var/run/docker.sock' })

return docker.image.create({}, { fromImage: 'ubuntu', tag: 'latest' })
  .then(stream => promisifyStream(stream))
  .then(() => docker.image.get('ubuntu').status())
  .then(image => image.history())
  .then(events => console.log(events))
  .catch(error => console.log(error))

Fetch events from docker

'use strict'
const fs = require('fs');
const {Docker} = require('node-docker-api');

const promisifyStream = stream => new Promise((resolve, reject) => {
    stream.on('data', data => console.log(data.toString()))
    stream.on('end', resolve)
    stream.on('error', reject)
})

const docker = new Docker({ socketPath: '/var/run/docker.sock' })

docker.events({
    since: ((new Date().getTime() / 1000) - 60).toFixed(0)
})
  .then(stream => promisifyStream(stream))
  .catch(error => console.log(error))