Docker – Using Multiple Commands in CMD Directive

docker

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 and shell forms. Your first command is an exec form. The exec form does not expand environment variables while the shell form does. It is possible that by using the exec form the command is failing due to its dependence on shell processing. You can check this by running docker 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 -

Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo", "$HOME" ].