jlucktay
jlucktay

Reputation: 432

Proper JSON notation syntax in a Dockerfile when piping output through multiple commands on a `CMD` line?

I am running through a Docker tutorial, and the Dockerfile contains the following line:

CMD /usr/games/fortune -a | cowsay

When using hadolint to lint the file, I get this recommendation:

DL3025 Use arguments JSON notation for CMD and ENTRYPOINT arguments

So I update the CMD line with JSON notation for the arguments:

CMD ["/usr/games/fortune", "-a", "|", "cowsay"]

Now, after I (re)build the image and run it, I get this error:

(null)/|: No such file or directory

What is the correct way to use proper JSON notation syntax when I need to pipe output from one command to another on a CMD line?

Upvotes: 3

Views: 9247

Answers (1)

atline
atline

Reputation: 31654

| is a shell symbol which only works within a shell environment.

CMD command param1 param2 (shell form)

This will work as follows: CMD [ "sh", "-c", "command param1 param2"].

CMD ["executable", "param1", "param2"] (exec form, this is the preferred form)

This will not invoke a shell, so | will not function.

You may reference something from here.

For your situation, you need to use a shell to leverage | so the correct way could be something like this:

CMD ["bash", "-c", "/usr/games/fortune -a | cowsay"]

Upvotes: 11

Related Questions