跳至內容
GitHub 儲存庫 論壇 RSS 新聞訂閱

基於 Alpine 的 Docker 映像檔

Johannes Müller

Crystal 團隊在 Docker Hub 上提供了已安裝 Crystal 編譯器的 Docker 映像檔,網址為 crystallang/crystal。自 0.13.0 以來的 Crystal 版本都以基於不同版本的 Ubuntu Linux 的 Docker 映像檔形式提供。現在也提供基於 Alpine Linux 的映像檔,從 0.32.1-alpine 開始。Alpine 映像檔比 Ubuntu 映像檔更輕量,0.32.1 的大小為 185 MB vs. 115 MB。節省了一些位元組。

但更重要的是,Alpine Linux 是基於 musl-libc 而不是 gnu-libc,後者是包括 Ubuntu 在內的大多數其他發行版預設使用的。連結到 musl-libc 是目前建立完全靜態連結的 Crystal 二進位檔的唯一方法。Alpine Linux 使這變得相當容易。例如,官方的 Linux Crystal 編譯器建置是在 Alpine Linux 上靜態連結到 musl-libc

適用於 APK(Alpine 的套件管理器)的 Crystal 套件已經存在相當長一段時間,而且 edge 版本通常會很快更新。更新的 Docker 映像檔將在新 Crystal 版本發布後立即提供。而且它們總是基於 Alpine Linux 的穩定版本,但提供最新的 Crystal 版本。

以下是如何使用 Docker 映像檔建置靜態連結的 Hello World 程式的範例

$ echo 'puts "Hello World!"' > hello-world.cr
$ docker run --rm -it -v $PWD:/workspace -w /workspace crystallang/crystal:0.32.1-alpine \
    crystal build hello-world.cr --static
$ ./hello-world
Hello World!
$ ldd hello-world
        statically linked