我想创建一个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.toml
和app2/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吗?
1条答案
按热度按时间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不在同一目录中