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

grunt-docker-spawn

v1.2.0

Published

Grunt task to run docker containers

Downloads

7

Readme

grunt-docker-spawn

This grunt task is inspired by grunt-shell-spawn. It supports instantiation of docker containers using dockerode library.

One of the best use of this task is being able to run complex integration tests against docker containers and then avoid dependencies and setups problematics.

The second best use is to be able to test your application against several backend versions without the need of a hard and long installation/uninstall process. Thus, you only need to pull a specific version then run your backend tests (ie: mongo:2.6.6, mongo:3.0.4, ...).

Install

npm install grunt-docker-spawn

Then add the task in your Gruntfile:

grunt.loadNpmTasks('grunt-docker-spawn')

Examples

The general configuration for a specific container is as follows:

containerName: {
  Image: {String}                    // The repository and name of the image to pull from the dockerhub 
  Cmd: [String]                      // The command which will override the default one
  Name: {String}                     // The name you want the new container to have
  Options: {
    tasks: {
      async: true
    },
    startContainerOptions: {Object}  // Directly forwarded option to the [docker API](https://docs.docker.com/reference/api/docker_remote_api/#full-documentation) 
    matchOuput: {function(chunk)}    // Callback called with the standard outputted data of the container as first argument
  }
}

The containerName object is directly forwarded to dockerode (see docker.createContainer).

Here's how you would launch mongodb(in replication), redis and elasticsearch in which mongodb is linked to elasticsearch (for indexation purpose for instance):

/**
* Ensure that service of containers are finished to start.
*/
function _taskSuccessIfMatch(grunt, regex, info) {
  return function(chunk) {
    var done = grunt.task.current.async();
    var out = '' + chunk;
    var started = regex;
    if (started.test(out)) {
      grunt.log.write(info);
      done(true);
    }
  };
}

...

container: {
  options: {
    machine: {
      host: '192.168.99.100',
      port: 2376,
      ca: fs.readFileSync(process.env.DOCKER_CERT_PATH + '/ca.pem', 'utf-8'),
      cert: fs.readFileSync(process.env.DOCKER_CERT_PATH + '/cert.pem', 'utf-8'),
      key: fs.readFileSync(process.env.DOCKER_CERT_PATH + '/key.pem', 'utf-8'),
      pass: 'mypass'
    }
  },
  redis: {
    Image: 'redis:latest',
    Name: 'redis',
    Options: {
      tasks: {
        async: true
      },
      startContainerOptions: { PortBindings: { '6379/tcp': [{ 'HostPort': '23456' }] } },
      matchOuput: _taskSuccessIfMatch(grunt, /on port/, 'Redis server is started')
    }
  },
  mongodb: {
    Image: 'mongo:latest',
    Cmd: 'mongod --replSet replication --smallfiles --oplogSize 128'.split(' '),
    Name: 'mongodb',
    Options: {
      tasks: {
        async: true
      },
      startContainerOptions: { PortBindings: { '27017/tcp': [{ 'HostPort': '23457' }] }, ExtraHosts: ['mongo:127.0.0.1']},
      matchOuput: _taskSuccessIfMatch(grunt, /connections on port 27017/, 'MongoDB server is started')
    }
  },
  elasticsearch: {
    Image: 'elasticsearch:latest',
    Cmd: ['elasticsearch', '-Des.discovery.zen.ping.multicast.enabled=false'],
    Name: 'elasticsearch',
    Options: {
      tasks: {
        async: true
      },
      startContainerOptions: { PortBindings: { '9200/tcp': [{ 'HostPort': '23458' }] }, Links: ['mongodb:mongo'] },
      matchOuput: _taskSuccessIfMatch(grunt, /started/, 'Elasticsearch server is started')
    }
  },
}

/!\ Do not forget to pull containers first if it is not already done. grunt-docker-spawn uses docker start implementation of dockerode, not docker run /!\

The options to give to instanciate a dockerode client can be set at the task level and selected with the docker argument:

grunt container:redis --docker=machine

This will instanciate the dockerode client with the configuration block defined in options.machine. Check the dockerode documentation for possible values.

Pull needed container

This is how you pull a container. Run this task during setup to initialize your environment. Containers must be available in the Docker Hub community repositories.

grunt container:redis:pull

Run a container

This is how you start a container. Run this task at the beginning of your grunt workflow.

grunt container:redis

Remove a container

This is how you remove a container. Run this task at the end of your grunt workflow.

grunt container:redis container:redis:remove

Containers that are not removed will continue running.

Typical grunt tasks for integration tests

grunt.registerTask('spawn-containers', ['container:redis', 'container:mongodb']);
grunt.registerTask('remove-containers', ['container:redis:remove', 'container:mongodb:remove']);
...
grunt.registerTask('test-integration', ['linters', 'spawn-containers', 'run-integration-tests', 'remove-containers']);

License

MIT License(c) Linagora