我试图从Haskell源代码构建静态链接二进制文件,并将此二进制文件复制到最小的Docker镜像,以便我的生产图像尽可能小。
作为一个测试案例,我使用了一个hello world程序:
main = print "Hello world"
test.cabal
文件是由cabal init
生成的默认文件,但我添加了
ghc-options: -static -optc-static -optl-static -optl-threaded
构建我运行
$ 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
要创建我的最小图像,我创建一个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"]
当我尝试构建并运行它时,这不起作用
$ 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
,但无法理解它。 strace
输出位于:http://pastebin.com/SNHT14Z9
我可以从scratch
开始这项工作吗?或者这是不可能的,因为'dlopen'问题
答案 0 :(得分:4)
您遇到了GHC运行时系统的一项功能。即使应用程序是静态的,它也需要一些辅助文件,语言环境文件就是其中之一。
请参阅https://ghc.haskell.org/trac/ghc/ticket/7695和https://ghc.haskell.org/trac/ghc/ticket/10298
如您所见,这将在7.10.2中确定(目前正好在角落之后)。
https://github.com/snoyberg/haskell-scratch图像希望在最小容器中列出您需要的所有文件。