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

simple-dockerode

v0.2.2

Published

Wraps Dockerode container exec function to make it easier to use

Downloads

86

Readme

Simple-Dockerode

Wraps Dockerode container exec function to make it easier to use.

Wrangling streams and four levels of callbacks on exec is a pain. This makes it simpler, hopefully.

https://nodei.co/npm/simple-dockerode.svg?downloads=true&downloadRank=true&stars=true

npm version Dependency Status

master: Build Status Docs Status

dev: Build Status Docs Status

bitHound Overall Score bitHound Code Code Climate

contributions welcome

Usage

Use this package just like you would Dockerode. The only difference is exec().

The first argument is your command, in exec array form. The second is an object of options. The third is a callback.

const Dockerode = require('simple-dockerode');

const docker = new Dockerode({protocol: 'http', host: 'localhost', port: 2375});

const myContainer = docker.getContainer('hello-world');

// Simple to grab the stdout and stderr.
myContainer.exec(['echo', 'goodbye world'], {stdout: true}, (err, results) => {
  console.log(results.stdout);
  // goodbye world
});

The results contain your exec's inspect values too, for the exit code and other information.

myContainer.exec(['stat', '/non-existent-file'], {stdout: true, stderr: true})
  .catch(console.error)
  .then(results => {
    if( results.inspect.ExitCode !== 0 ) {
      // This file obviously doesn't exist, we get an exit code of 1
      console.error(results.stderr);
      // stat: cannot stat '/non-existent-file': No such file or directory
    } else {
      console.log(results.stdout);
    }
  })
;

Stdin can be either a string or a Stream:

myContainer.exec(['tee', '/non-existent-file'], {stdin: 'this is impossible!'}, (err, results) => { ... });

myContainer.exec(['cat', '/non-existent-file'], {stdout: true, stderr: true}, (err, results) => {
  if( results.inspect.ExitCode !== 0 ) {
    console.error(results.stderr);
  } else {
    console.log(results.stdout);
    // this is impossible!
  }
});

// Set up a nonsense little stream
const sender = new Stream.Readable();
sender.push('this is impossible!');
sender.push(null);
myContainer.exec(['tee', '/non-existent-file'], {stdin: sender}, (err, results) => { ... });

In live mode ({live: true} in the options), it allows you to play with the streams yourself. The results in the callback become a function that you can use to plug in to Dockerode's demuxer. It also returns the main stream object.

myContainer.exec(['someInteractiveCommand'], {live: true, stdin: true}, (err, streamLink) => {
  const stream = streamLink(process.stdout, process.stderr);
  myStream.pipe(stream);
  stream.on('end', () => console.log('done!'));
});

// I haven't tested this since I have no idea why you would want to do it, but you probably could.
myContainer.exec(['cat', '/non-existent-file'], {live: true, stdout: true}, (err, streamLink) => {
  myContainer.exec(['tee', '/another-file'], {stdin: streamLink()}, (err, results) => { ... });
});

Properties and Methods

exec()

Arguments:

  • Cmd: an array of the command you want to run, exec style.

    This should look the same as normal Dockerode's Cmd option.

  • options: Optional. An object of options.

    See below for an explanation of the options.

  • callback: Optional. A function to be called when your exec is done.

    Called with (err, results), where results is an object containing inspect. If stdout or stderr are true, the object contains stdout and stderr as well.

    Or, if live is true, see live option below.

    If omitted, a Promise is returned instead.

Options

stdout

Default: false

When set to true, the exec's stdout will be returned to you in results.

stderr

Default: false

When set to true, the exec's stderr will be returned to you in results.

Actually, setting either stdout or stderr will return both of them anyway.

stdin

Default: null

This option supports either a string or a Readable Stream. The contents will be sent to the exec's stdin pipe.

If you're using live mode (live: true), then you should also set stdin if you want to be able to pipe into the returned stream object. It will not pipe the content for you, so you can just set stdin: true, or any other truthy value.

live

Default: false

This mode will allow you to manipulate the streams yourself. You must add stdout: true, stderr: true, stdin: true, or any combination.

Instead of calling your callback with (err, results), it calls it with (err, streamLink), where streamLink is a function that can be called with up to two arguments. These arguments are directly fed to Dockerode's modem demuxer, and must be Writeable Stream objects. streamLink() returns the (possibly) muxed stream object, allowing you to attach events like .on('end').

createOpts

Default: null

This option is a direct interface to Dockerode's options on the normal exec function. Anything specified here will override other options.

Be careful when using this option, as you may override something that one of the simple-dockerode options has already set.

execOpts

Default: null

This option is a direct interface to Dockerode's options on the normal start function. Anything specified here will override other options.

Be careful when using this option, as you may override something that one of the simple-dockerode options has already set.

execRaw()

The original exec function in Dockerode, preserved under this name if you want to use it.

Testing

Run npm test to execute the test suite. You may need sudo if your user does not have permissions to access Docker.

The tests require that your local machine is running an instance of Docker, can pull an image from Docker Hub, and is not already running a container called simple-dockerode-test.