Kyle Banks

Building and Running Docker Containers from Node.js via Dockerton

Written by @kylewbanks on Nov 21, 2015.

Today I open sourced Dockerton, a Node.js wrapper library for Docker. In this post, I'll show you how you can generate a Dockerfile, build an image, and run containers from an easy-to-use JavaScript interface.

First up, if you want to check out the source, head over to GitHub.

Installation is easy, simply install via npm and you're good to go:

npm install --save dockerton

Next, we'll require Dockerton and create an instance:

var Dockerton = require('dockerton');

var dockerton = new Dockerton('dockerton-tutorial')
    .from('docker/whalesay', 'latest')
    .run('apt-get -y update && apt-get install -y fortunes')
    .cmd('/usr/games/fortune -a | cowsay');

As you can see, we're going to be recreating the Whalesay example from the Docker website.

Now that we've constructed an instance of Dockerton and issued our commands, we can generate a Dockerfile.

dockerton.dockerfile()
    .then(function(contents) {
        console.log("Generated Dockerfile:");     
        console.log(contents);
    });

Running the code as-is, you should see the following output:

Generated Dockerfile:
FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay

Now that we've generated a Dockerfile, we are all set to build and run an image:

dockerton.dockerfile()
    .then(function(contents) {
        console.log("Generated Dockerfile:");     
        console.log(contents);

        return dockerton.buildImage();
    })
    .then(function(imageDetails) {
        return dockerton.runImage();
    });

Go ahead and run the program again and you should see output similar to:

 _______________________________________ 
/ The reasonable man adapts himself to  \
| the world; the unreasonable one       |
| persists in trying to adapt the world |
| to himself. Therefore all progress    |
| depends on the unreasonable man.      |
|                                       |
\ -- George Bernard Shaw                /
 --------------------------------------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/   

Don't worry if your output doesn't match exactly, we use the fortunes program to randomize what the whale says.

And here's the full example:

var Dockerton = require('dockerton');

var dockerton = new Dockerton('dockerton-tutorial')
    .from('docker/whalesay', 'latest')
    .run('apt-get -y update && apt-get install -y fortunes')
    .cmd('/usr/games/fortune -a | cowsay');

dockerton.dockerfile()
    .then(function(contents) {
        console.log("Generated Dockerfile:");     
        console.log(contents);

        return dockerton.buildImage();
    })
    .then(function(imageDetails) {
        return dockerton.runImage();
    });

Pretty simple right? Dockerton supports all Dockerfile commands, as well as command-line arguments and more. Head over to GitHub for the full documentation, and for more examples.

Let me know if this post was helpful on Twitter @kylewbanks or down below!