'Docker: $'\r': command not found on Windows

I'm currently having the following Docker setup to run a MySQL 5.7 container on my local (Windows 10) machine:

builds/Dockerfile:

# Base image
FROM mysql:5.7

# Copy starting scripts file
COPY start.sh /root/start.sh

RUN sed -i 's/\r$//' /root/start.sh

# Run necessary services
CMD ["/bin/bash", "/root/start.sh"]

builds/start.sh

#!/bin/sh

cp /etc/mysql/conf.d/temp/* /etc/mysql/conf.d/

/entrypoint.sh mysqld

docker-compose.yml

version: '2'

services:
   db:
     build: ./builds
     container_name: mysql1
     environment:
       MYSQL_ROOT_HOST: '%'
       MYSQL_ROOT_PASSWORD: "test2"
     ports:
       - "13306:3306"
     volumes:
       - ./containerdata:/etc/mysql/conf.d/temp

containerdata: .... contains a MySQL my.cnf file to customize the binding, allowing to connect from host machine etc...

This setup works flawlessly (when running docker-compose up) on my other Linux machine, but fails on this Windows machine with following errors:

mysql1 | /root/start.sh: line 2: $'\r': command not found
mysql1 | cp: cannot stat '/etc/mysql/conf.d/temp/*': No such file or directory
mysql1 | /root/start.sh: line 4: $'\r': command not found
: not foundntrypoint.sh: line 215: exec: mysqld
mysql1 exited with code 127

As you can see, I even tried with using "sed" to replace all '\r' (even though when I open the start.sh file with Notepad++, the line ending is already Unix-LF and there is no '\r'). Even then, this still fails with the above error.

Could you help me to solve this problem ? Thanks in advance for any help.

Edit: I already used "Linux containers", also tried dos2unix instead of sed, in the Dockerfile.



Solution 1:[1]

I was passing for the same problem and I found this error is caused for caracters that only the windows use. To solution this error, write the file shellscript using nano or other linux file editors.

If you use windows 10, you can start a sub system linux to write the shellscript.

Hope this helps

Solution 2:[2]

I was able to easily do this by changing the LF to Unix in Notepad++. Open the file in Notepad++, then look in the bottom right hand corner. You'll see "Windows (CRLF)" change it to "Unix (LF)" and this will resolve the issue.

Solution 3:[3]

I had the similar problem on my Windows machine after git pull updates from the main project repo. To fix it I've put .gitattributes file on my repo root with this line

*.sh    text eol=lf

Actually, it forbids git from changing EOL symbols in *.sh files depending of current OS.

Solution 4:[4]

Edit the EOL (end of line) of your .sh files to LF(unix) using notepad++

Edit -> EOL Conversion -> Unix (LF)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Carlos Henrique
Solution 2 Andrew Grube
Solution 3 Andriy Kryvtsun
Solution 4 Enrico