我试图从Haskell源代码构建一个静态链接的二进制文件,并将此二进制文件复制到最小的Docker映像,以使我的生产映像尽可能小。Docker中的静态链接Haskell程序
由于我使用的是一个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'问题,这是不可能的?
您可能对[本文]感兴趣(https://www.fpcomplete.com/blog/2015/05/haskell-web-server-in-5mb)。 – Zeta
这篇文章实际上是我决定尝试重新创建相同的东西,但使用静态二进制文件的原因。我确实尝试从'dysinger/haskell-scratch'开始运行我的静态二进制文件,但是这会导致错误代码139。 – sgillis
对此不确定,因为您已经使用了'scratch',而不是'haskell-scratch',如文章中所述,请参阅https://github.com/snoyberg/haskell-scratch。 – Zeta