【问题标题】:Docker run not working with: failed to load ... permission deniedDocker 运行无法使用:加载失败...权限被拒绝
【发布时间】:2022-01-19 14:28:26
【问题描述】:

我正在尝试在 Digital Ocean(它是一个数据中心)上进行部署,但遇到了阻止部署的问题。

构建过程运行良好。

我的项目 Dockerfile:

####################################################################################################
## Builder
####################################################################################################
FROM rust:latest AS builder

RUN rustup target add x86_64-unknown-linux-musl
RUN apt update && apt install -y musl-tools musl-dev
RUN update-ca-certificates

WORKDIR /myrustapp

COPY ./ .

RUN cargo build --release

RUN chmod -R 777 /myrustapp

####################################################################################################
## Final image
####################################################################################################
FROM scratch

WORKDIR /myrustapp

COPY --from=builder /myrustapp/target/release/myrustapp ./

# RUN chmod -R 777 /myrustapp <- this occure a build crash because sh is not present in scratch image

最后(D.O执行的EQ命令):docker run myimage /myrustapp

结果/错误:

[myrustapp] [2022-01-19 14:14:19] starting container: starting non-root container [/myrustapp]: creating process: failed to load /myrustapp: permission denied

提前非常感谢!

【问题讨论】:

  • /myrustapp 参数在docker run 命令中的作用是什么?
  • 目的是容器中的运行命令,但实际上在Digital Ocean中我有一个字段来指定容器中的启动命令。所以这里只是原始 docker 命令@LeiYang 中等效命令的一个示例
  • 根据您的 Docker 版本,您可以尝试将 --chmod=755 添加到您的 COPY 命令中。
  • 您的 Docker 命令运行用户是否在 docker 组中?我的意思是 sudo usermod -aG docker $USER ...
  • @Jmb Digital Ocean 向我显示:“错误构建图像:解析 dockerfile:Dockerfile 解析错误行 47:未知标志”添加此标志后,但在我自己的系统上为构建端工作,运行端不工作。

标签: docker rust dockerfile containers digital-ocean


【解决方案1】:

所以,我使用 MUSL builder 为我的容器制作了一个新映像,以防止依赖项因划痕或 alpine 映像而丢失。

我的新 Dockerfile:

# Build Stage
FROM ekidd/rust-musl-builder:latest AS builder
# We need to add the source code to the image because `rust-musl-builder`
# assumes a UID of 1000, but TravisCI has switched to 2000.
WORKDIR /myrustapp

ADD --chown=rust:rust . ./

RUN cargo build --release --target=x86_64-unknown-linux-musl

# Bundle Stage
FROM scratch

# If you want SSL for requests
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /myrustapp/target/x86_64-unknown-linux-musl/release/myrustapp /myrustapp

CMD ["/myrustapp"]

实际上,它可以完美运行,但可能无法适应所有依赖项。

如果发生这种情况,@DazWilkin 的解决方案效果很好,但图像会重很多。

我只需要对其进行一些编辑即可使其正常工作(2022 年)。

修改后的版本:

FROM rust:latest as builder

RUN USER=root cargo new --bin myrustapp

WORKDIR /myrustapp

COPY ./Cargo.toml ./Cargo.toml
RUN cargo build --release
RUN rm src/*.rs

ADD . ./

RUN rm ./target/release/deps/myrustapp*

RUN cargo build --release


FROM debian:latest as runtime

WORKDIR /bin

# Copy from builder and rename to 'server'
COPY --from=builder /myrustapp/target/release/myrustapp ./server

RUN apt-get update \
    && apt-get install -y ca-certificates tzdata \
    && rm -rf /var/lib/apt/lists/*

ENV TZ=Etc/UTC \
    USER=appuser

RUN groupadd ${USER} \
    && useradd -g ${USER} ${USER} && \
    chown -R ${USER}:${USER} /bin

USER ${USER}

EXPOSE 8080

ENTRYPOINT ["./server"]

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    • 2017-01-26
    • 1970-01-01
    相关资源
    最近更新 更多