docker-cli-js
A node.js wrapper for the docker command line tool
Installation
Warning
Code uses exec = child_process.exec; https://github.com/Quobject/docker-cli-js/issues/22
Step 1: Prerequisites
The docker command line tool must be installed and accessible in the path
Step 2: Installation
npm install docker-cli-js
Then:
var dockerCLI = require('docker-cli-js');
var DockerOptions = dockerCLI.Options;
var Docker = dockerCLI.Docker;
Usage
Modern JS - direct call
const { dockerCommand } = require('docker-cli-js');
const options = {
machineName: null,
currentWorkingDirectory: null,
echo: true,
};
const data = await dockerCommand('build -t nginximg .', options);
With promise
var options = new DockerOptions(
'aws_machine01',
'nginx',
true,
);
var docker = new Docker(options);
docker.command('build -t nginximg .').then(function (data) {
console.log('data = ', data);
})
With callback:
var docker = new Docker();
docker.command('info', function (err, data) {
console.log('data = ', data);
});
Typescript
import { Docker, Options } from 'docker-cli-js';
const options = new Options(
null,
path.join(__dirname, '..', 'test', 'nginx'),
true,
);
let docker = new Docker(options);
docker.command('build -t nginximg .').then(function (data) {
console.log('data = ', data);
});
docker.command('run --name nginxcont -d -p 80:80 nginximg').then(function (data) {
console.log('data = ', data);
});
docker.command('ps').then(function (data) {
console.log('data = ', data);
});
docker.command('images').then(function (data) {
console.log('data = ', data);
});
docker.command('network ls').then(function (data) {
console.log('data = ', data);
});
docker.command('inspect consul').then(function (data) {
console.log('data = ', data);
});
docker.command('info').then(function (data) {
console.log('data = ', data);
});
docker.command('search nginxcont').then(function (data) {
console.log('data = ', data);
});
docker.command('login -u myusername -p mypassword').then(function (data) {
console.log('data = ', data);
}, function (rejected) {
console.log('rejected = ', rejected);
});
docker.command('pull nginx:latest').then(function (data) {
console.log('data = ', data);
}, function (rejected) {
console.log('rejected = ', rejected);
});
docker.command('push nginx:latest').then(function (data) {
console.log('data = ', data);
}, function (rejected) {
console.log('rejected = ', rejected);
});
License
MIT