Docker中静态链接的Haskell程序

sgi*_*lis 7 haskell docker

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

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

main = print "Hello world"
Run Code Online (Sandbox Code Playgroud)

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

ghc-options: -static -optc-static -optl-static -optl-threaded
Run Code Online (Sandbox Code Playgroud)

建立我运行

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

构建提供以下错误:

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
Run Code Online (Sandbox Code Playgroud)

根据我的理解,这意味着我需要确保我有正确版本的glibc才能执行二进制文件.执行二进制文件在同一容器中工作正常:

# ./dist/build/test/test
"Hello world"
Run Code Online (Sandbox Code Playgroud)

它也按预期静态链接:

# ldd ./dist/build/test/test
not a dynamic executable
Run Code Online (Sandbox Code Playgroud)

为了创建我的最小图像,我创建了一个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"]
Run Code Online (Sandbox Code Playgroud)

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

$ docker build -t test .
$ docker run -it test
test: out of memory (requested 1048576 bytes)
Run Code Online (Sandbox Code Playgroud)

我从busybox图像开始尝试了同样的事情(没有添加libc.so.6),但这也没有用.将它添加到已ubuntu:14.04完成的工作中(这可能是因为haskell:7.10基于此图像).

我试着运行strace命令,但无法理解它.该strace输出是在这里:http://pastebin.com/SNHT14Z9

我可以做这个工作scratch吗?或者由于'dlopen'问题,这是不可能的?

pha*_*dej 4

您遇到了 GHC 运行时系统的一个功能。即使应用程序是静态的,它也需要一些辅助文件,区域设置文件就是其中之一。

请参阅https://ghc.haskell.org/trac/ghc/ticket/7695https://ghc.haskell.org/trac/ghc/ticket/10298

正如您所看到的,这将在 7.10.2 中得到修复(目前就在角落后面)。

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