Following best practices
Next, let's improve our Dockerfile by applying best practices.
Shell versus exec forms
The RUN
, CMD
, and ENTRYPOINT
Dockerfile instructions are all used to run commands. However, there are two ways to specify the command to run:
- shell form;
RUN yarn run build
: The command is run inside a new shell process, which, by default, is/bin/sh -c
on Linux andcmd /S /C
on Windows - exec form;
RUN ["yarn", "run", "build"]
: The command is not run inside a new shell process
The shell form exists to allow you to use shell processing features like variable substitution and to chain multiple commands together. However, not every command requires these features. In those cases, you should use the exec form.
When shell processing is not required, the exec form is preferred because it saves resources by running one less process (the shell process).
We can demonstrate this by using ps, which is a Linux command-line tool that shows you a snapshot of the current processes. First, let’s enter...