Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Repository files navigation

Docker container pool

A Node.JS module to manage a pool of docker containers.

Usage

Pool=require('docker-pool')varpool=newPool({// name of poolname: 'my pool',// maximum number of containers to create at any given timemax: 1,// minimum number of containers to keep in pool at any given timemin: 1,// boolean that specifies whether idle containers at or below the min threshold// should be destroyed/re-createdrefreshIdle: true,// max milliseconds a container can go unused before it should be destroyedidleTimeoutMillis: 30000,// frequency to check for idle containersreapIntervalMillis: 1000,// int between 1 and x - if set, acquirers can specify their// relative priority in the queue if no containers are availablepriorityRange: 1,// int number of containers that should be created concurrently - this setting// throttles requests to the docker daemon for container creationcreateConcurrency: 1,// int number of containers that can by disposed of concurrently - this setting// throttles requests to the docker daemon for container stop and removedisposeConcurrency: 1,// int milliseconds after container start that Docker Pool should// wait before returning the container in the acquire callback - this setting// allows some time for the docker container process to startreadyCheck: function(container,callback){// perform test on container to ensure that it is ready to be used,// such as connecting to a port. then invoke the callback to indicate// readiness. if ready == false, use the retry interval so the pool// can test readiness again later. If error != null, then retries will stop and// the container acquisition will fail.callback(null/* error */,true/* ready? */,0/* retry interval in milliseconds */);},// options for containerscontainer: {// every container will run using this image (required)image: 'ubuntu',// string command to run in the container (required)command: 'ncat -e /bin/cat -k -t -l 5555',// array of volume paths (optional)volumes: ['/container/path/to/expose'],// object of volume paths to bind to host filesystem (optional)binds: {'/host/path/to/bind': '/container/path/to/expose'},// ports to expose on the containerports: [5555],// RAM allowed to container in bytes (optional, default 10MB)ramBytes: 10485760,// share of CPU given to container relative to all other containers (optional, default 1)cpuShare: 1,// int seconds to be allowed on stop before container will be killed (optional, default 10)stopTimeout: 10}});// function that can be used to check a container to determine whether it is OK to use,// or if it should be destroyed. This function is called by acquire() before returning a// container from the pool.pool.validate=function(container){// containers older than 60s should not be usedreturnnewDate()-container.startedAt>60000;};// use the acquire function to use a container in the pool. containers that have been// acquired cannot be re-acquired until after they are released.pool.acquire(function(err,container){if(err)thrownewError(err);// use the container however you wantconsole.log('Connect to ncat running in this container at '+container.ip+':5555');// then release the container back to the poolpool.release(container);});process.on('SIGINT',function(){// to clean up your pool's containers, use the drain function// this will stop and remove all containers created by the poolpool.drain(function(){exit();});});

Testing

Docker cannot be run on Mac OS X. For this reason, you must use Vagrant which runs an instance of Ubuntu under a VM.

  • Use vagrant up to build provision and start the Vagrant instance
  • Use vagrant reload to reboot the Vagrant instance (so that the Linux 3.8 kernel will be loaded)
  • SSH to vagrant using vagrant ssh
  • Change to the directory containing this module using cd /vagrant
  • Install dependencies using npm install
  • Run the tests using npm test
  • After the tests run, there should be no containers listed with docker ps -a
  • To stop docker containers running in the vagrant instance, use docker stop $(docker ps -a -q)
  • To remove docker containers running in the vagrant instance, use docker rm $(docker ps -a -q)

About

A Node.JS module to manage a pool of docker containers.

Resources

Stars

8 stars

Watchers

9 watching

Forks

Releases

Packages

Contributors

Languages