在Docker中静态链接Haskell程序

我正在尝试从Haskell源代码构build一个静态链接的二进制文件,并将此二进制文件复制到一个最小的Docker映像,以使我的生产映像尽可能小。

作为一个testing用例,我正在使用hello world程序:

main = print "Hello world" 

test.cabal文件是由cabal init生成的默认值,除了我添加的

 ghc-options: -static -optc-static -optl-static -optl-threaded 

build立我运行

 $ docker run -it -v $(pwd):/src haskell:7.10 /bin/bash # cd src # cabal build 

该版本提供了以下错误:

 opt/ghc/7.10.1/lib/ghc-7.10.1/rts/libHSrts.a(Linker.o): In function `internal_dlopen': (.text+0x727): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking 

从我的理解,这意味着我需要确保我有正确版本的glibc为了能够执行二进制文件。 执行二进制工作正常在同一个容器中:

 # ./dist/build/test/test "Hello world" 

它也是静态链接如预期:

 # ldd ./dist/build/test/test not a dynamic executable 

为了创build我的最小图像,我创build了一个Dockerfile( libc.so.6文件从haskell:7.10图像复制而来):

 FROM scratch ADD dist/build/test/test /test ADD libc.so.6 /lib/x86_64-linux-gnu/libc.so.6 CMD ["/test"] 

当我尝试构build并运行它时,这不起作用

 $ docker build -t test . $ docker run -it test test: out of memory (requested 1048576 bytes) 

我尝试了一个从busybox映像(不添加libc.so.6 )开始的同样的事情,但是这也不起作用。 添加到ubuntu:14.04工作(这可能是因为haskell:7.10是基于这个图像)。

我试图在命令上运行strace ,但是没有多less意义。 strace输出在这里: http : //pastebin.com/SNHT14Z9

我可以scratch这项工作吗? 或者这是不可能的,由于“dlopen”问题?

您遇到了GHC运行时系统的function。 即使应用程序是静态的,它需要一些辅助文件,locale文件也是其中之一。

https://ghc.haskell.org/trac/ghc/ticket/7695和https://ghc.haskell.org/trac/ghc/ticket/10298

正如你所看到的那样,这将在7.10.2(现在就在这个angular落之后)得到修正。

https://github.com/snoyberg/haskell-scratch图像希望列出你需要在最小的容器中的所有文件。