82

I have been trying to create my own busybox base image.

# ./mkimage.sh -t pensu/busybox busybox-static
+ mkdir -p /var/tmp/docker-mkimage.US3tHy0uBQ/rootfs
+ tar --numeric-owner -caf /var/tmp/docker-mkimage.US3tHy0uBQ/rootfs.tar.xz -C /var/tmp/docker-mkimage.US3tHy0uBQ/rootfs '--transform=s,^./,,' .
+ cat > '/var/tmp/docker-mkimage.US3tHy0uBQ/Dockerfile'
+ rm -rf /var/tmp/docker-mkimage.US3tHy0uBQ/rootfs
+ docker build -t pensu/busybox /var/tmp/docker-mkimage.US3tHy0uBQ
Sending build context to Docker daemon 863.2 kB
Sending build context to Docker daemon 
Step 0 : FROM scratch
 ---> 
Step 1 : ADD rootfs.tar.xz /
 ---> 8eac78bfc9d6
Removing intermediate container ad9bbb8f7536
Successfully built 8eac78bfc9d6
+ rm -rf /var/tmp/docker-mkimage.US3tHy0uBQ

I can see the image is available with my docker repo.

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
pensu/busybox       latest              8eac78bfc9d6        7 seconds ago       2.476 MB

But when I try to do docker run, I always get the error:

# docker run -it pensu/busybox /bin/sh
exec: "/bin/sh": stat /bin/sh: no such file or directorytime="2015-04-09T16:03:45+05:30" level="fatal" msg="Error response from daemon: Cannot start container 8fe73b7832193c847d7975175a4be86d1f0b550b6a00b812bd4cdd18fe752468: exec: \"/bin/sh\": stat /bin/sh: no such file or directory" 

I am not able to understand why is it giving that error? Am I doing something wrong? How else can I validate that I am creating a correct image that is in working condition?

Pensu
  • 3,263
  • 10
  • 46
  • 71

8 Answers8

84

After you create image, check it with:

$ docker inspect $image_name 

and check what you have in CMD option. For busy box it should be:

"Cmd": [
     "/bin/sh"
]

Maybe you are overwritting CMD option in your ./mkimage.sh

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
wsl
  • 8,875
  • 1
  • 20
  • 24
  • 1
    "Cmd": [ "/bin/sh", "-c", "#(nop) ADD file:905ed83e56295de9b6eb24708b583e78e4e9a06d3a80184276b55b5db04d70aa in /" ], It is /bin/sh only. – Pensu Apr 09 '15 at 10:01
  • If You want to run container You must provide task which will be performed by container, rigth now docker is executing /bin/sh. For example if you want to run jar inside your container try java -jar yourjar.jar – wsl Apr 09 '15 at 10:11
  • That's what I am thinking about. Is it not possible to get an interactive shell with an busybox image? What else could I run inside a busybox container? – Pensu Apr 09 '15 at 10:14
  • You should be able to run busybox with simple command, as: docker run -d busybox /bin/sh -c "while true; do echo hello world; sleep 1; done". This will run your image in background and will echo hello world each 1 second. – wsl Apr 09 '15 at 11:45
  • Okay, it did create a container, I mean I got a container id, and then it gave me the same error. # docker run -d pensu/busybox /bin/sh -c "while true; do echo hello world; sleep 1; done" d8db2c9c59e9cf36e577342c6ff746b23b7124eb28b94bc3aad4665a9f3d39a5 time="2015-04-10T12:26:02+05:30" level="fatal" msg="Error response from daemon: Cannot start container d8db2c9c59e9cf36e577342c6ff746b23b7124eb28b94bc3aad4665a9f3d39a5: exec: \"/bin/sh\": stat /bin/sh: no such file or directory" – Pensu Apr 10 '15 at 06:02
  • Because your image expects a main class to load your application inside a container, check http://stackoverflow.com/questions/25364940/how-to-create-docker-image-for-local-application-taking-file-and-value-parameter – wsl Apr 10 '15 at 08:39
  • 1
    @wsl, that link is about running Java in docker. This question has nothing to do with Java. – cowlinator Dec 14 '18 at 20:21
  • 1
    The comment above says `"Cmd": [ "/bin/sh", "-c", "#(nop) ADD file:905ed83e56295de9b6eb24708b583e78e4e9a06d3a80184276b55b5db04d70aa in /" ]` - notice the `ADD` stuff; you have a typo in your `Dockerfile`. – tripleee Oct 15 '20 at 09:38
42

I hit this error ("stat /bin/bash: no such file or directory") when running the command:

docker exec -it 80372bc2c41e /bin/bash

The solution was to identify the kind of terminal (or shell) that is available on the container. To do so, I ran:

docker inspect 80372bc2c41e

In the output from that command, I saw:

"Cmd": [
    "/bin/sh",
    "-c",
    "gunicorn -b 0.0.0.0:7082 server.app:app"
],

This tells me that there's a /bin/sh command available, and I was able to connect with:

docker exec -it 80372bc2c41e /bin/sh
duhaime
  • 25,611
  • 17
  • 169
  • 224
40

Using $ docker inspect [imageID] Incase the Image has no /bin/bash in the output, you can use command below: it worked for me perfectly

$ docker exec -it <container id> sh
TrevorDeTutor
  • 683
  • 6
  • 11
  • OCI runtime exec failed: exec failed: unable to start container process: exec: "sh": executable file not found in $PATH: unknown – pszaba Jun 25 '22 at 15:23
16

This error

docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown.

occurs when creating a docker image from base image eg. scratch. This is because the resulting image does not have a shell to execute the image. If your use:

ENV EXECUTABLE hello
cmd [$EXECUTABLE]

in your docker file, docker uses /bin/sh to parse the input string. and hence the error. Inspecting on the image, your will find:

$docker inspect <image-name>
"Entrypoint": [
                "/bin/sh",
                "-c",
                "[$HM_APP]"
            ]

This means that the ENTRYPOINT or CMD arguments will be parsed using /bin/sh -c. The solution that worked for me is to parse the command as a JSON array of string e.g.

cmd ["hello"]

and inspecting the image again:

"Entrypoint": [
                "hello"
            ]

This removes the dependence on /bin/sh the docker app can now execute the binary file. Example:

FROM scratch

# Environmental variables

# Copy files
ADD . /
# Home dir
WORKDIR /bin

EXPOSE 8083
ENTRYPOINT ["hospitalms"]

Hope this helps someone in future.

MUNGAI NJOROGE
  • 1,144
  • 1
  • 12
  • 25
  • 1
    `As of Docker 1.5.0 (specifically, docker/docker#8827), FROM scratch is a no-op in the Dockerfile. Using the scratch “image” signals to the build process that you want the next command in the Dockerfile to be the first filesystem layer in your image.` This means that your container does not contain /bin/sh, or anything else (other than the linux kernel i assume) – cowlinator Dec 14 '18 at 21:00
  • Yes, you don't have a kernel attached to the image. Running any Linux command or docker shell dependent command will fail. – MUNGAI NJOROGE Dec 14 '18 at 21:03
6

On Windows (msys) using Docker Toolbox/Machine, I had to add an extra / before /bin/bash to indicate that it was a *nix filepath.

So, docker run --rm -it <image>:latest //bin/bash

Urchin
  • 395
  • 3
  • 9
5

check your image cmd using the command docker inspect image_name . The output might be like this:

"Cmd": [
    "/bin/bash",
    "-c",
    "#(nop) ",
    "CMD [\"/bin/bash\"]"
],

So use the command docker exec -it container_id /bin/bash. If your cmd output is different like this:

"Cmd": [
    "/bin/sh",
    "-c",
    "#(nop) ",
    "CMD [\"/bin/sh\"]"
],

Use /bin/sh instead of /bin/bash in the command above.

helvete
  • 2,455
  • 13
  • 33
  • 37
sree
  • 51
  • 1
  • 3
1

Explicitly mention the ubuntu version in the docker file which you are trying to RUN,

FROM ubuntu:14.04

Dont use like FROM ubuntu:Latest. This resolved my above "Cannot Start Container: stat /bin/sh: no such file or directory" issue

A.H.
  • 63,967
  • 15
  • 92
  • 126
1

I had a similar problem:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"sh\": executable file not found in $PATH": unknown.

In my case, I know the image works in other places, then was a corrupted local image. I solved the issue removing the image (docker rmi <imagename>) and pulling it again(docker pull <imagename>).

I did a docker system prune too, but I think it's not mandatory.

ton
  • 3,827
  • 1
  • 42
  • 40