如何解决M1 Pro上的Docker Buildx错误?

ltqd579y  于 12个月前  发布在  Docker
关注(0)|答案(1)|浏览(189)

我试图在Docker多平台镜像中为Rust“helloworld”webapp构建Docker镜像。然而,我构建linux/amd64镜像总是失败,而linux/arm64构建总是成功。
我的配置:

  • MacBook Pro 2021 M1 Pro 32GB RAM
  • MacOS 12.4
  • Docker for Desktop 4.10.1

我的src/main.rs是:

use actix_web::{get, App, HttpResponse, HttpServer, Responder};

#[get("/hello_world")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(hello))
        .bind(("0.0.0.0", 5000))?
        .run()
        .await
}

字符串
我的cargo.toml

[package]
name = "helloworld"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4"
tokio = { version = "1.19.2", features = ["macros", "rt-multi-thread"] }

[profile.release]
codegen-units=1


我的Dockerfile

FROM rust:latest as builder
WORKDIR /app
COPY . .
RUN apt update && apt install lld clang -y
RUN cargo clean && cargo update
RUN cargo build --release --bin helloworld


FROM debian:bullseye-slim AS runtime
WORKDIR /app
RUN apt-get update -y \
    && apt-get install -y --no-install-recommends openssl ca-certificates \
    && apt-get install -y libpq-dev \
    && apt-get install -y bash \
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/helloworld helloworld
EXPOSE 5000
ENTRYPOINT ["./helloworld"]


然后运行docker buildx build --platform linux/amd64 -t helloword plain .,得到以下错误:

#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x4e54e3)[0x40021864e3]
#25 30.77 /lib/x86_64-linux-gnu/libpthread.so.0(+0x14140)[0x4005fbf140]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZNK4llvm13AttributeList19addAttributeAtIndexERNS_11LLVMContextEjNS_9Attribute8AttrKindE+0xb10)[0x40089725b0]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm26PostOrderFunctionAttrsPass3runERNS_13LazyCallGraph3SCCERNS_15AnalysisManagerIS2_JRS1_EEES5_RNS_17CGSCCUpdateResultE+0x37e)[0x40089edc6e]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(+0x284e8dd)[0x40089ed8dd]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm11PassManagerINS_13LazyCallGraph3SCCENS_15AnalysisManagerIS2_JRS1_EEEJS4_RNS_17CGSCCUpdateResultEEE3runERS2_RS5_S4_S7_+0x482)[0x4008efb052]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(+0x2d5bbbd)[0x4008efabbd]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm21DevirtSCCRepeatedPass3runERNS_13LazyCallGraph3SCCERNS_15AnalysisManagerIS2_JRS1_EEES5_RNS_17CGSCCUpdateResultE+0x23a)[0x4008f461ea]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(+0x2da6f9d)[0x4008f45f9d]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm33ModuleToPostOrderCGSCCPassAdaptor3runERNS_6ModuleERNS_15AnalysisManagerIS1_JEEE+0x107b)[0x4008d05bdb]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(+0x2b65b4d)[0x4008d04b4d]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm11PassManagerINS_6ModuleENS_15AnalysisManagerIS1_JEEEJEE3runERS1_RS3_+0x144)[0x40092d8274]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm24ModuleInlinerWrapperPass3runERNS_6ModuleERNS_15AnalysisManagerIS1_JEEE+0x185)[0x40092d7905]
#25 30.77 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(+0x313876d)[0x40092d776d]
#25 30.78 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm11PassManagerINS_6ModuleENS_15AnalysisManagerIS1_JEEEJEE3runERS1_RS3_+0x144)[0x40092d8274]
#25 30.78 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x2638b01)[0x40042d9b01]
#25 30.78 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x262fb98)[0x40042d0b98]
#25 30.78 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x26300be)[0x40042d10be]
#25 30.78 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x2625cad)[0x40042c6cad]
#25 30.78 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x25f553f)[0x400429653f]
#25 30.78 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x2617e93)[0x40042b8e93]
#25 30.78 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/libstd-69edc9ac8de4d39c.so(rust_metadata_std_8996aeeb2a45fe64+0xab3c3)[0x4005ccb3c3]
#25 30.78 /lib/x86_64-linux-gnu/libpthread.so.0(+0x8ea7)[0x4005fb3ea7]
#25 30.78 /lib/x86_64-linux-gnu/libc.so.6(clone+0x3f)[0x40060d5def]
#25 30.78 qemu: uncaught target signal 11 (Segmentation fault) - core dumped
#25 30.80 error: could not compile `bytes`
#25 30.80 
#25 30.80 Caused by:
#25 30.80   process didn't exit successfully: `rustc --crate-name bytes --edition=2018 /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/bytes-1.2.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C codegen-units=1 --cfg 'feature="default"' --cfg 'feature="std"' -C metadata=6da28169ce33971a -C extra-filename=-6da28169ce33971a --out-dir /app/target/release/deps -L dependency=/app/target/release/deps --cap-lints allow` (signal: 11, SIGSEGV: invalid memory reference)
#25 30.80 warning: build failed, waiting for other jobs to finish...
#25 33.26 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x4e54e3)[0x40021864e3]
#25 33.27 /lib/x86_64-linux-gnu/libpthread.so.0(+0x14140)[0x4005fbf140]
#25 33.27 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/rustc(+0x48941)[0x4000048941]
#25 33.27 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/rustc(+0x9841)[0x4000009841]
#25 33.27 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm9MCContext5resetEv+0x55c)[0x400935c49c]
#25 33.27 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm28MachineModuleInfoWrapperPass14doFinalizationERNS_6ModuleE+0x35)[0x400935bf05]
#25 33.27 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-14-rust-1.62.1-stable.so(_ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE+0xb0)[0x4009442660]
#25 33.28 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x2634c75)[0x40042d5c75]
#25 33.28 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x262ddb7)[0x40042cedb7]
#25 33.28 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x26310c6)[0x40042d20c6]
#25 33.28 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x262be78)[0x40042cce78]
#25 33.28 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x2625d43)[0x40042c6d43]
#25 33.28 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x25f553f)[0x400429653f]
#25 33.28 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-1bde28880ea8bde8.so(+0x2617e93)[0x40042b8e93]
#25 33.28 /usr/local/rustup/toolchains/1.62.1-x86_64-unknown-linux-gnu/bin/../lib/libstd-69edc9ac8de4d39c.so(rust_metadata_std_8996aeeb2a45fe64+0xab3c3)[0x4005ccb3c3]
#25 33.28 /lib/x86_64-linux-gnu/libpthread.so.0(+0x8ea7)[0x4005fb3ea7]
#25 33.28 /lib/x86_64-linux-gnu/libc.so.6(clone+0x3f)[0x40060d5def]
#25 33.28 qemu: uncaught target signal 11 (Segmentation fault) - core dumped
#25 33.29 error: could not compile `typenum`
#25 33.29 
#25 33.29 Caused by:
#25 33.29   process didn't exit successfully: `rustc --crate-name build_script_main --edition=2018 /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/typenum-1.15.0/build/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off -C metadata=ee6956477f56ac1c -C extra-filename=-ee6956477f56ac1c --out-dir /app/target/release/build/typenum-ee6956477f56ac1c -L dependency=/app/target/release/deps --cap-lints allow` (signal: 11, SIGSEGV: invalid memory reference)


我已经被困在这3天了,你知道它可能来自哪里以及如何解决它吗?
非常感谢!

5vf7fwbs

5vf7fwbs1#

您可以根据环境更改Dockerfile。因此,在本地,您将声明一个Linux arm64用于在M1上运行,如果您需要在服务器上使用amd64,请将其放在服务器的Dockerfile中。例如,在文件的顶部:

FROM --platform=arm64 node:18-alpine as base
FROM base AS deps
WORKDIR /app
COPY package.json package-lock.json* pnpm-lock.yaml* ./

RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN npm run build

...

字符串

相关问题