Tiny node containers

11.03.2014

My favorite language at the moment is javascipt. It's fun & functional!

Since I'm also working quite a bit with docker, I've been frustrated with the size of nodejs docker images. A typical node container holds node, npm and all your dependencies. Add a few apt-get's and your quickly looking at > 500 MB.

I even started hacking some Go solely for the ability to compile to a single binary.

Until I found nexe...


I can haz javascript aaaand binary???

Building with nexe

Nexe will compile your node app into a single executable binary. No joke! Have a look!

Since we are now compiling, we need to think about things like compile target. Containers run linux. My desktop runs Darwin. A binary compiled on/for Darwin won't be able run inside a container. So, I made a container for compiling apps with nexe.

docker run -v $(pwd):/app -w /app asbjornenge/nexe-docker -i index.js -o app

Weird bugs

Granted, nexe is a bit flakey atm. I found two main bugs that I had to work around:

A default package.json somehow messes up the executable.
Workaround: I added a build script that will move package.json to pkg.json, build, then move it back.

When passing arguments to a comiled binary, there must exist a first argument.
Workaround: Just pass a random first argument.

Container

When distributing, we can use the simplest container possible, and just add the binary.

FROM debian:jessie
ADD app /usr/bin/app
ENTRYPOINT ["app"]

Diff

I used this approach to build skylink, check out the difference!

      |   normal  |  nexe
 ---------------------------
 size |  640.3 MB | 133.6 MB

Credits

♥ to the nexe folks!
Gif from here.
Thanks!