3

I'm new with Docker, so sorry if this is a noob question :(

A friend shared with me his project and I'm trying to create the container but I have an error.

Here is my input:

docker build -t project-py -f project-py/prod/Dockerfile .

And here is the error:

 => ERROR [19/27] RUN build.sh                                                                                     0.2s
------
 > [19/27] RUN build.sh:
: invalid optionash: -
------
failed to solve with frontend dockerfile.v0: failed to build LLB: executor failed running [/bin/sh -c build.sh]: runc did not terminate sucessfully

What I understand so far is he doesn't understand this line in my build.sh : #!/bin/bash -e

I have done some test:

  • leave the build.sh file blank = works
  • remove the "-e" = give the error bad interpreter

My friend doesn't have the error...

Here is my Dockerfile:

# based on https://github.com/dockerfiles/django-uwsgi-nginx
FROM amazonlinux

ENV HOME /home/appuser
ENV AWS_DEFAULT_REGION us-east-1

# Install required packages and remove the cache when done
RUN yum update -y && yum install -y \
    amazon-linux-extras awscli git \
    make glibc-devel gcc patch mysql-devel \
    enchant pyOpenSSL python3 python3-devel python3-pip python3-setuptools python-imaging \
    which vim git \
    && yum clean all

RUN cd /tmp && \
    curl -O https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm && \
    rpm -U ./amazon-cloudwatch-agent.rpm && \
    rm amazon-cloudwatch-agent.rpm

RUN amazon-linux-extras enable nginx1 && yum install -y nginx && yum clean all

#breaking this up to benefit from docker caching...
RUN pip3 install --upgrade pip supervisor uwsgitop \
  && useradd -m appuser \
    && chown -R appuser:appuser /home/appuser \
    && mkdir /var/log/project

# this is our log config
COPY project-py/prod/amazon-cloudwatch-agent/amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/config.json
# this is a hacked version of the ctl script that doesn't use systemd/initd to start it
COPY project-py/prod/amazon-cloudwatch-agent/amazon-cloudwatch-agent-ctl /opt/aws/amazon-cloudwatch-agent/bin/

USER appuser

# COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism
# to prevent re-installing all your dependencies when you made a change a line or two in your app.
COPY --chown=appuser:appuser project-py/requirements/production.txt $HOME/project-py/requirements.txt

RUN python3 -m venv $HOME/venv && /bin/bash -c "source $HOME/venv/bin/activate; pip3 install -r $HOME/project-py/requirements.txt"

# add our stuff. break it up so that we can benefit from docker caching
COPY --chown=appuser:appuser project-docs $HOME/project-docs
COPY --chown=appuser:appuser project-static $HOME/project-static
COPY --chown=appuser:appuser project-www $HOME/project-www
COPY --chown=appuser:appuser project-py/assets/fonts $HOME/project-py/assets/fonts
COPY --chown=appuser:appuser project-py/*.py $HOME/project-py/
COPY --chown=appuser:appuser project-py/project $HOME/project-py/project
COPY --chown=appuser:appuser project-py/templates $HOME/project-py/templates
COPY --chown=appuser:appuser project-py/tools $HOME/project-py/tools
COPY project-py/prod/build.sh /usr/local/bin/
RUN build.sh

Here is the build.sh :

#!/bin/bash -e

source $HOME/venv/bin/activate
echo "compiling translations..."
pybabel compile --use-fuzzy -d $HOME/project-py/hourglass/translations

I'm working on W10 build 2004 on a surface Pro 7, with this version of docker :

Client: Docker Engine - Community
 Cloud integration: 1.0.2
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 17:00:27 2020
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:07:04 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.3.7
  GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Thanks a lot for your help

Jordan Matas
  • 102
  • 11
  • Did you save the file with Linux linefeeds? Does `/bin/bash` exist inside your base image? – BMitch Nov 21 '20 at 09:49
  • What is the base image (`FROM ...`) of your Dockerfile? Can you [edit] your post to add a [mcve] of your Dockerfile and part of that sh script that reproduces the error? – Gino Mempin Nov 21 '20 at 10:16
  • my file is save with Linux linefeed( CR LF ) I edit my post to add the Dockerfile and the build.sh Thanks ! – Jordan Matas Nov 21 '20 at 15:22
  • Check you script file for hidden symbols like ^@ or ^M. You could use `vi` or `cat -v filename` for this, as advised in this answer. https://stackoverflow.com/a/17136853/4486909 – Olesya Bolobova Nov 21 '20 at 15:33
  • Linux format is just LF, no CR; the CR+LF format is DOS/Windows style, and [*will* cause problems in the script](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) (and your garbled error message indicates that it's a carriage return it's complaining about). Convert your script (and any other files that might have the same problem) to proper Linux text format (just LF) and try it again. – Gordon Davisson Nov 21 '20 at 16:40

2 Answers2

4

The file appears to be saved with Windows linefeeds (cr+lf). Save the script with Linux linefeeds (lf) since Linux will interpret the carriage return character as part of the command line.

BMitch
  • 231,797
  • 42
  • 475
  • 450
0

In Dockfile add this line before RUN ./setup.sh

RUN cat setup.sh | sed '1 s/\r$//' > setup.sh

basically, it will remove \r from the first line.