rust使用docker编译并部署

1.使用场景
rust的二进制,在编译时会带入openssl,如果编译和运行的环境不一致,会报openssl的问题,所以最好的处理方法是使用docker编译,并使用docker运行
2.dockerfile

# Compile
FROM    rust:1.75.0-alpine3.18 AS compiler

RUN     apk add -q --update-cache --no-cache build-base openssl-dev cmake
WORKDIR /
ENV     RUSTFLAGS="-C target-feature=-crt-static"
COPY    . .
RUN     set -eux; \
        cargo build --release


# Run
FROM    alpine:3.18

RUN     apk update --quiet \
        && apk add -q --no-cache libgcc tini curl openssl
# add evasrobot and meilitool to the `/bin` so you can run it from anywhere
# and it's easy to find.
COPY    --from=compiler /target/release/evasrobot /evas/evasrobot
COPY    --from=compiler /src/configs/config.yaml /evas/src/configs/
COPY    --from=compiler /src/logs/log4rs_prd.yaml /evas/src/logs/
# To stay compatible with the older version of the container (pre v0.27.0) we're
# going to symlink the evasrobot binary in the path to `/evasrobot`
EXPOSE  9080/tcp

ENTRYPOINT ["tini", "--"]
WORKDIR /evas
CMD   ["./evasrobot"]

多次尝试后,上面的dockerfile可以完成工作
2.1编译
使用 rust:1.75.0-alpine3.18,安装openssl 拷贝源码到镜像,然后执行编译
2.2运行
运行环境也是alpine:3.18,将编译生成物拷贝进去,然后运行,亲测可用。
打出来的镜像也非常小,40M,一个完整的后端项目就全包含了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容