Dockerfile for a Crystal application

Build a space-effective docker image for a Crystal application

Michael Nikitochkin
The Incident Commander
2 min readApr 21, 2021

--

TL;DR: Build an application with static linking and use Alpine base image to reduce the size of the Docker image.

The building process of a Crystal app requires the installation of additional lib packages. Even the result binary is small, the docker image is significant.

Photo by Luke Stackpoole on Unsplash

A straightforward solution is to build a binary with all required libraries, a.k.a. Static Linking. It would allow skipping the installation of extra packages.

Crystal supports Static Linking, but it does not work with glibc (used in mostly all Linux distributives). To build an application with static linking, add the option --static .

So I changed the base image from Ubuntu to Alpine. Alpine uses musl instead of glibc.

Dockerfile

Here is my version of Dockerfile base on Alpine:

Results

DockerHub’s information about the image size (probably it shows the size of new layers):

Here are the sizes of images from my local machine:

As you see, the size of the Ubuntu version was 400MB, when the Alpine version is just 23MB.

References

--

--

Michael Nikitochkin
The Incident Commander

software engineer. like ruby, crystal and golang. play with containers and kubernetes via terraform.