Issue with line endings when developing on a Windows but running on a UNIX docker container

Viewed 1998

My team are building our first docker based .NET app and have written a bash script that runs as part of the build process (from the dockerfile). We are all on Windows 10 machines.

When the bash script runs, it throws an error: /bin/bash^M: bad interpreter: No such file or directory.

From my research, this appears to be because of line endings being Windows line endings, not UNIX based. I was able to fix this by converting the endings using Notepad++ and the script ran fine. However, when I went into SourceTree to see the changed files, it briefly showed as a changed file (with something saying it couldn't detect changes). Once I clicked the stage file button, it disappeared as if there wasn't any changed files.

How do I resolve this? I can see a line endings setting in GIT but not sure what the right setting should be.

3 Answers

Using dos2unix will help you with this problem. What dos2unix does is it removes the hidden windows characters that you encounter (^M).

Windows-based text editors put special characters at the end of lines to denote a line return or newline. Normally harmless, some applications on a Linux server cannot understand these characters and can cause the service to not respond correctly.

If you don't have dos2unix in your linux machine yet:

Install dos2unix

apt install dos2unix

Run dos2unix on your problematic file

dos2unix your_problematic_file.txt

You are probably looking for the .gitattributes file.

It allows you to set in which files you want which line endings.

Read more here: Documentation

Add a .gitattributes file to the top of your repository with the following contents.

* text=auto
*.sh text eol=lf
*.conf text eol=lf

Source

Related