Dockerfile-js, render well-formed Dockerfiles.


Keywords
docker, dockerfile, generator
License
ISC
Install
npm install dockerfilejs@1.2.1

Documentation

Build Status Coverage Status

Dockerfilejs

A simple Dockerfile generator. Try it now!

Based on the official Dockerfile Reference

var Dockerfile = require("dockerfilejs").Dockerfile;
var file = new Dockerfile();

file.comment('The above code example yields this file!')
  .env({DEBUG:'express:* node index.js'})
  .expose(8080)
  .separator('\n')
  .from({ image : 'node', tag : 'latest'})
  .comment('FROM gets bumped under initial comments')
  .render();
# The above code example yields this file!
FROM node:latest
ENV DEBUG="express:* node index.js"
EXPOSE 8080
# FROM gets bumped under initial comments

Complex environment? No problem!

file.env({
    complex: {
        objects:  'are not',
        a: 'problem',
        at : {
            all: 'really!'
        }
}}).render();
/*
ENV complex.objects="are not" \
    complex.a=problem \
    complex.at.all=really!
*/

Made a mistake? Get rid of it!

file.copy('~/.ssh/*', '/tmp');
// oops!
file.steps().pop();

Advanced examples

file.separator('\n')
// Will set the separator of each step for the entire file.render() output

file.label({ complex: { objects: 'allowed' } });
// LABEL complex.objects="allowed"

file.expose([8080, '8081', { number: 443, protocol: 'tcp' }]);
// EXPOSE 8080 8081 443/tcp

file.run({ command: ['touch /file.txt', ['echo', 'hello world', '>>', '/file.txt'] ] });
// RUN touch /file.txt \
//   && echo "hello world" >> /file.txt

file.copy({ src : ['/id_rsa', '/id_rsa.pub'], dest: '/root/.ssh/' }, true);
// ONBUILD COPY ["/id_rsa", "/id_rsa.pub", "/root/.ssh"]

file.copy({ src : ['/id_rsa', '/id_rsa.pub'], dest: '/root/.ssh/', onbuild: true });
// ONBUILD COPY ["/id_rsa", "/id_rsa.pub", "/root/.ssh"]

file.copy({ src : ['/id_rsa'], dest: '/root/.ssh/', chown: '0:0' });
// COPY --chown=0:0 ["/id_rsa", "/root/.ssh"]

file.copy({ src : ['/id_rsa'], dest: '/root/.ssh/', user: 0, group: 0 });
// COPY --chown=0:0 ["/id_rsa", "/root/.ssh"]

file.copy({ src : ['/id_rsa'], dest: '/root/.ssh/', chown: { user: 0, group: 0 });
// COPY --chown=0:0 ["/id_rsa", "/root/.ssh"]

file.cmd({ executable: '/bin/bash', params: ['-c', 'hello world'] });
// CMD ["/bin/bash", "-c", "hello world"]

file.cmd({ command:'/bin/bash', params: ['-c', 'hello world'] })
// CMD /bin/bash -c "hello world"

file.healthCheck({
    options: { retries: 4, timeout: '30s'},
    command: 'wget',
    params: ['example.com']
})
// HEALTHCHECK --retries=4 --timeout=30s \
//   CMD wget example.com

file.user('root');
// USER root

Multi-stage builds

file.from({ image: 'node', tag: '10-alpine', name: 'build' })
// FROM node:10-alpine AS build

// ... run your build commands here ...

file.stage()
// Adds a new stage

file.from({ image: 'node', registry: 'docker.io', tag: '10-alpine' })
// Sets the `FROM` instruction in the new stage, etc ...

file.copy({ from: 'build', src: ['./lib'], dest: './lib' })
// COPY --from=build ["./lib", "./lib"]