What is dockerode?
dockerode is a Node.js module that provides a way to interact with Docker's Remote API. It allows you to manage Docker containers, images, networks, volumes, and more programmatically.
What are dockerode's main functionalities?
Manage Containers
This feature allows you to create and manage Docker containers. The code sample demonstrates how to create and start a container using the 'ubuntu' image.
const Docker = require('dockerode');
const docker = new Docker();
// Create a container
const createContainer = async () => {
const container = await docker.createContainer({
Image: 'ubuntu',
Cmd: ['/bin/bash'],
name: 'my-ubuntu-container'
});
await container.start();
console.log('Container started');
};
createContainer();
Manage Images
This feature allows you to manage Docker images. The code sample demonstrates how to pull an image from Docker Hub.
const Docker = require('dockerode');
const docker = new Docker();
// Pull an image
const pullImage = async () => {
await docker.pull('ubuntu', (err, stream) => {
docker.modem.followProgress(stream, onFinished, onProgress);
function onFinished(err, output) {
console.log('Image pulled');
}
function onProgress(event) {
console.log(event);
}
});
};
pullImage();
Manage Networks
This feature allows you to manage Docker networks. The code sample demonstrates how to create a network with the 'bridge' driver.
const Docker = require('dockerode');
const docker = new Docker();
// Create a network
const createNetwork = async () => {
const network = await docker.createNetwork({
Name: 'my-network',
Driver: 'bridge'
});
console.log('Network created');
};
createNetwork();
Manage Volumes
This feature allows you to manage Docker volumes. The code sample demonstrates how to create a volume.
const Docker = require('dockerode');
const docker = new Docker();
// Create a volume
const createVolume = async () => {
const volume = await docker.createVolume({
Name: 'my-volume'
});
console.log('Volume created');
};
createVolume();
Other packages similar to dockerode
node-docker-api
node-docker-api is another Node.js module for interacting with Docker's Remote API. It provides similar functionalities to dockerode, such as managing containers, images, networks, and volumes. However, its API design and usage patterns may differ slightly.
docker-cli-js
docker-cli-js is a Node.js module that allows you to interact with Docker using the command line interface (CLI). Unlike dockerode, which interacts directly with Docker's Remote API, docker-cli-js executes Docker CLI commands and parses the output. This can be useful if you prefer working with the CLI or need to execute specific Docker commands not covered by the API.
dockerode
Yet another node.js Docker.io Remote API module.
Why is dockerode
different from all the Docker node.js module out there:
- streams -
dockerode
does NOT break any stream, it passes them to you allowing for some stream voodoo. - entities - containers and images are defined entities and not random static methods.
- run -
dockerode
allow you to seamless run commands in a container ala docker run
. - tests -
dockerode
really aims to have a good test set, allowing to follow Docker
changes easily, quickly and painfully. - ws - New websocket endpoints introduced in 0.6 are supported. (beta, do not use in production yet)
- features - implement ALL
Docker
Remote API features. (94% implemented)
installation
npm install dockerode
getting started
to use dockerode
first you need to instantiate it:
var Docker = require('dockerode');
var docker = new Docker({socketPath: '/var/run/docker.sock'});
var docker2 = new Docker({host: 'http://192.168.1.10', port: 3000});
Manipulating a container:
var container = docker.getContainer('71501a8ab0f8');
container.start(function (err, data) {
console.log(data);
});
container.remove(function (err, data) {
console.log(data);
});
Creating a container:
docker.createContainer({Image: 'ubuntu', Cmd: ['/bin/bash']}, function(err, container) {
container.start(function(err, data) {
});
});
Streams goodness:
container.attach({stream: true, stdout: true, stderr: true}, function(err, stream) {
stream.pipe(process.stdout);
});
docker.createImage({fromImage: 'ubuntu'}, function(err, stream) {
stream.pipe(process.stdout);
});
Equivalent of docker run
in dockerode
:
image
- container imagecmd
- command to be executedstream
- stream which will be used for execution output.temporary
- if true
container will be removed after execution ends.callback
- callback caled when execution ends.
docker.run('ubuntu', 'uname -a', process.stdout, true, function(err, data) {
console.log(data.StatusCode);
});
notes
- Input options are directly passed to Docker.io check Docker Remote API documentation for more details.
- Return values are unchanged from Docker, official Docker.io documentation will also apply to them.
tests
Tests were implemented using mocha
and chai
do npm test
to run them.
license
Pedro Dias <abru.pt>
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.html
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.