How to compile OCaml binary natively with Alpine Docker?

My long standing problem of OCaml on Alpine…
I have the following Dockerfile:

FROM alpine:edge AS builder

# Install the build dependencies
RUN apk add --no-cache make m4 which patch sudo wget git pkgconfig \
	gcc g++ musl-dev linux-headers libffi libffi-dev pcre perl python3 \
	gmp-dev pcre-dev xz-dev gmp xz coreutils \
	ocaml ocaml-compiler-libs ocaml-ocamldoc opam ocaml-findlib

RUN chmod +s `which bwrap`

RUN addgroup -g 1000 -S user && adduser -u 1000 -D -S user -G user
USER user
WORKDIR /home/user

RUN opam init --auto-setup --disable-sandboxing --yes && \
	eval `opam env` && opam install --yes ocamlfind

RUN eval `opam env` && CC=musl-gcc \
  opam switch create 4.10.0+musl+static+flambda && \
  opam switch 4.10.0+musl+static+flambda

But while compiling it gives an error:

STEP 10: RUN eval `opam env` && CC=musl-gcc   opam switch create 4.10.0+musl+static+flambda &&   opam switch 4.10.0+musl+static+flambda

<><> Gathering sources ><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
[ocaml-variants.4.10.0+musl+static+flambda] downloaded from cache at

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
-> installed base-bigarray.base
-> installed base-threads.base
-> installed base-unix.base
[ERROR] The compilation of ocaml-variants failed at "/home/user/.opam/4.10.0+musl+static+flambda/.opam-switch/build/ocaml-variants.4.10.0+musl+static+flambda/./configure --prefix=/home/user/.opam/4.10.0+musl+static+flambda CC=musl-gcc
        CFLAGS=-Os ASPP=musl-gcc -c --enable-flambda LIBS=-static".

#=== ERROR while compiling ocaml-variants.4.10.0+musl+static+flambda ==========#
# context              2.0.5 | linux/x86_64 |  |
# path                 ~/.opam/4.10.0+musl+static+flambda/.opam-switch/build/ocaml-variants.4.10.0+musl+static+flambda
# command              ~/.opam/4.10.0+musl+static+flambda/.opam-switch/build/ocaml-variants.4.10.0+musl+static+flambda/./configure --prefix=/home/user/.opam/4.10.0+musl+static+flambda CC=musl-gcc CFLAGS=-Os ASPP=musl-gcc -c --enable-flambda LIBS=-static
# exit-code            77
# env-file             ~/.opam/log/ocaml-variants-3-d0f245.env
# output-file          ~/.opam/log/ocaml-variants-3-d0f245.out
### output ###
# configure: Configuring OCaml version 4.10.0
# checking build system type... x86_64-pc-linux-musl
# checking host system type... x86_64-pc-linux-musl
# checking target system type... x86_64-pc-linux-musl
# checking for ld... ld
# checking how to print strings... printf
# checking for gcc... musl-gcc
# checking whether the C compiler works... no
# configure: error: in `/home/user/.opam/4.10.0+musl+static+flambda/.opam-switch/build/ocaml-variants.4.10.0+musl+static+flambda':
# configure: error: C compiler cannot create executables
# See `config.log' for more details

<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
+- The following actions failed
| - build ocaml-variants 4.10.0+musl+static+flambda
+- The following changes have been performed (the rest was aborted)
| - install base-bigarray base
| - install base-threads  base
| - install base-unix     base

Here is the exact reason - it says "musl-gcc not found" (there is indeed no such binary in Alpine, even with musl-dev package installed):

Same result with and without CC=musl-gcc.

Not sure if a bug or I did something wrong.


I found the reason - it assumes that there exist a wrapper script called musl-gcc which doesn’t exist on Alpine, and plain gcc calls should be used instead.
I added ln -s /usr/bin/gcc /usr/bin/musl-gcc and everything worked. Created an issue for this


I think it was discussed already, but I can’t find where. IIRC, it was said that you don’t need the CC=musl-gcc and +musl+static switch on alpine, because that’s already the default compiler and libc there.

edit: found the discussion, but you were part of it, so I suppose that it doesn’t work for you.

1 Like

Yes, I am trying to solve it the right way: