rust docker交叉編譯靜態單體執行文件

   1. 這是有人專門做好對編譯對應平臺的docker image(https://github.com/messense/rust-musl-cross).

Rust toolchain Cross Compile Target Docker Image Tag
stable x86_64-unknown-linux-musl x86_64-musl
stable i686-unknown-linux-musl i686-musl
stable arm-unknown-linux-musleabi arm-musleabi
stable arm-unknown-linux-musleabihf arm-musleabihf
stable armv7-unknown-linux-musleabihf armv7-musleabihf
stable armv5te-unknown-linux-musleabi armv5te-musleabi
stable mips-unknown-linux-musl mips-musl
stable mipsel-unknown-linux-musl mipsel-musl

用法:

(1)先拉取對應鏡像 docker pull messense/rust-musl-cross:armv7-musleabihf

(2)爲了方便使用,把docker命令做成別名

alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:armv7-musleabihf'
(3)這樣就可以用"rust-musl-builder"這個命令簡化使用
 rust-musl-builder cargo build --release

(4)上面就和使用普通cargo打包一樣了,調這個命令其它就相當於掛載本地項目到容器中打包。

(5)以上鏡像爲了打包靜態單體,鏡像中已包含了(爲了有這個教程,就是在centos中打包靜態單體時需要openssl,解決了普通openssl的打包,發現靜態打包還需要musl-gcc,而centos找了半天也沒有找到,所以就想到了docker省得自己搭環境了)

  • The standard musl-libc libraries.
  • OpenSSL, which is needed by many Rust applications.

 2.使用另一個鏡像,其它是先找到這個鏡像的(https://github.com/emk/rust-musl-builder

rust-musl-builder uses musl-libc, musl-gcc, and the new rustup target support. It includes static versions of several libraries:

  • The standard musl-libc libraries.
  • OpenSSL, which is needed by many Rust applications.
  • libpq, which is needed for applications that use diesel with PostgreSQL.
  • libz, which is needed by libpq.

 支持緩存構建:

You may be able to speed up build performance by adding the following -v commands to the rust-musl-builder alias:

-v cargo-git:/home/rust/.cargo/git
-v cargo-registry:/home/rust/.cargo/registry
-v target:/home/rust/src/target

You will also need to fix the permissions on the mounted volumes:

rust-musl-builder sudo chown -R rust:rust \
  /home/rust/.cargo/git /home/rust/.cargo/registry /home/rust/src/target

用法(和上面的用法一樣,大部分用docker交叉編譯都類似,鏡像裏幫你做好了編譯環境所需要的一起,尤其是牆的問題更坑):

1. 
alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder'

2. rust-musl-builder cargo build --release

 PS:若在運行編譯時,報權限問題,chmod 777 -R 項目路徑授權。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章