Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Container will not start: stat /bin/sh: no such file or directory

Errorstarting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown

Short answer

Docker tried to run your command through /bin/sh and the image has no shell. That happens with scratch and distroless images when CMD, ENTRYPOINT or RUN is written as a plain string. Write the command as a JSON array so no shell is needed, and run build steps in an earlier stage.

  • Applies to Docker Engine 28, Dockerfile syntax 1.x, distroless images
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print how the image starts. A command stored as ["/bin/sh","-c","..."] means the Dockerfile used the string form.

    docker image inspect --format 'entrypoint={{json .Config.Entrypoint}} cmd={{json .Config.Cmd}}' my-image
  2. 2Match what you see, then jump to its fix

    1. 1The inspect output starts with /bin/sh and -cCMD or ENTRYPOINT is written in shell form on an image without a shellFix
    2. 2The build, not the run, fails on a RUN line after FROM scratch or a distroless FROMA RUN instruction sits in a final stage that is scratch or distrolessFix
    3. 3docker run -it my-image sh or docker exec -it ... sh gives the same errorYou ran sh yourself to look inside, and the image has noneFix

What does the error look like?

  • starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown
  • exec: "/bin/sh": stat /bin/sh: no such file or directory

Cause 1 of 3

CMD or ENTRYPOINT is written in shell form on an image without a shell

1Check

Look at the last CMD and ENTRYPOINT lines in the Dockerfile. A plain string such as CMD node server.js is wrapped as /bin/sh -c by Docker. Distroless images contain no shell at all.

grep -n -E '^(CMD|ENTRYPOINT)' Dockerfile

2Fix

Use the exec form, a JSON array with double quotes, with the full path of the program.

# wrong on an image without a shell
CMD /app/server

# right
CMD ["/app/server"]

3Verify

Rebuild and start the container.

docker build -t my-image .
docker run --rm my-image

Confirmed by docs.docker.com

Did this fix it?

Cause 2 of 3

A RUN instruction sits in a final stage that is scratch or distroless

1Check

RUN in shell form always goes through a command shell, so it cannot work in a stage whose base has none.

grep -n -E '^(FROM|RUN)' Dockerfile

2Fix

Do the installing, compiling and file preparation in an earlier stage that has a shell, then copy only the result into the final stage.

FROM golang:1.24 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/server .

FROM scratch
COPY --from=build /out/server /server
CMD ["/server"]

3Verify

Build the image again.

docker build -t my-image .

Confirmed by docs.docker.com

Did this fix it?

Cause 3 of 3

You ran sh yourself to look inside, and the image has none

1Check

Distroless images ship without shells and package managers on purpose, so docker exec and docker run with sh cannot work.

docker run --rm -it --entrypoint sh my-image

2Fix

Attach a debug shell without changing the image with docker debug, or build from the distroless debug variant, which includes a busybox shell.

docker debug my-container

# or in the Dockerfile, for the debug build only
FROM gcr.io/distroless/python3-debian13:debug
# then
docker run --entrypoint=sh -ti my_debug_image

3Verify

A prompt opens inside the container or image.

ls /

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Write CMD and ENTRYPOINT in exec form in every Dockerfile, with absolute paths.
  • Keep build steps in a builder stage and make the final stage copy-only.
  • Keep a :debug variant or docker debug at hand before you need it in an incident.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.