Search code examples
dockerdockerfile

Exec form in docker file for CMD with parameter


I'm trying to use the exec form in a dockerfile CMD:

CMD ["node", "main.js"]

But how do I have to handle additional parameters?

CMD npx next start -p $PORT_VALUE

Should it be

CMD ["npx", "next", "start", "-p", "$PORT_VALUE"]

or

CMD ["npx", "next", "start", "-p $PORT_VALUE"]

?


Solution

  • Neither. Variable substitution is done by the shell, so you need to run a shell to do it. I.e.

    CMD npx next start -p $PORT_VALUE 
    

    Or you can run the shell yourself in exec form

    CMD ["/bin/sh", "-c", "npx next start -p $PORT_VALUE"]