software engineering

Get container ID from within container

By Adam K Dean on

To get the container ID from within a container, use this:

$ cat /proc/self/cgroup | grep "docker" | sed s/\\//\\n/g | tail -1

I'll probably stick it in an npm module at some point.

Shell settings for safer scripts

By Adam K Dean on

I was just reading through one of Progrium's scripts when I came across set -eo pipefail at the beginning of a script. Having not seen that before, I decided to Google. This is the result of that.

You can use set to manipulate shell variables and functions. Some of these can help you write safer scripts.

set -e

If any command fails, set -e will make the entire script fail, rather than just skipping onto the next line. If you want to allow a line to fail then you can pop || true onto the end of it.

set -u

This will treat unset variables as an error, and immediately exit the script.

set -o pipefail

By default only the last command in a list of piped commands returns a failure code if it fails. By using set -o pipefile, if any of the commands fail, the line will fail. Using this with set -e means that if any command in a piped command fails, the script will fail.

Now back to my reading...

Install Consul on Ubuntu 14.04

By Adam K Dean on

To install Consul on Ubuntu 14.04, first make sure you have unzip available:

$ apt-get install -y unzip

Now, grab the Consul archive, make sure to get the latest & the right architecture, at the time of writing it is 0.5.0, and for Ubuntu it's linux_amd64:

$ wget

Now unzip it.

$ unzip

Now move consul to somewhere in your PATH:

$ mv consul /usr/bin/local/consul

Finally, check it works:

$ consul --version

Consul v0.5.0
Consul Protocol: 2 (Understands back to: 1)

Good job!

Rename Ubuntu droplet

By Adam K Dean on

To rename a Digital Ocean Ubuntu droplet, update /etc/hostname and /etc/hosts, then run:

service restart hostname

You'll need either root access or sudo for this.

ES6 compatible sleep function

By Adam K Dean on

While debugging locally, it can be hard to see how an application runs in the wild as the network has no delay. Quite often, you get around this using a sleep function.

Pre-ES6 generators, you might do this with a callback, maybe using a setTimeout that calls the callback.

function sleep(ms, callback) {
    setTimeout(callback, ms);

With ES6 generators, where you want to yield sleep(1000) etc, you can't use callbacks. What you can do is return a function that takes a single parameter, done, which through closure has access to the parameter you want to pass in, ms. When the returned function is called by whatever cog under the hood calls the returned functions when yielding, your inner function will have access to the ms parameter you passed in, along with a callback that JS passes in, which when called, will continue on from where you yielded the sleep.

function sleep(ms) {
    return function(done) {
        setTimeout(done, ms);

yield sleep(1000);

This is now available on To install:

npm install es6-sleep

To use, let's say while inside some Koa middleware:

var sleep = require('es6-sleep');

app.use(function *() {
    // do something
    yield sleep(1000);
    // continue

Hopefully that makes sense. It does in my head.