在Alpine Linux Docker容器中安装运行MLton编译器遇阻,求解决方案
Great question—yes, you absolutely can get MLton running on Alpine Linux in Docker, but you need to work around Alpine's musl libc differences and MLton's pre-built binary limitations. Let's break down your issues and fix them step by step.
Why Your Attempts Failed
- Attempt 1 (Standard Alpine): The pre-compiled MLton binary you downloaded is built for glibc, but Alpine uses musl libc by default. Even though the
mlton-compilefile exists, musl can't execute glibc-linked binaries, which is why you got that confusing "No such file or directory" error. - Attempt 2 (Alpine-glibc): Adding glibc fixed the execution issue, but MLton's pre-built
libmlton.astatic library wasn't compiled with position-independent code (-fPIC). Alpine's GCC defaults to building position-independent executables (PIE), which clashes with non-PIC static libraries—hence the relocation error you saw.
Solution 1: Compile MLton from Source (Most Reliable)
Building MLton directly on Alpine ensures full compatibility with musl libc. Here's a working Dockerfile that does this:
FROM alpine:latest # Install all dependencies needed to build MLton RUN apk add --no-cache bash build-base gmp-dev linux-headers git make # Clone the latest stable MLton source (use the newest tag available) RUN git clone --depth 1 --branch 20210117 https://github.com/MLton/mlton.git /mlton # Build and install MLton WORKDIR /mlton RUN make all && make install # Verify the installation works RUN echo 'print "Hello, world!\n";' > hello-world.sml && mlton hello-world.sml && ./hello-world
I used the 20210117 stable tag here—you can swap this out for a newer release tag if one exists. Compiling from source takes a bit longer, but you end up with a fully musl-native MLton that has no glibc dependencies.
Solution 2: Patch Pre-built MLton for Alpine-glibc
If you'd rather avoid compiling from source, you can tweak MLton's configuration to make it play nice with Alpine-glibc. The fix is to disable PIE in GCC's flags:
FROM frolvlad/alpine-glibc:latest COPY mlton-20130715-1.amd64-linux.tgz / RUN apk add --no-cache bash build-base gmp-dev linux-headers \ && tar xf /mlton-20130715-1.amd64-linux.tgz --directory /usr/ --strip-components 1 \ # Modify MLton's GCC flags to disable PIE && sed -i 's/^\(CCFLAGS = .*\)/\1 -no-pie/' /usr/lib/mlton/targets/self/MLton.Platform.CFLAGS \ # Test the setup && echo 'print "Hello, world!\n";' >> hello-world.sml \ && mlton hello-world.sml && ./hello-world
The -no-pie flag tells GCC to build a non-position-independent executable, which avoids the relocation conflict with MLton's older non-PIC static library.
Quick Note
That 2013 MLton release is pretty outdated—if you can, go with Solution 1 and use a newer source release. You'll get better performance, bug fixes, and better compatibility with modern systems.
内容的提问来源于stack exchange,提问作者Alban Linard

