我試圖從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