Im trying to build a docker image for my RUST TcpListner server, but im encountering the following error when trying to build the image, the rust code will compile correctly alone –
=> ERROR [stage-1 3/3] COPY --from=builder /usr/src/docker_rust_hello/target/release/docker_rust_hello . 0.0s
------
> [stage-1 3/3] COPY --from=builder /usr/src/docker_rust_hello/target/release/docker_rust_hello .:
------
Dockerfile:29
--------------------
27 |
28 | # Copy the build artifacts from the builder image
29 | >>> COPY --from=builder /usr/src/docker_rust_hello/target/release/docker_rust_hello .
30 |
31 | # Expose the port the server will run on
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 5e61c6a8-f6f0-45f7-9c2b-479c85d302bb::bqeyqdo8e3tpbogqxg3jjxpis: "/usr/src/docker_rust_hello/target/release/docker_rust_hello": not found
As stated above, when i compile the code localy with cargo build -release
everything is fine and i can run the code, i can even see the target/release/docker_rust_hello
file in my localy compiled code.
Main.rs –
use std::net::TcpListener;
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
println!("Connection established!");
}
}
and the dockerfile –
# Stage 1: Build the Rust application
FROM rust:latest as builder
# Set the working directory
WORKDIR /usr/src/docker_rust_hello
# Copy the Cargo.toml and Cargo.lock files
COPY Cargo.toml Cargo.lock ./
# Create a dummy main.rs to build dependencies
RUN mkdir src && echo "fn main() {}" > src/main.rs
# Build only dependencies to cache this layer
RUN cargo build --release
# Now copy the actual source code
COPY src ./src
# Build the application
RUN cargo build --release
# Stage 2: Create a smaller image with the built binary
FROM debian:buster-slim
# Set the working directory
WORKDIR /usr/src/docker_rust_hello
# Copy the build artifacts from the builder image
COPY --from=builder /usr/src/docker_rust_hello/target/release/docker_rust_hello .
# Expose the port the server will run on
EXPOSE 3000
# Run the application
CMD ["./docker_rust_hello"]