summer4096 / dockerscript

Write your Dockerfile in javascript!
MIT License
22 stars 3 forks source link

Important note

This was kind of a joke... you probably shouldn't actually do this.

dockerscript

Write your Dockerfile in javascript!

Install

npm install -g dockerscript

Example dockerfile.js

from('debian', 'wheezy')

maintainer('you', 'your@email.com')

env('NGINX_VERSION', '1.7.11-1~wheezy')

run`
  apt-key adv --keyserver pgp.mit.edu --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 &&
  echo "deb http://nginx.org/packages/mainline/debian/ wheezy nginx" >> /etc/apt/sources.list &&
  apt-get update &&
  apt-get install -y ca-certificates nginx=$NGINX_VERSION &&
  rm -rf /var/lib/apt/lists/*
`

comment('forward request and error logs to docker log collector')

run`
  ln -sf /dev/stdout /var/log/nginx/access.log &&
  ln -sf /dev/stderr /var/log/nginx/error.log
`

volume('/var/cache/nginx')

expose(80, 443)

cmd("nginx", "-g", "daemon off;")

this turns into:

FROM debian:wheezy

MAINTAINER you <your@email.com>

ENV NGINX_VERSION=1.7.11-1~wheezy

RUN apt-key adv --keyserver pgp.mit.edu --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 && \
    echo "deb http://nginx.org/packages/mainline/debian/ wheezy nginx" >> /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y ca-certificates nginx=$NGINX_VERSION && \
    rm -rf /var/lib/apt/lists/*

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log

VOLUME /var/cache/nginx

EXPOSE 80 443

CMD ["nginx","-g","daemon off;"]

Usage

Run the dockerscript command or use the ds alias.

With no arguments, it looks for a dockerfile.js in the current directory and writes to Dockerfile in the current directory.

Your script will be passed through babel so you're free to use es6.

#Simple usage:
$ ls
dockerfile.js
$ ds
$ ls
Dockerfile  dockerfile.js

#If you want to write to a different file, do this:
$ ds mydockerfile.js Dockerfile

#If you want to write to stdout, do this:
$ ds -
FROM ubuntu
...

#If you want to keep error logs, do this:
$ ds input.js Dockerfile.test dockerscript_errors.log

Globals

The following global functions are available to your dockerscript files:

from(name, [tag])

maintainer(name, [email])

run(...commands)

You can use run five different ways:

run('ls '+dir)
run('ls -l '+dir)
run('ls', '-l', dir)
run(['ls', '-l', dir])
run`ls -l ${dir}`

Using template tags you can easily pass in multiple lines. The linebreaks will be escaped for you.

run`
  ls -l ${dir} &&
  touch ${dir}/somethingElse
`

cmd(...commands)

You can use cmd in all of the same ways that you can use run

expose(...ports)

You can specify one port, or multiple.

expose(80)
expose(80, 443)

env(key, value)

You can use env two ways:

env('NODE_VERSION', '0.12.0')
env({
  NODE_VERSION: '0.12.0',
  NPM_VERSION: '2.5.1'
})

add(src..., dest)

Don't worry about path whitespace, I got this.

copy(src..., dest)

Don't worry about path whitespace, I got this.

entrypoint(...commands)

Works just like run and cmd.

volume(...volumes)

Specify one or more.

user(username)

onbuild(callback)

Use it like this:

onbuild(function(){
  run('echo', 'done building!')
})

The callback function will not be called on build, it's just for structure.

Docker doesn't allow multiple onbuild commands, so if you put more than one thing in here it won't work.

include(path)

Grab a partial dockerscript file and include it. You can use this to break up your dockerfiles into manageable and reusable chunks.

Like require, the path is relative to the currently executing script. Unlike require, it does not return any exports. It's just for partials.

Why?

if (process.env.DOCKER_ENV == 'production') {
  include('./monitoring')
  add('./www', '/var/www')
} else if (process.env.DOCKER_ENV == 'development') {
  include('./debugger')
  volume('/var/www')
}

Contributing

Contributors will be rewarded with a lifetime supply of imaginary pizza. :pizza: :pizza: :pizza:

If you send me a pull request that's good, I'll probably merge it.

Please follow some simple guidelines:

If you need help with any of these things, let me know and I'll do my best to help you out.