docker,

Docker forms: SHELL vs EXEC

Feb 26, 2023 · 1 min read · Post a comment

Shell and exec Docker forms present two different ways of specifying the command that need to run once the container is up and running. So, what’s the difference?!

Prerequisites

  • Docker

Solution

The main difference between these two is that the SHELL form executes the specified command in a shell, while the EXEC form executes it directly.

In the SHELL form, the command is passed to the container’s default shell, which is typically /bin/sh or /bin/bash, and then executed as if it were entered from the CLI. This means that the command can benefit from many shell-specific features such as variable expansion, command substitution, and shell redirection. For example, the following SHELL form command would execute the command “echo test” in a shell:

CMD echo test

On the other side, the EXEC form executes directly without any use of shell. This means that the command is passed directly to the OS’s process manager to run. The command is defined as an array of strings, with the first string being the executable and the rest being arguments. For example, the following EXEC form command would execute the command “echo” with the arguments “test”:

CMD ["echo", "test"]

Conclusion

Generally, the choice between SHELL and EXEC forms depends on the requirements of the command being executed. If the command requires shell-specific features, then the SHELL form is necessary. But, if the command is simple and does not require shell-specific features, then the EXEC form is more efficient and arguably secure since it bypasses the shell.

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.