Socket
Socket
Sign inDemoInstall

crew

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crew

crew makes managing Docker a breeze.


Version published
Weekly downloads
30
Maintainers
1
Weekly downloads
 
Created
Source

crew

crew makes managing Docker a breeze.

Installation

$ npm install crew

Quick start

First you need to add a reference to crew to to your application.

var crew = require('crew');

To connect to a Docker server, call the crew function and provide the hostname as well as the port. Additionally, you need to provide a private key and a certificate for client-side authentication, and a CA certificate for server-side authentication.

You may use the environment variables DOCKER_HOST and DOCKER_CERT_PATH to get appropriate values.

crew({
  host: url.parse(process.env.DOCKER_HOST).hostname,
  port: url.parse(process.env.DOCKER_HOST).port,
  keys: {
    privateKey: fs.readFileSync(path.join(process.env.DOCKER_CERT_PATH, 'key.pem')),
    certificate: fs.readFileSync(path.join(process.env.DOCKER_CERT_PATH, 'cert.pem')),
    caCertificate: fs.readFileSync(path.join(process.env.DOCKER_CERT_PATH, 'ca.pem'))
  }
}, function (err, dockWorker) {
  // ...
});

Please note that the initially provided options are available at dockWorker.options.

Pinging Docker

To ping the Docker server, use the ping function.

dockWorker.ping(function (err) {
  // ...
});

Verifying that an image is available

If you need to verify whether an image is available on the Docker server, use the hasImage function and provide the name of the image.

dockWorker.hasImage('hello-world', function (err, hasImage) {
  console.log(hasImage); // => true
  // ...
});

Please note that verification does not respect tags, i.e. if any version of the image is available, verification will succeed.

Downloading an image

To download an image to the Docker server, use the download function and provide the name of the image. If you want to download a specific version, add the tag to the name of the image.

dockWorker.download('hello-world', function (err) {
  // ...
});

Building an image

To build an image, call the buildImage function and provide the directory you want to use, a Dockerfile, and the name of the image.

dockWorker.buildImage({
  directory: __dirname,
  dockerfile: path.join(__dirname, 'my-dockerfile'),
  name: 'myImage'
}, function (err) {
  // ...
});

If you want to exclude some files from the newly built image, you can use the dockerignore property to provide the path to an ignore file.

dockWorker.buildImage({
  directory: __dirname,
  dockerfile: path.join(__dirname, 'my-dockerfile'),
  dockerignore: path.join(__dirname, 'my-dockerignore'),
  name: 'myImage'
}, function (err) {
  // ...
});

Starting a container

To create and start a container, call the start function and provide the name of the image and the desired container name. This returns the newly created container's id.

dockWorker.start({
  image: 'hello-world',
  name: 'myContainer'
}, function (err, id) {
  console.log(id); // => '70073a08b0f7fdfef44ca6fe03ba5e796d4773d9628b6f68eb7e34568dc73e1f'
  // ...
});
Restarting crashed containers

If you want your container to restart automatically on crashes, add the restart property to the parameter object and set it to true.

dockWorker.start({
  image: 'hello-world',
  name: 'myContainer',
  restart: true
}, function (err, id) {
  // ...
});
Forwarding ports

To forward container ports to the host, add the ports property to the parameter object and hand over an array of forwardings.

dockWorker.start({
  image: 'hello-world',
  name: 'myContainer',
  ports: [
    { container: 3000, host: 80 }
  ]
}, function (err, id) {
  // ...
});
Setting environment variables

To set environment variables, add the env property to the parameter object and hand over the keys and values you want to use as environment variables.

dockWorker.start({
  image: 'hello-world',
  name: 'myContainer',
  env: {
    port: 3000
  }
}, function (err, id) {
  // ...
});
Using volumes

To use volumes from the host, add the volumes property to the parameter object and hand over an array of volume mappings.

dockWorker.start({
  image: 'hello-world',
  name: 'myContainer',
  volumes: [
    { container: '/data', host: '/home/janedoe/foo' }
  ]
}, function (err, id) {
  // ...
});

To link a container to another one, add the links property to the parameter object and hand over an array of link mappings.

dockWorker.start({
  image: 'hello-world',
  name: 'myContainer',
  links: [
    { name: 'mongodb', alias: 'db' }
  ]
}, function (err, id) {
  // ...
});
Configuring network settings

To add extra hosts to the container's /etc/hosts file, add the network property to the parameter object and assign a hosts property to it.

dockWorker.start({
  image: 'hello-world',
  name: 'myContainer',
  network: {
    hosts: [
      { name: 'example.com', ip: '192.168.0.1' }
    ]
  }
}, function (err, id) {
  // ...
});

Getting information on running containers

To get information on running containers for a specific image, use the getRunningContainersFor function and provide the image name.

dockWorker.getRunningContainersFor('my-image', function (err, containers) {
  console.log(containers);
  // => [
  //      {
  //        image: 'my-image',
  //        name: 'my-container',
  //        ports: [
  //          { container: 3000, host: 3000 }
  //        ],
  //        env: {
  //          PORT: '3000'
  //        },
  //        volumes: [
  //          { container: '/data', host: '/home/janedoe/foo' }
  //        ],
  //        links: [
  //          { name: 'your-container', alias: 'yours' }
  //        ],
  //        network: {
  //          hosts: [
  //            { name: 'example.com', ip: '192.168.0.1' }
  //          ]
  //        }
  //      }
  //    ]
});

Alternatively you may specify the image name by a regular expression.

dockWorker.getRunningContainersFor(/^my/, function (err, containers) {
  // ...
});

Getting logs of a container

To get the logs of a running container, call the getLogs function and provide the name of the container.

dockWorker.getLogs('myContainer', function (err, stdOut, stdErr) {
  // ...
});

Stopping a container

To stop and automatically remove a running container, call the stop function and provide the name of the container.

dockWorker.stop('myContainer', function (err) {
  // ...
});

Running the build

This module can be built using Grunt. Besides running the tests, this also analyses the code. To run Grunt, go to the folder where you have installed crew and run grunt. You need to have grunt-cli installed.

$ grunt

Before running the test, you need to build the thenativeweb/crew-test image. It is included in this repository. To build it, run the following command.

$ grunt build

License

The MIT License (MIT) Copyright (c) 2014-2015 the native web.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 08 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc