How do I replace "\r" line endings when running Docker script on Windows?

3k views Asked by At

I'm using Docker 19 on Windows 10 (using Cygwin to run Docker). I have this web/Dockerfile ...

FROM python:3.7-slim

RUN apt-get update && apt-get install

RUN apt-get install -y dos2unix
RUN apt-get install -y libmariadb-dev-compat libmariadb-dev
RUN apt-get update \
    && apt-get install -y --no-install-recommends gcc \
    && rm -rf /var/lib/apt/lists/*

RUN python -m pip install --upgrade pip

WORKDIR /app/

COPY requirements.txt requirements.txt

COPY entrypoint.sh entrypoint.sh

RUN tr -d '\r' < /app/entrypoint.sh > /app/entrypoint2.sh
RUN python -m pip install -r requirements.txt

RUN grep '\r' /app/entrypoint.sh
RUN dos2unix /app/entrypoint.sh
RUN grep '\r' /app/entrypoint.sh

ENTRYPOINT ["bash", "/app/entrypoint.sh"]

and the entrypoint.sh file referenced looks like

#!/bin/bash
set -e

python manage.py migrate
python manage.py migrate directory
python manage.py docker_init_db_data

exec "$@"

But I guess there are some "\r" line endings that causes running "docker-compose up" on Windows to die. In my above file, I have

RUN dos2unix /app/entrypoint.sh

But I guess this doesn't do it, because running "docker-compose up" results in

web_1     | set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
web_1     | /app/entrypoint.sh: line 3: $'\r': command not found
web_1     | Unknown command: 'migrate\r'. Did you mean migrate?
web_1     | Type 'manage.py help' for usage.

How do I properly replace "\r" line endings in my shell script so that I can properly run my Dockerfile on Windows (and ideally all other) platforms?

2

There are 2 answers

0
Pak Maneth On

I just ran into the same problem, and found out that it is caused by the file end of line break type. On windows most file will be saved in CRLF instead of LF. Change the break type from CRLF to LF would solve the issue.

if you're on vscode you can easily change it at the bottom right.

0
atabak hooshangi On

I had same issue and with typing the commands in one line and separating them with && solved the problem.

in your case it would be:

python manage.py migrate --noinput && python manage.py migrate directory && python manage.py docker_init_db_data &&

hope it will solve your problem.