2

For the last couple of hours I have been trying to start my startup.sh script on docker container start, but for some reason it doesn't work.

My Dockerfile to create the image:

FROM armv7/armhf-ubuntu:latest
MAINTAINER Mohammed Noureldin <m.n.e@hotmail.com>
RUN apt update && apt upgrade -y && apt install -y mumble-server
ADD scripts/startup.sh /startup.sh

My startup script:

#!/bin/bash
/etc/init.d/mumble-server start

Nothing happens here, though I tried to create a file inside the script but also nothing happened!

I tried to execute the script directly from command line, but it doesn't work I don't know why:

docker run command:

docker run --name murmur -itd --restart always --network bridge -p 64738:64738 -v /var/lib/mumble-server/ -v /etc/ mnoureldin/murmur:latest /bin/bash -c "bash /startup.sh;/bin/bash"

And here what I get when trying to execute the script manually:

43b9d8dd4116bc605537c7af35ab186ca165ea6e957fab5908d39b2f085edf41
mohammed@server01:~/Dockerfiles/Mumble $ docker attach murmur
root@43b9d8dd4116:/# bash
.dockerenv  boot/       etc/        lib/        mnt/        proc/       run/        srv/        sys/        usr/
bin/        dev/        home/       media/      opt/        root/       sbin/       startup.sh  tmp/        var/
root@43b9d8dd4116:/# bash startup.sh
Usage: /etc/init.d/mumble-server {start|stop|restart|force-reload}

Or when I have an empty line between the two lines of the script I get this error:

root@830193e67fd7:/# bash startup.sh
startup.sh: line 2: $'\r': command not found
Usage: /etc/init.d/mumble-server {start|stop|restart|force-reload}

Could some one explain what is heppening and why it doesn't work?

Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99
  • 1
    The `\r` error means that your shell script uses Windows line breaks (CRLF, or `\r\n`), which confuse Bash. Make sure that you convert your script to Unix/Linux line breaks (LF, or just `\n`) to get rid of that error. Your text editor should be able to convert the file to use LF instead of CRLF... – nwinkler Nov 01 '16 at 15:30
  • @nwinkler That is in the second case, but what about when I don't have spaces? anyway I am trying to convert that as you said – Mohammed Noureldin Nov 01 '16 at 15:32
  • 1
    Try the file with LF instead of CRLF, it might fix the initial problem as well. – nwinkler Nov 01 '16 at 15:32
  • @nwinkler It really did! thanks! Could you post that as an answer? that will be pretty cool if you told me how to fix that directly in windows (I am using notepad++), but I didn't know how to fix that, so I just rewrote the file in vim in linux. thank you very much – Mohammed Noureldin Nov 01 '16 at 15:36
  • Thanks - added an answer for this, also with a link to a question about how to do that in Notepad++. – nwinkler Nov 01 '16 at 15:40

1 Answers1

5

The error is caused by the line endings in your shell script. It looks like you're using Windows line endings (CRLF, or \r\n), where the unexpected r is confusing Bash. Bash only expects LF or \n, hence the error message.

Most programmer text editors have some kind of support for making these changes. Notepad++ has "Edit > EOL Conversion > Unix/OSX Format". Please see EOL conversion in notepad ++ for more info.

Community
  • 1
  • 1
nwinkler
  • 52,665
  • 21
  • 154
  • 168