I've written a web-server in nim using the prologue framework. I would like to deploy that application using an alpine-docker-container.
As far as I'm aware, compiling means you dynamically link against your system libraries for whatever you need, that system library on any normal distro being glibc
.
On alpine however you do not use glibc
, you use musl
, so dynamically linking against glibc
means my application will expect glibc functions with glibc
names that do not exist since there are only musl
functions.
The big question that arises out of this for me as a python developer that jumped onto nim and knows very little about compilers:
How do I compile, so that I link dynamically against musl
?
The folks from nim discord's brought me to the answer. It consists of passing flags to the nim-compiler to swap out the compiler nim normally uses for its generated C-code, in order to use musl-gcc
. This can be done by using the --gcc.exe:"musl-gcc"
and --gcc.linkerexe:"musl-gcc"
flags.
Here an example for Linux:
--prefix
being /usr/local
, as that may adversely affect your system. Use somewhere else where it is unlikely to override files, e.g. /usr/local/musl
. This path will further be referred to as <MUSL_INSTALL_PATH>
make && make install
in the unpacked directory<MUSL_INSTALL_PATH>
to your PATH
environment variable--gcc.exe:"musl-gcc"
to swap out gcc with musl-gcc and --gcc.linkerexe:"musl-gcc"
to swap out the default linker with musl-gcc as well. An example can look like this:nim c \
--gcc.exe:"musl-gcc" \
--gcc.linkerexe:"musl-gcc" \
--define:release \
--threads:on \
--mm:orc \
--deepcopy:on \
--define:lto \
--outdir:"." \
<PATH_TO_YOUR_MAIN_PROJECT_FILE>.nim
This should generate a binary that is dynamically linked against musl and thus can run within an alpine docker container.