Home > Mobile >  Why does Dockerfile use different syntax for RUN and CMD?
Why does Dockerfile use different syntax for RUN and CMD?

Time:08-13

Just seems odd that in a Dockerfile a RUN line accepts plain bash like apk add --no-cache python2 g make but the CMD line takes the command like this ["node", "src/index.js"]. What's the reason for this?

CodePudding user response:

RUN, CMD, and ENTRYPOINT all have the same syntax. Both can take a JSON array of individual command words, or a bare string that is interpreted by a shell. The linked Dockerfile documentation shows both forms for all three directives.

For CMD and ENTRYPOINT, there are specific reasons to not want a shell (you may want to be clearer about which process is really process ID 1; you may be trying to use CMD as an argument list to an ENTRYPOINT process). These don't really apply to RUN, though. Conversely, RUN will frequently run multiple commands in sequence, which requires a shell to interpret.

# most common; '&&' requires a shell
RUN apt-get update && apt-get install ...
# also legal; can only run one command, no environment variables or redirects
RUN ["make", "install"]

# both forms of CMD are legal and common
CMD ./manage.py runserver 0.0.0.0:8000
CMD ["./manage.py", "runserver", "0.0.0.0:8000"]

# almost always wants to be JSON-array syntax
ENTRYPOINT ["bundle", "exec"]
# technically legal, but can't be passed a CMD and is hard to override
ENTRYPOINT bundle exec rails server
  • Related