Ral*_*ann 3 linux rust docker arm64 dockerfile
我尝试使用小型 Rust 应用程序创建 Docker 映像。我想在 Raspberry Pi 4b 上运行的 Kubernetes 集群上运行它。所以图像一定是linux/arm64/v8
。
我在 macOS 上使用以下命令创建图像:
$ docker build --platform linux/arm64/v8 -t dasralph/ping:arm64_1.0.4 .
Run Code Online (Sandbox Code Playgroud)
但是当我在 Raspberry Pi 上运行它时,找不到执行程序:
$ sudo docker run dasralph/ping:arm64_1.0.4
Unable to find image 'dasralph/ping:arm64_1.0.4' locally
arm64_1.0.4: Pulling from dasralph/ping
4f4fb700ef54: Pull complete
38f252ce47e1: Pull complete
Digest: sha256:4fbda499e0552bf08bf230db56906d185bd340655c0cc741ad10ee0ea642c626
Status: Downloaded newer image for dasralph/ping:arm64_1.0.4
exec /ping: no such file or directory
Run Code Online (Sandbox Code Playgroud)
这是我的 Docker 文件:
# STAGE 1 is to build the binary
# Use rust-based image for container
FROM rust:1.61 AS builder
# Adding necessary packages
RUN apt update && apt upgrade -y
RUN apt install -y g++-aarch64-linux-gnu libc6-dev-arm64-cross
RUN rustup target add aarch64-unknown-linux-gnu
RUN rustup toolchain install stable-aarch64-unknown-linux-gnu
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \
CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc \
CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++
# Set working directory in container; make directory if not exists
RUN mkdir -p /usr/src/ping
WORKDIR /usr/src/ping
# Copy all Cargo files from local computer to container
COPY Cargo.toml .
COPY Cargo.lock .
COPY .env.docker .env
COPY src src
# Build release application
RUN cargo build --target aarch64-unknown-linux-gnu --release
# STAGE 2 is to have smallest image possible by including only necessary binary
# Use smallest base image
FROM shinsenter/scratch
# Copy application binary from STAGE 1 image to STAGE 2 image
COPY --from=builder /usr/src/ping/target/aarch64-unknown-linux-gnu/release/ping /
EXPOSE 8080
ENTRYPOINT ["/ping"]
Run Code Online (Sandbox Code Playgroud)
有人暗示出了什么问题吗?
Cargo.toml
[package]
name = "ping"
version = "0.2.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4"
load-dotenv = "0.1.2"
Run Code Online (Sandbox Code Playgroud)
main.rs
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
use std::env;
use load_dotenv::load_dotenv;
load_dotenv!();
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let bind_address = env!("BIND_ADDRESS", "BIND_ADDRESS must be set");
println!("BIND_ADDRESS: {bind_address}");
HttpServer::new(|| {
App::new()
.service(hello)
})
.bind((bind_address, 8080))?
.run()
.await
}
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello Ralph!")
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,我需要确保Rust 编译成静态二进制文件。看起来 MUSL 是实现这一目标的一种方法。
这是我更新后的 Dockerfile:
# Build: docker build --platform linux/arm64/v8 -t dasralph/ping:arm64_0.1.0 --push .
# Run: docker run -p 8080:8080 ping
# Test: curl http://localhost:8080/
# STAGE 1 is to build the binary
# Use rust-based image for container
FROM rust:1.61.0-alpine AS builder
# Adding necessary packages
RUN apk update
RUN apk add pkgconfig openssl openssl-dev musl-dev
RUN rustup target add aarch64-unknown-linux-musl
RUN rustup toolchain install stable-aarch64-unknown-linux-musl
# Set working directory in container; make directory if not exists
RUN mkdir -p /usr/src/ping
WORKDIR /usr/src/ping
# Copy all files from local computer to container
COPY Cargo.toml .
COPY Cargo.lock .
COPY .env.docker .env
COPY src src
# Build release application
RUN cargo build --target aarch64-unknown-linux-musl --release
# STAGE 2 is to have smallest image possible by including only necessary binary
# Use smallest base image
FROM shinsenter/scratch
# Copy application binary from STAGE 1 image to STAGE 2 image
COPY --from=builder /usr/src/ping/target/aarch64-unknown-linux-musl/release/ping /
EXPOSE 8080
ENTRYPOINT ["/ping"]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2119 次 |
最近记录: |