A Pitfall of Writing Shell Scripts on Windows

A Pitfall of Writing Shell Scripts on Windows

This is something I have learned the hard way and most likely a rookie mistake. I have been using Visual Studio Code when tailoring Dockerfile definitions for various projects since Visual Studio Code comes with a very handy Docker extension from Microsoft. You can view, attach, start an interactive shell, etc. to a running container. You can also manage your images in the same interface.

This week I was trying to create a Docker container that would have Cron jobs running. Although I was able to configure the scheduled Cron jobs, I could not get the service start when container is created and running. I couldn't simply put ENTRYPOINT ["cron", "-f"] as I was already using ENTRYPOINT for executing R scripts through Plumber endpoint.

The solution was to use an ENTRYPOINT script. So, I created a new sh file and initialised my script like below.

#!/bin/sh

service cron start

R -e "pr <- plumber::plumb('/app/plumber.R'); pr\$run(host='0.0.0.0', port=8000)"

# Hand off to the CMD
exec "$@"

I thought to myself this should work. Aaaaand nope! Two hours later and all the builds failed with the following error.

standard_init_linux.go:207: exec user process caused "no such file or directory"

I checked all the file/folder paths and everything seemed fine. Then, as I was reading through the errors, I realised something.

And, there it was. Visual Studio Code uses Windows style line-endings (CR LF) by default. My base Docker image is a Debian one. As soon as I changed the line-endings style to unix-style (LF) all the builds passed.

TL;DR If you are writing shell scripts using Visual Studio Code on Windows, make sure to change your line-ending style from CR LF to LF.