Low Level Lovers

The stories of low layer programming and developments.

DockerfileのCMDがなぜか [ ] 付きだと動かない

コンテナからコマンドを定期実行したいなと思ってこんなDockerfileを書きました。

FROM alpine:3.12
CMD ["crond", "-f", "-d", "8"]

そしたらエラーが出て動かない…

/bin/sh: [crond,: not found

最後の行の [ ] を使わないように書き換えるとなぜか動きました。

CMD crond -f -d 8

動きゃいいやと思考停止してしまいそうになったけど、[ ] を使う形式が推奨される書き方なのになぜだろ・・・と https://docs.docker.com/engine/reference/builder/#cmd を読んでみると…

If you want to run your without a shell then you must express the command as a JSON array and give the full path to the executable.

安易に [ ] を使わなければ動いた!と安心せず、実行可能ファイルをフルパスで指定しろということですね。

CMD ["/usr/sbin/crond", "-f", "-d", "8"]