Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockerfile: allow heredocs for CMD instruction #3082

Closed
wants to merge 1 commit into from

Conversation

jedevc
Copy link
Member

@jedevc jedevc commented Sep 5, 2022

Revisits #2265, introducing support for using heredocs for the CMD instruction

I came across a use case for this in #2773. In prototyping jedevc/buildkit-syft-scanner, the last couple of commands were always COPY and then set the CMD to the created script - the heredoc syntax simplifies this:

COPY <<-"EOF" /scan-script.sh
	#!/bin/sh
	...
EOF
CMD sh /scan-script.sh

Heredocs here work identically to the heredocs for RUN, with a couple of differences for shebang scripts:

  • We create a new layer writing the script contents to /mnt/pipes/

    Usually CMD does not create a new layer, however, the file is required to run the script as we would with RUN, since we want to avoid manual parsing of the shebang line.

  • We have to use /mnt/pipes instead of /dev/pipes, since /dev/pipes is mounted at runtime.

Tests would be needed for this, but will hold off until getting some feedback first 😄

This patch introduces support for using heredocs for the CMD instruction -
they work identically to the heredocs for RUN, with a couple of
differences for shebang scripts:

- We create a new layer writing the script contents to /mnt/pipes/

  Usually CMD does not create a new layer, however, the file is required
  to run the script as we would with RUN, since we want to avoid manual
  parsing of the shebang line.

- We have to use /mnt/pipes instead of /dev/pipes, since /dev/pipes is
  mounted at runtime.

Signed-off-by: Justin Chadwell <[email protected]>
@jedevc
Copy link
Member Author

jedevc commented Sep 5, 2022

🤦 🤦 woops, didn't realize that #2265 was already a PR (and not an issue), I prefer the implementation in that one, closing here to re-open there.

@jedevc jedevc closed this Sep 5, 2022
@tonistiigi
Copy link
Member

For your use case, doesn't using the CMD sh /scan-script.sh leave a much clearer message of what is happening in the docker history and docker inspect output?

@jedevc
Copy link
Member Author

jedevc commented Sep 7, 2022

So a Dockerfile:

FROM alpine

CMD <<EOF
#!/bin/sh
echo test
EOF

Would produce:

$ docker history test
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
b042135a1e36   17 minutes ago   CMD ["/bin/sh" "-c" "/mnt/pipes/EOF"] # buil…   16.4kB    buildkit.dockerfile.v0
<missing>      4 weeks ago      /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B        
<missing>      4 weeks ago      /bin/sh -c #(nop) ADD file:2a949686d9886ac7c…   6.2MB     

$ docker inspect test
...
            "Cmd": [
                "/bin/sh",
                "-c",
                "/mnt/pipes/EOF"
            ],
...

Agreed, the final CMD isn't perfect in this case, however, we would normally perform a shell prefix operation, so there is some precedence for (lightly) modifying the CMD in the Dockerfile. I cleaned up the history a little bit in the other PR, but I think seeing the pipe files here is acceptable, since that's what we already have for the RUN history items.

If adding a new layer like this isn't really acceptable at all for CMD we could always support the variations where they are simply passed directly to the shell as command parameters - those keep the Cmd json field completely readable, we'd only have to not support the variant with shebangs.

@tonistiigi
Copy link
Member

It seems like a special case. If the user adds one more line to the Dockerfile instead of using this then the history and inspect output would be cleaner and likely the Dockerfile itself would be easier to understand as to understand what CMD <<eof exactly means you would need to look at the docs. When we add this then it is confusing why the same doesn't work for ENTRYPOINT and the scope keeps growing. When -v /mnt etc. is used the image quite unexpectedly gets undefined entrypoint behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants