Home > database >  Docker Build and Docker run `no such file or directory` when file exists as executable
Docker Build and Docker run `no such file or directory` when file exists as executable

Time:10-29

I have a dockerfile that's copying a binary from a previous step. The entrypoint/command does not work however. I can see the file exists though.

FROM rust as rust-builder
WORKDIR /app
COPY Cargo.lock Cargo.lock
COPY Cargo.toml Cargo.toml
COPY src/ src
RUN cargo build --release

FROM alpine:latest
COPY --from=rust-builder /app/target/release/kube-logs-generator /app/kube-logs-generator
RUN ls -la /app
CMD  ["/app/kube-logs-generator"]

This is the output:

Sending build context to Docker daemon  2.159GB
Step 1/10 : FROM rust as rust-builder
 ---> dd3f19acb681
Step 2/10 : WORKDIR /app
 ---> Using cache
 ---> 5e0281f74323
Step 3/10 : COPY Cargo.lock Cargo.lock
 ---> Using cache
 ---> 060b7d6f8349
Step 4/10 : COPY Cargo.toml Cargo.toml
 ---> Using cache
 ---> 56b90bed67d5
Step 5/10 : COPY src/ src
 ---> Using cache
 ---> cdfa52607837
Step 6/10 : RUN cargo build --release
 ---> Using cache
 ---> 2f13c20bbebe
Step 7/10 : FROM alpine:latest
 ---> 9c6f07244728
Step 8/10 : COPY --from=rust-builder /app/target/release/kube-logs-generator /app/kube-logs-generator
 ---> Using cache
 ---> b2158ebfac6f
Step 9/10 : RUN ls -la /app
 ---> Running in cda38e0f4ff0
total 8056
drwxr-xr-x    1 root     root            38 Oct 28 14:07 .
drwxr-xr-x    1 root     root           140 Oct 28 14:14 ..
-rwxr-xr-x    1 root     root       8245984 Oct 28 14:03 kube-logs-generator
Removing intermediate container cda38e0f4ff0
 ---> 6bf0803d98d6
Step 10/10 : CMD  ["/app/kube-logs-generator"]
 ---> Running in 99ba34dd6afa
Removing intermediate container 99ba34dd6afa
 ---> c616fa4a1d55
Successfully built c616fa4a1d55
Successfully tagged bkauffman7/kube-logs-generator:latest 

Then running

docker run bkauffman7/kube-logs-generator

exec /app/kube-logs-generator: no such file or directory

I'm not sure why it thinks the file isn't there.

CodePudding user response:

Your final application stage is using an Alpine-based image, but your builder isn't using an Alpine base. There are some incompatibilities in the base C library stack that can cause "no such file or directory" errors, even when the file plainly exists.

The easiest way to work around this is to use a Debian- or Ubuntu-based image for the final stage. This is somewhat larger, but it includes the standard GNU libc.

FROM rust:bullseye as rust-builder
...
FROM debian:bullseye
COPY --from=rust-builder /app/target/release/kube-logs-generator /app/kube-logs-generator

The Docker Hub rust image includes an Alpine variant, and it also may work to use an Alpine-based image as the builder

FROM rust:alpine3.16 AS rust-builder
...
FROM alpine:3.16

In both cases, I've made sure that both the Linux distribution and their major versions match between the builder and runtime stages.

  • Related