node-dockerfile
TypeScript icon, indicating that this package has built-in type declarations

0.7.1 • Public • Published

Node-dockerfile

NPM version Travis build status
A small library for programmatic generation of Dockerfiles using Node.

Node-dockerfile allows you to dynamically create Dockerfile files.
This library was designed to allow us to dynamically create docker images to assist deployment automation.

Written in TypeScript by Carl Winkler

Installation

npm install node-dockerfile --save

Example usage

import { Builder } from 'node-dockerfile'
const dockerfile = new Builder();
 
// Let's just add in a bunch of funky commands for a bit of fun
dockerfile
    .from("node:6")
    .newLine()
    .envs([
        { key: 'NODE_ENV', value: 'developlement' },
        { key: 'APP_ENV', value: 'dev' }
    ])
    .comment("Clone and install dockerfile")
    .run([
        "apt-get install -y git",
        "git clone https://github.com/seikho/node-dockerfile /code/node-dockerfile"
  ])
    .newLine()
    .run(["cd /code/node-dockerfile", "npm install"]);
    .run("npm install -g http-server")
    .env('NODE_ENV', 'production')
    .newLine()
    .workDir("/code/node-dockerfile")
    .cmd("http-server");
    
// .write takes a callback which takes 'error' and 'content'.
// Content being the content of the generated filed.
const cb = function(err, content) {
    if (err) console.log("Failed to write: %s", err);
    else console.log("Successfully wrote the dockerfile!"); 
}
// .write takes 3 arguments: 'location', 'replaceExisting' and the callback above.
 
dockerfile.write(".", true, cb);
 
// If all goes well...
// Console: >> 'Successfully wrote to dockerfile!' 

API

from(image: string)

myFdockerfileile.from("ubuntu:latest");
// FROM ubuntu:latest  

maintainer(maintainerName: string)

dockerfile.maintainer("Carl Winkler");
// MAINTAINER Carl Winkler

run(instructions: string|string[])

dockerfile.run("apt-get intall -y curl");
// RUN apt-get install -y curl
 
// We can create multi-line run commands
dockerfile.run([
    "apt-get install -y git",
    "git clone https://github.com/seikho/node-dockerfile.git"
]);
/**
 * RUN apt-get install -y git \
 * && git clone https://github.com/seikho/node-dockerfile.git
 */

comment(comment: string) Adds a comment to the file

dockerfile.comment("This is a comment");
// # This is a comment

newLine() Adds a new line to the Dockerfile -- purely cosmetic

cmd(instructions: string|string[])

dockerfile.cmd("node --harmony index.js");
// CMD node --harmony index.js
 
dockerfile.cmd(["node", "--harmony", "index.js"]);
// '["node", "--harmony", "index.js"]'

label(key: string, label: string)

dockerfile.label("someLabel", "someValue");
// LABEL someLabel=someValue

expose(port: number)

dockerfile.expose(8080);
// EXPOSE 8080

arg(key: string, value: string)

dockerfile.arg("user", "docker");
// ARG user=docker

envs(pairs: Array<{ key: string, value: string }>)

dockerfile.envs([
        { key: "DOCKER_CERT_PATH", value: "/root/.docker/" },
        { key: "NODE_ENV", value: "DEVELOPMENT" }
    ]);
// ENV DOCKER_CERT_PATH="/root/.docker/" \ NODE_ENV="development"

env(key: string, value: string)

dockerfile.env("DOCKER_CERT_PATH", "/root/.docker/");
// ENV DOCKER_CERT_PATH="/root/.docker/"

add(source: string, destination: string)

dockerfile.add("hom*", "/mydir");
// ADD hom* /mydir/

copy(source: string, destination: string)

// current working directory: /home/carl/projects/node-dockerfile
var dynamicPath = path.resolve("../my-library"); // /home/carl/projects/my-library
dockerfile.copy(dynamicPath, "/code/my-library");
// COPY /home/carl/projects/my-library /code/my-library

entryPoint(instructions: string|string[])

dockerfile.entryPoint("top -b");
// ENTRYPOINT top -b
 
dockerfile.entryPoint(["top","-b"]);
// ENTRYPOINT ["top", "-b"]

volume(volume: string)

dockerfile.volume("/some/volume");
// VOLUME /some/volume

workDir(path: string)

dockerfile.workDir("/some/volume");
// WORKDIR /some/volume

user(user: string)

dockerfile.user("carl");
// USER carl

onBuild(instructions: string)

dockerfile.onBuild("ADD . /app/src");
// ONBUILD ADD . /app/src

write(writeLocation: string, replaceExisting: boolean, callback: (error, content) => void))

dockerfile.write("../my-image", true, function(err, content) {
    if (err) doSomethingElse();
    else doSuccessFunction();
});

writeStream()

var fs = require('fs');
 
dockerfile.writeStream()
      .pipe(fs.createWriteStream('Dockerfile'));

Package Sidebar

Install

npm i node-dockerfile

Weekly Downloads

1

Version

0.7.1

License

MIT

Last publish

Collaborators

  • seikho