Coder Social home page Coder Social logo

grunt-dock's Introduction

grunt-dock

Build Status Code Climate

A Grunt dock for Docker.

Grunt plugin to manage your Docker images & containers.

The main goal of this plugin is to accelerate the development flow with Docker. But it can also be used to deploy your app in a production environment.

Last but not least, Grunt-dock is based on the module Dockerode. Input options can be passed to this module and return values are unchanged.

Why a Grunt plugin?

The idea came when I was developping a Docker container. I used to enter a lot of commands like build/start/kill then cleaning all the stuff and restarting the workflow.

Before Grunt-Dock, to clean Docker images/containers:

docker ps -a -q --filter "status=exited" | xargs docker rm
docker rmi `docker images -q --filter "dangling=true"`

After:

grunt dock:clean

Installation

npm install grunt-dock

Commands

Grunt-dock supports these commands:

  • list images or containers
  • build images
  • push push built images to a Docker registry
  • pull pull images from a Docker registry to one or more hosts
  • clean dangling images and exited containers
  • start/stop/restart/kill/pause/unpause containers
  • logs containers

Grunt configuration

Grunt-Dock supports main and targets level configuration.

Here is a basic Grunt configuration:

dock: {
  options: {
  
    registry: <Docker registry URL>,
    auth : {
      email : <email to use to connect to Docker registry>,
      username : <Docker registry username>,
      password : <Docker registry password>
    }
  
    docker: {
      // docker connection
      // See Dockerode for options
    }
  
    // It is possible to define images in the 'default' grunt option
    // The command will look like 'grunt dock:build'
    images: {
      'dockerapp': { // Name to use for Docker
        dockerfile: 'Dockerfile',
        options: { 
          build:   { /* extra options to docker build   */ },
          create:  { /* extra options to docker create  */ },
          start:   { /* extra options to docker start   */ },
          stop:    { /* extra options to docker stop    */ },
          kill:    { /* extra options to docker kill    */ },
          logs:    { /* extra options to docker logs    */ },
          pause:   { /* extra options to docker pause   */ },
          unpause: { /* extra options to docker unpause */ },
          push:    { /* extra options to docker push */ },
          pull:    [/* Docker connections (one for every node from which to pull */],
          run:     {
                     docker: [/* Docker connections (one for every node from which 
                              the container has to run */],
                     cmd : [/* Commands to execute upon running the container */],
                     create : /* Container create options */,
                     start : /* Containder start options */
          }
        }
      }
    }
  }, // options
  
  dev: {
    options: {
    
      // You can also define images by target
      // This case, to invoke a command: 'grunt dock:dev:build'
      images: {
      'appname': { // Name to use for Docker
           dockerfile: 'Dockerfile',
           options: { 
             build:   { /* extra options to docker build   */ },
             create:  { /* extra options to docker create  */ },
             start:   { /* extra options to docker start   */ },
             stop:    { /* extra options to docker stop    */ },
             kill:    { /* extra options to docker kill    */ },
             logs:    { /* extra options to docker logs    */ },
             pause:   { /* extra options to docker pause   */ },
             unpause: { /* extra options to docker unpause */ }
           }
        }
      }
    }
  } // dev env
} // dock

The property 'dockerfile' supports:

  • tar archive any tarballs supported by Docker (.tar, .tar.gz, .tar.bz2, .tar.xz). It will be passed directly to the Docker server.
  • directory Grunt-Dock will create a tarball (gz compressed) of this directory before sending it to Docker.
  • plain file Grunt-Dock will create a tarball containing 1 "Dockerfile" file.

You can define some specifics options to pass for each commands of each images (start, stop, build, etc.). See Docker API/Dockerode documentations.

To use it, simply enter these commands:

# Build
grunt dock:build
grunt dock:dev:build

# Start, stop, restart, kill, pause, unpause, logs (replace sample action by right one)
grunt dock:start
grunt dock:dev:start

# List
grunt dock:list
grunt dock:dev:list

# Clean
grunt dock:clean
grunt dock:dev:clean

It is possible to only apply a command for a specific image. For instance, to only start the container 'appname', enter:

# Start only appname container
grunt dock:start:appname
# for 'dev' target:
grunt dock:dev:start:appname

list and clean commands allow a third param: image or container to only list/clean this type:

# List only images
grunt dock:list:image
grunt dock:dev:list:image

# List only containers
grunt dock:list:container
grunt dock:dev:list:container

Samples

See the examples directory.

Coming next

  • Add some better tests
  • Add or create some Docker useful features!

Contributing

Pull requests are welcome.

Please update the tests and the documentation.

License

The MIT License (MIT)

Copyright (c) 2014 Johann Troendle

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.

grunt-dock's People

Contributors

jotrdl avatar lmorandini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

grunt-dock's Issues

Errorhandling

Hi,
we had a problem with the build process for the docker image. The docker remote api response the code 200, although the dockerfile does not work properly (In my case the COPY command). Do you know, where the problem is? I think, the plugin don't log or output the response body.

Image is not tagged or rebuilt

I have problem with grunt dock:dev:build command, the image is not tagged and therefore not found when runt grunt dock:dev:start. Tried with grunt-dock: 1.0.2 and 1.0.1 , nodejs 0.10 and 0.12

Runing docker build -t myname . works fine. And after this grunt dock:dev:start works fine.

Also I noticed that changing Dockerfile doesn't trigger image rebuild on grunt dock:dev:build

My Gruntfile.js

 dock: {
      dev: {
    options: {
            docker: {                                                                                                              
                protocol: 'https',                                                                                                 
                host: '192.168.99.100',                                                                                            
                port: '2376',                                                                                                      

                ca: fs.readFileSync(dockerMachines.dev.caPath),                                                                    
                cert: fs.readFileSync(dockerMachines.dev.certPath),                                                                
                key: fs.readFileSync(dockerMachines.dev.keyPath)                                                                   
            },                                                                                                                     
            images: {                                                                                                              
                'myname': {                                                                                                        
                    dockerfile: 'Dockerfile'                                                                                       
        }
            }                                                                                                                      
        }                                                                                                                          
      } 
    }

output from grunt dock:dev:build (which shows it is not using my current Dockerfile)

└─▪ grunt dock:dev:build
Running "dock:dev:build" (dock) task

Building image [myname]
Step 0 : FROM dockerfile/nodejs
 ---> 3a387fc48869
Step 1 : RUN npm install -g bower grunt-cli
 ---> Using cache
 ---> fe7e745bfeec
Step 2 : COPY ./dist /app
>> Build successfuly done.

Done, without errors.

Dockerfile:

FROM dockerfile/nodejs

RUN npm install -g bower grunt-cli

COPY ./dist /app

ADD start.sh /start.sh
ADD start.sh /start.sh2 #trigger a change, should rebuild the image

EXPOSE 8080

CMD ["/start.sh"]

Support auths from config file

hi,

would it be possible to auto populate auth data from:
$HOME/.docker/config.json

the reason is mostly so that a single user do not have to encode auth details in a shared Gruntfile.js

or is there some other way?

Tasks not responsive

When I run any task like grunt dock:list, there is no result coming out. It seems like the plugin is not working.

My Gruntfile.js:

var fs = require('fs'),
    path = require('path'),
    utils = require('grunt-dock/lib/utils');

module.exports = function(grunt) {
  require('load-grunt-tasks')(grunt);

  var caPath   = path.resolve(utils.getUserHome(), '.boot2docker/certs/boot2docker-vm/', 'ca.pem'),
      certPath = path.resolve(utils.getUserHome(), '.boot2docker/certs/boot2docker-vm/', 'cert.pem'),
      keyPath  = path.resolve(utils.getUserHome(), '.boot2docker/certs/boot2docker-vm/', 'key.pem');

  grunt.initConfig({
    dock: {
      options: {

        // Docker connection options
        // For this example, assume it is a Boot2Docker config.
        // By default, Boot2Docker only accepts secure connection.
        docker: {
          protocol: 'https',
          host: '192.168.59.99',
          port: '2376',

          ca: fs.readFileSync(caPath),
          cert: fs.readFileSync(certPath),
          key: fs.readFileSync(keyPath)
        },

        images: {
          // The 'simple' image
          'tjwudi/wudi-link': {
            // The Dockerfile to use
            dockerfile: './Dockerfile',

            // Options for dockerode
            options: {

              // When starting the container:
              // Bind the container port to the host (same port)
              // + 
              // Bind the './bundle' directory to the '/bundle' container one
              start:  {
                "PortBindings": { "3000/tcp": [ { "HostPort": "8080" } ] },
                "Binds":[__dirname + ":/src"]
              },

              // For the logs command, we want to display stdout
              logs: { stdout: true }
            }
          }
        }
      }
    }
  });

  require('grunt-dock/tasks/dock')(grunt);
};

Vulnerability report

We are a group of researchers from Leiden University, and we conduct research on vulnerabilities in open-source software. We have discovered and verified a high-severity vulnerability in your project(JoTrdl/grunt-dock). Explaining the vulnerability further in this issue could allow malicious users to access details, so we recommend enabling private vulnerability reporting on GitHub to discuss this matter confidentially.
After you have enabled this feature, please add a comment to this issue so we can continue our discussion. If you have any questions, feel free to leave a reply here or send an email to: j.akhoundali [at] liacs.leidenuniv.nl

dockerignore file not acknowledged

When passing a build command through grunt-dock, and specifying a directory as the dockerfile argument, the .dockerignore file is not taken into consideration (ergo, all files are transferred as docker context). Issuing a standard "docker build ..." command in the specified directory works just fine.

Sample configuration for grunt-dock:

images: {
    'IMAGE NAME: {
        dockerfile: '.',
        options: {
            build: {
                t: 'IMAGE TAG'
            }
        }
    }
}

So, again, running this ignores any .dockerignore files in the current directory.

Run and Push/Pull commands

It would be useful to also have Run (for watch like tasks) and Push/Pull commands. Push/Pull would be useful for deployment tasks (or? etc...).

If I have some time I can create some pull requests for this tasks.

ADD and COPY commands short-circuts the build

I'm trying to build a docker image with grunt-dock but whenever I get to a ADD or COPY step the build is aborted and >> Build successfuly done is printed.

This is an example Dockerfile

FROM dockerfile/nodejs
MAINTAINER Torbjørn Vatn <[email protected]>

RUN npm install -g grunt-cli

ADD package.json /data/package.json

RUN ls

Working directory not same as Dockerfile

Hi,

I have a simple, 3-line Dockerfile. The last line is a simple "COPY" command that takes the "dist" directory and drops it into the image container's /usr/share/nginx/html directory. However, when I execute this file I get the following output and error:

Building image [foo]
Step 1 : FROM nginx
 ---> b4c1ea3cfac2
Step 2 : MAINTAINER Matt Titmus
 ---> Using cache
 ---> 16548937824d
Step 3 : COPY dist /usr/share/nginx/html
Warning: lstat dist: no such file or directory� Use --force to continue.

The dist directory definitely exists. :)

Is there a way to define the working directory for the purposes of the docker build? Alternatively, can the plugin define it by default as the directory that the Dockerfile lives in?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.