0

My dockerfile looks as below:

FROM ubuntu
ENTRYPOINT echo
CMD ["helloworld"]

The container built from the above dockerfile image is giving a blank output.

[root@dockerhost dproj]# docker run -it --name con1 demo

[root@dockerhost dproj]#

Expected Output: helloworld

meallhour
  • 13,921
  • 21
  • 60
  • 117
  • `The container built` Soooo did you run it? `Expected Output: echo helloworld` Why should `echo` be in the output? – KamilCuk Feb 05 '23 at 21:48

1 Answers1

2

From https://docs.docker.com/engine/reference/builder/#entrypoint :

The shell form: ENTRYPOINT command param1 param2

The shell form prevents any CMD or run command line arguments from being used, but has the disadvantage that your ENTRYPOINT will be started as a subcommand of /bin/sh -c

From man sh:

   -c        Read  commands  from the command_string operand. Set the value of special parameter 0 (see Section 2.5.2, Special Parameters) from the
             value of the command_name operand and the positional parameters ($1, $2, and so on) in sequence from the remaining argument  operands.
             No commands shall be read from the standard input.

/bin/sh -c echo helloworld should assign helloworld to shells $0 and execute echo, which will output an empty line.

I.e. ENTRYPOINT ["echo"] is not ENTRYPOINT echo.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • can you please help me understand what is meant by ` _shells_ `$0`?` – meallhour Feb 05 '23 at 22:02
  • `/bin/sh` processes. `/bin/sh` is the shell. I.e. `/bin/sh -c 'echo $0' this_is_the_zero_arg`. – KamilCuk Feb 05 '23 at 22:04
  • actually, I am not able to understand what is meant by `/bin/sh processes?` thanks for all the details provided so far. Basically need help with understanding "`/bin/sh -c echo helloworld` should assign `helloworld` to _shells_ `$0` and execute `echo`." – meallhour Feb 05 '23 at 22:11
  • 1
    https://unix.stackexchange.com/questions/144514/add-arguments-to-bash-c https://unix.stackexchange.com/questions/152391/bash-c-with-positional-parameters https://linuxhint.com/0-bash-script/ https://unix.stackexchange.com/questions/412707/why-0-is-not-a-positional-parameter https://stackoverflow.com/questions/29258603/what-do-0-1-2-mean-in-shell-script – KamilCuk Feb 05 '23 at 22:59
  • 1
    @meallhour In `sh -c "echo" helloworld`, only the thing _directly after `-c`_ -- in this case, the `echo` -- is a script to run. It would need to be `sh -c 'echo helloworld'` for the `helloworld` to be part of that script. Instead, it goes into the argument vector, but the command `echo` doesn't _look_ at the calling shell's argument vector. – Charles Duffy Feb 06 '23 at 01:16
  • 1
    @meallhour, Docker and many other programs (including common versions of `ps`!) confuse this point when they don't add quoting to show how arguments are (or aren't) held together when printing command lines, but what KamilCuk and I are telling you is what happens under the hood; you can look at `/proc/*/cmdline` files with a tool that prints NUL characters, so you can distinguish where there's a NUL and where there's a regular space. (When it's `sh-cecho hello` you get `hello` on output, when it's `sh-cechohello` you don't). – Charles Duffy Feb 06 '23 at 01:17