user10680765
user10680765

Reputation: 11

Docker image openjdk:8-jdk-alpine fails to execute a simple command

I created a docker image from openjdk:8-jdk-alpine using the below Dockerfile:

Dockerfile

But when I try to execute simple commands I get the following errors:

/bin/sh: ./run.sh: not found

my run.sh looks like this enter image description here I try to "docker run -it [images] bash" enter to the interactive environment,I can see the file "run.sh".In the directory /bin bash exist,but I execute run.sh also display " /bin/sh: ./run.sh: not found"

PS:Sorry for my poor english,I am a chinese student

Upvotes: 1

Views: 5245

Answers (1)

Anya Shenanigans
Anya Shenanigans

Reputation: 94624

The printed content of the run.sh, indicates that my original assessment was incorrect; however based on the error message, and the image of the run.sh file, I have a lead.

Your run.sh script has an exec line of #!/bin/sh, which means that it does not need bash to operate so my previous assessment was incorrect.

Starting on a mac, I created a run.sh script, duplicated the dockerfile (mostly), and it ran correctly, producing a valid run.

I then converted the run.sh to use dos line endings and got the following:

$ file run.sh
run.sh: POSIX shell script text executable, ASCII text, with CRLF line terminators
$ docker run --rm -it bob
/bin/sh: ./run.sh: not found

Which looks suspiciously like your error message.

From this, it would lead me to believe that your run.sh file contains dos line endings. Based on the images, I'm guessing that you're on windows, which is where the problem with the run.sh script originates.

how to convert the line endings (some examples):

  • dos2unix run.sh
  • perl -pi -e 's/\r\n/\n/g' run.sh

Previous Answer

The most likely reason for this issue is that the shebang line in the run.sh contains: #!/usr/bin/bash, or something of that ilk - i.e. it doesn't reference the valid path to the binary that will run the shell script.

On alpine, bash is installed into /bin, so if you try to run the script you will see the error:

/ # ./run.sh
/bin/sh: ./run.sh: not found
/ # cat run.sh
#!/usr/bin/bash

echo "Hi"

workaround (1): after the apk add bash, do an:

RUN ln -s /bin/bash /usr/bin

in your Dockerfile. This will create a symlink for bash, allowing the program to run:

/ # ln -s /bin/bash /usr/bin
/ # ./run.sh
Hi

workaround(2) - if you don't want to make a symlink like this, you can always invoke bash as part of the CMD -

CMD [ 'bash', './run.sh' ]

Upvotes: 1

Related Questions