bashdockerdocker-composedockerfilevirtualization

Docker gives 'no such file or directory: unknown' on a docker run command


I was able to successfully build a Docker image, via docker build -t foo/bar ..
Here is its Dockerfile:

FROM ubuntu:20.04

COPY benchmark.sh /home/benchmarking-programming-languages/benchmark.sh
CMD [ "/home/benchmarking-programming-languages/benchmark.sh -v" ]

And here is the file benchmark.sh:

#!/usr/bin/env bash
## Nothing here, this is not a typo

However, running it via docker run -it foo/bar gives me the error:

Error invoking remote method 'docker-run-container': Error: (HTTP code 400) unexpected - failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/home/benchmarking-programming-languages/benchmark.sh -v": stat /home/benchmarking-programming-languages/benchmark.sh -v: no such file or directory: unknown

Despite this, when running the image as a container with a shell, via docker run -it foo/bar sh, I can, not only see the file, but execute it with no errors!
Can someone suggest a reason for why the error happens, and how to fix it?


Solution

  • In your Dockerfile you have specified the CMD as

    CMD [ "/home/benchmarking-programming-languages/benchmark.sh -v" ]
    

    This uses the JSON syntax of the CMD instruction, i.e. is an array of strings where the first string is the executable and each following string is a parameter to that executable.

    Since you only have a single string specified docker tries to invoke the executable /home/benchmarking-programming-languages/benchmark.sh -v - i.e. a file named "benchmark.sh -v", containing a space in its name and ending with -v. But what you actually intended to do was to invoke the benchmark.sh script with the -v parameter.

    You can do this by correctly specifying the parameter(s) as separate strings:

    CMD ["/home/benchmarking-programming-languages/benchmark.sh", "-v"]
    

    or by using the shell syntax:

    CMD /home/benchmarking-programming-languages/benchmark.sh -v