37

I want to understand how CMD and ENTRYPOINT works. So, I just created a very simple Dockerfile

FROM scratch

CMD echo "Hello First"

ENTRYPOINT echo "Hello second" 

Then I build image of this :

docker build -t my_image .

The logs are as below:

Step 1/3 : FROM scratch ---> Step 2/3 : CMD echo "Hello First" ---> Using cache ---> 9f2b6a00982f Step 3/3 : ENTRYPOINT echo "Hello second" ---> Using cache ---> 1bbe520f9526 Successfully built 1bbe520f9526 Successfully tagged my_image:latest SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context w ill have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

When I create a container of this image it returns:

docker run my_image

Error is:

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

Can someone please help me about error?

Emma
  • 27,428
  • 11
  • 44
  • 69
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
  • refer this. https://stackoverflow.com/q/21553353/9025542 – Thilak Feb 22 '19 at 06:08
  • I wanted to [edit] your question to fix the formatting of the Docker build transcript and the error message, but I could not guess what they really looked like. Please use code formatting for computer messages or at the very least don't rewrap them. – tripleee Feb 22 '19 at 06:26

1 Answers1

37

There are two things happening here.

A Dockerfile that starts FROM scratch starts from a base image that has absolutely nothing at all in it. It is totally empty. There is not a set of base tools or libraries or anything else, beyond a couple of device files Docker pushes in for you.

The ENTRYPOINT echo ... command gets rewritten by Docker into ENTRYPOINT ["/bin/sh", "-c", "echo ..."], and causes the CMD to be totally ignored. Unless overridden with docker run --entrypoint, this becomes the main process the container runs.

Since it is a FROM scratch image and contains absolutely nothing at all, it doesn't contain a shell, hence the "/bin/sh: no such file or directory" error.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • 3
    How can I install sh into the image? – Mehraj Malik Feb 23 '19 at 08:39
  • 2
    Building images `FROM scratch` is kind of an advanced topic. `FROM ubuntu:18.04` might give you a more familiar environment. – David Maze Feb 23 '19 at 10:48
  • Hi @DavidMaze, when launching `boot2docker.iso` in a Linux virtual machine running in Virtualbox, `/bin/sh` is there or rather its implementation by `busybox`. How is `FROM scratch` different from starting a released `boot2docker.iso`? – Adam.at.Epsilon Jul 16 '19 at 09:39
  • 2
    Boot2Docker is a virtual machine, not a Docker image. Also there is _nothing_ in `FROM scratch`. Actually nothing. No Busybox. If you need a `/bin/sh` (99%, but not 100%, of images do) there is a [`busybox`](https://hub.docker.com/_/busybox/) image that has it, but usually people start with [`alpine`](https://hub.docker.com/_/alpine/) which can run most compiled programs and is easier to extend. – David Maze Jul 16 '19 at 10:18
  • @DavidMaze thank you. Is it a reasonable assumption that running boot2docker.iso in a Virtualbox yields the same execution environment (for a program or script) as doing `FROM docker` and then launching a script with `RUN`? – Adam.at.Epsilon Jul 25 '19 at 08:51
  • No, they'll be quite different (both in terms of filesystem and other processes running). – David Maze Jul 25 '19 at 09:37