带有Rust代码的Dockerfile-正确的WORKDIR应该是什么

y53ybaqx  于 2023-06-05  发布在  Docker
关注(0)|答案(1)|浏览(487)

我想创建一个Dockerfile来运行我的Rust代码,但是,我想我在WORKDIR上做错了什么。
我的Rust代码具有以下结构:

my_app
├── Dockerfile
├── code
│   ├── Cargo.lock
│   ├── Cargo.toml
│   ├── app1
│   │   ├── Cargo.toml
│   │   ├── benches
│   │   └── src
│   ├── app2
│   │   ├── Cargo.toml
│   │   ├── benches
│   │   └── src
│   └── target
└── dependency_code
    ├── Cargo.toml
    └── src

主Cargo.toml定义为:

[profile.release]
panic = "abort"
opt-level = "s"
overflow-checks = true

[profile.dev]
panic = "abort"

[workspace]

resolver = "2"
members = ['app1', 'app2']

dependecy_code是我在app1和app2中使用的依赖包之一的自定义代码,在它们的app1/Cargo.tomlapp2/Cargo.toml中指定为

[dependencies]
bls12_381 = { path = "../../dependecy_code", default-features = false}

我的Dockerfile定义为

# Use a base image with the 1.66.1 version of Rust installed
FROM rust:1.66.1

# Set the working directory in the container
WORKDIR /my_app/code

# Copy the local application code into the container
COPY . .

# Build the Rust application
RUN cargo build --release

但是,当我尝试以docker build -t my-rust-app .运行它时,我得到以下错误:

[+] Building 0.9s (8/8) FINISHED
 => [internal] load .dockerignore                                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                                              0.0s
 => [internal] load build definition from Dockerfile                                                                                                                         0.0s
 => => transferring dockerfile: 351B                                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/rust:1.66.1                                                                                                               0.5s
 => [1/4] FROM docker.io/library/rust:1.66.1@sha256:23b7815cff2b70148cc7fa732825a5384a347b38217f162525c8f86610a6ae35                                                         0.0s
 => [internal] load build context                                                                                                                                            0.2s
 => => transferring context: 827.43kB                                                                                                                                        0.2s
 => CACHED [2/4] WORKDIR /my_app/code                                                                                                                               0.0s
 => CACHED [3/4] COPY . .                                                                                                                                                    0.0s
 => ERROR [4/4] RUN cargo build --release                                                                                                                                    0.2s
------
 > [4/4] RUN cargo build --release:
#0 0.180 error: could not find `Cargo.toml` in `/my_app/code` or any parent directory
------
Dockerfile:11
--------------------
   9 |
  10 |     # Build the Rust application
  11 | >>> RUN cargo build --release
  12 |
  13 |
--------------------
ERROR: failed to solve: process "/bin/sh -c cargo build --release" did not complete successfully: exit code: 101

我尝试将Dockerfile放在code文件夹中,但docker在加载failed to load manifest for dependency dependency_code时出现问题。有人可以指导我如何修复Dockerfile吗?

oxcyiej7

oxcyiej71#

当您执行COPY . .时,您正在将my_app目录中的所有内容复制到您的WORKDIR /my_app/code。这将导致cargo.toml文件的路径为/my_app/code/code/Cargo.toml,并且docker正在workdir /my_app/code目录中运行cargo build --release,其中没有Cargo.toml文件。
我建议将workdir更改为/my_app,将run命令更改为cargo build --release --manifest-path code/Cargo.toml或类似的命令,以反映Cargo.toml与WORKDIR不在同一目录中

相关问题