Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 938
ramcrys Avatar asked Aug 17 '18 04:08

ramcrys


People also ask

Is it possible to run R code from a docker image?

Congratulations, you now have a clean Docker image that not only automatically runs your R script whenever a container is started, but also tells you exactly which part of the code it is executing via console messages. Happy docking! I am a Data Scientist at STATWORX and love telling stories with data – the ShinyR the better!

How to fix Docker command not found in Windows 7?

5 docker: command not found Windows 7: Just set the path of docker in system variable Step:1[Click on path -> edit-> paste the docker location] Step:2[Paste the docker location] In my case C:\Program Files\Docker Toolbox.

Why can’t I run Docker on Windows?

One of the issues with Docker (or any Linux/macOS based system) on Windows is the difference in how line endings are handled. Windows ends lines in a carriage return and a linefeed \r\n while Linux and macOS only use a linefeed \n.

How to check Docker version in Windows 7?

5 docker: command not found Windows 7: Just set the path of docker in system variable Step:1[Click on path -> edit-> paste the docker location] Step:2[Paste the docker location] In my case C:\Program Files\Docker Toolbox. now check $ docker version Share Improve this answer Follow edited Dec 2 '20 at 6:58 Menuka Ishan


2 Answers

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.

like image 129
Andrew Grube Avatar answered Sep 18 '22 07:09

Andrew Grube


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.

like image 25
Andriy Kryvtsun Avatar answered Sep 21 '22 07:09

Andriy Kryvtsun