Not understanding what is happening when I try to execute two commands at runtime via CMD directive in `Dockerfile. I assumed that this should work:
CMD ["/etc/init.d/nullmailer", "start", ";", "/usr/sbin/php5-fpm"]
But it's not working. Container has not started. So I had to do it like this:
CMD ["sh", "-c", "/etc/init.d/nullmailer start ; /usr/sbin/php5-fpm"]
I don't understand. Why is that? Why first line is not the right way? Can somebody explain me these "CMD shell format vs JSON format, etc" stuff. In simple words.
Just to note – the same was with command:
directive in docker-compose.yml
, as expected.
Best Answer
I believe the difference might be because the second command does shell processing while the first does not. Per the official documentation, there are the
exec
andshell
forms. Your first command is anexec
form. Theexec
form does not expand environment variables while theshell
form does. It is possible that by using theexec
form the command is failing due to its dependence on shell processing. You can check this by runningdocker logs CONTAINERID
Your second command, the shell form, is equivalent to -
CMD /etc/init.d/nullmailer start ; /usr/sbin/php5-fpm
Excerpts from the documentation -