authors/straight-shoota.jpg Johannes Müller 02 Feb 2020

Alpine-based Docker images

The Crystal team provides Docker images with installed Crystal compiler on Docker Hub at crystallang/crystal. Crystal versions since 0.13.0 are available as Docker images based on different versions of Ubuntu Linux. Images based on Alpine Linux are now also vailable, starting with 0.32.1-alpine. Alpine images are a bit more lightweight than Ubuntu images, for 0.32.1 it’s 185 MB vs. 115 MB. A few bytes saved.

But more importantly, Alpine Linux is based on musl-libc instead of gnu-libc which is used by default on most other distributions, including Ubuntu. Linking against musl-libc is currently the only way to build fully statically linked Crystal binaries. Alpine Linux makes this easy enough. For example, the offical Crystal compiler builds for Linux are statically linked against musl-libc on Alpine Linux.

Crystal packages for APK (Alpine’s package manager) have been available for quite some time and the edge releases are usually updated pretty quickly. Updated Docker images are going to be available immediately on a new Crystal release. And they’re always based on a stable version of Alpine Linux but provide the latest Crystal release.

Here’s an example how the Docker image can be used to build a statically linked Hello World program:

$ echo 'puts "Hello World!"' > hello-world.cr
$ docker run --rm -it -v $PWD:/workspace -w /workspace crystallang/crystal:0.32.1-alpine \
    crystal build hello-world.cr --static
$ ./hello-world
Hello World!
$ ldd hello-world
        statically linked