Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / docker

Runninng Docker Commands via Gulp

4.33/5 (2 votes)
14 Jun 2016CPOL3 min read 6K  
There is a saying: Laziness is the mother of all bad habits. However, for us--programmers--I find another saying to be true more often: Laziness is the driver of all progress.While working on a couple projects, which I'll blog about separately in posts to come, I found myself executing the
There is a saying: "Laziness is the mother of all bad habits." However, for us--programmers--I find another saying to be true more often: "Laziness is the driver of all progress."

While working on a couple projects, which I'll blog about separately in posts to come, I found myself executing the same Docker commands over and over again. Being a lazy programmer I decided to automate it, which led to the birth of a new library: docker-cmd-js.


Intro: Beyond the Bits and Bolts 

First, let me take a step back and talk about why I started using Docker in the first place.

The nature of the project I'm currently working on requires a lot of integration tests. These tests run against different databases (relational and nosql, different versions). The amount of databases that I'd need to test against could be rather large. Installing all these databases locally would be painful. Besides I'd have to maintain the databases state for the tests to run properly. With Docker I could start up a virtual machine with any amount of containers with any databases I need and test against these.

To be completely fair, I didn't discover Docker on my own. My good friend Matt Klein, who I recruited to help me with the project suggested I start using Docker. He also wrote an initial draft of docker-cmd-js. At first this code lived inside the project, but I decided to contribute to the community and open source the code.

This proved to be a good decision for several reasons:
  • I had to improve quality of docker-cmd-js to make it presentable.
  • I learned a bit more about TypeScript (yes, the source code is in TypeScript)
  • It forced me to get familiar with publishing packages on NPM (which I found to be very easy).
  • Open sourcing the code also gave me a reason to try my hand at blogging, which I've never done before.


Automating Using Gulp

Gulp is a tool that lets a developer automate tasks. Great! So can I simply create a task that would start a container by writing something like this?

<code>var spawnSync = require('child_process').spawnSync;<br /><br />function run(command) {<br />    var items = command.split(' ');<br />    var r = spawnSync(items[0], items.slice(1));<br />    return {<br />        stdOut: r.stdout.toString(),<br />        stdErr: r.stderr.toString()<br />    }<br />}<br /><br />gulp.task('startVm', ()=> {<br />    return run('docker-machine create --driver virtualbox default');<br />});</code><br />

The idea is right, but this won't work with all Docker commands. It took me a little bit of research to find out that running Docker commands inside cmd.exe on Windows requires setting up a bunch of environment variables beforehand. Besides, using spawnSync, like in the example above has few problems too:
  • The command output won't be seen until the command is executed, which, for lengthy tasks, can be frustrating.
  • The synchronous nature of the code does not let commands run in parallel.
<br />docker-cmd-js takes care of all these issues. It sets all needed environment variables and returns a promise when the command is done, which lets commands run in parallel. To start a virtual machine, write this:

<code>var dockerCmd = require('docker-cmd-js');<br />var cmd = new dockerCmd.Cmd();<br /><br />gulp.task('startVm', (done)=> {<br />    cmd.run('docker-machine create --driver virtualbox default').then(<br />        ()=> { done(); },<br />        (err)=> { done.fail(err); }<br />    );<br />});</code><br />

I also took the liberty and added some "shortcut" methods to the library that will let commands run quicker. For example, the command above will fail if the machine is already running. That's not desired in most cases. I just want to make sure it is running and continue with other Docker commands. For this one could simply write:

<code>gulp.task('startVm', (done)=> {<br />    cmd.machine.start().then(<br />        ()=> { done(); },<br />        (err)=> { done.fail(err); }<br />    );<br />});</code><br />


Gotcha - "mysql" Container Works Only After a Delay!

A lot of programmers have stumbled across this issue. You'd start a container with a simple Dockerfile:

<code>FROM mysql<br /><br />ENV MYSQL_ROOT_PASSWORD=rootPassword<br />ENV MYSQL_USER=testUser<br />ENV MYSQL_PASSWORD=password</code><br />

...and have this container linked with another one (say a nodejs app) only to find out that your app returns an error. It cannot connect to the database. However, if you wait about 7 seconds, the app will connect just fine.

This is due to the fact that mysql service inside the container takes about 7 seconds to spin up after the container starts running. This issue is being tackled. You can track it on Github. Meanwhile I created a method that makes sure of a port availability. This workaround might not work in some situations, but it works great for mysql and similar major use-cases. See the usage below:

<code>cmd.image.build('mysql_test')<br />    .then(() => cmd.container.start('mysql_test', { publish: '3306:3306' }))<br />    .then(() => cmd.container.waitForPort({ port: 3306, timeoutMs: 15 * 1000 })) // 15 seconds<br />    .then(() => runMySqlTests());</code><br />

There are more helpful methods like the one above. See the full list on the Github repository page.
docker-cmd-js is available through NPM: npm install docker-cmd-js.

I hope people will find this package useful. Any feedback or contributions to the code are welcome!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)