Home > OS >  Running Dockerfile with bash script
Running Dockerfile with bash script

Time:09-07

Hi I'm trying to run this following Dockerfile

FROM ubuntu:20.04
ADD install /
RUN chmod u x /install
RUN /install
ENV PATH /root/miniconda3/bin:$PATH
CMD ["ipython"] 

combined with this bash script

#!/bin/bash
apt-get update
apt-get upgrade -y
apt-get install -y bzip2 gcc git htop screen vim wget
apt-get upgrade -y bashapt-get clean
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O Miniconda.sh
bash Miniconda.sh -b
rm -rf Miniconda.sh
export PATH="/root/miniconda3/bin:$PATH"
conda update -y conda python
conda install -y pandas
conda install -y ipython

The Dockerfile and the bash script are in the same folder, really not sure what I'm doing wrong here. This is the error I'm getting:

 $ docker build -t py4fi:basic .
[ ] Building 0.5s (8/8) FINISHED                             
 => [internal] load build definition from Dockerfile    0.0s
 => => transferring dockerfile: 31B                     0.0s 
 => [internal] load .dockerignore                       0.0s 
 => => transferring context: 2B                         0.0s 
 => [internal] load metadata for docker.io/library/ubu  0.0s 
 => [internal] load build context                       0.0s 
 => => transferring context: 434B                       0.0s 
 => [1/4] FROM docker.io/library/ubuntu:20.04           0.0s 
 => CACHED [2/4] ADD install /                          0.0s
 => CACHED [3/4] RUN chmod u x /install                 0.0s 
 => ERROR [4/4] RUN /install                            0.4s 
------                                                       
 > [4/4] RUN /install:
#8 0.416 /bin/sh: 1: /install: not found
------
executor failed running [/bin/sh -c /install]: exit code: 127

Any ideas what I'm doing wrong here?

CodePudding user response:

TL;DR

On all platforms, even containerized, Bash scripts must have Unix-style line endings (LF), except in some cases on Windows, like with sufficiently recent versions Git Bash.

Details

I just reproduced your exact error message when I saved the install file with CRLF line endings on my computer.

On Windows, Git-Bash is patched to tolerate Windows-style CRLF line endings, but on all other platforms Bash only accepts Unix-style LF line endings. When you run install in the docker build, it's using the Bash that is distributed with your base image, where it ends up looking for (and not finding) /bin/bash\0x0D instead of /bin/bash.

The solution is simple, you have to convert the newlines to LF for the install file. (The Dockerfile can have either line endings.) On my computer, VSCode has a CRLF showing at the right of the status bar (see my screen snip below), which I can click to change to LF and save the file again with Unix line endings.

Change: enter image description here

To: enter image description here

  • Related