2

I am building and running a Linux-based Docker image in Windows 10, but struggling to use the container's entrypoint to use a bash script.

Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS builder
RUN curl -fsSL https://get.pulumi.com | sh
WORKDIR /app
COPY ./MyProject ./
RUN ls
RUN chmod +x pulumi-go.sh
ENTRYPOINT ["./pulumi-go.sh"]

pulumi-go.sh:

#!/bin/bash
echo Hello world

And I get the following error when running docker run --rm -it myproject:latest:

standard_init_linux.go:228: exec user process caused: exec format error

Things I tried:

  • Removed windows line endings from the bash script
  • Using #!/bin/sh and #!/bin/ash

Side finding... see the docker build output when I add a RUN ./pulumi-go.sh step:

#13 [9/9] RUN ./pulumi-go.sh
#13 sha256:741c439f6f65a3a2d0c70377583f4ea20120e5a4d0950396e24ce388cb04159e
#13 0.524 ./pulumi-go.sh: 1: ./pulumi-go.sh: #!/bin/bash: not found
#13 0.524 Hello world
#13 DONE 0.6s

What is not found if the script is clearly executing?

RUN bash --version tells me bash is just fine:

#11 [7/9] RUN bash --version
#11 0.517 GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)

RUN which bash:

#11 [7/9] RUN which bash
#11 0.540 /bin/bash

RUN cat /etc/os-release:

#11 [ 7/10] RUN cat /etc/os-release
#11 0.550 PRETTY_NAME="Debian GNU/Linux 10 (buster)"
#11 0.550 NAME="Debian GNU/Linux"
#11 0.550 VERSION_ID="10"
#11 0.550 VERSION="10 (buster)"
#11 0.550 VERSION_CODENAME=buster
#11 0.550 ID=debian

Viewing all characters in Notepad++:

enter image description here

If I change entrypoint to this:

ENTRYPOINT ["/bin/bash", "-c", "./pulumi-go.sh"]

I get the following response

./pulumi-go.sh: line 1: #!/bin/bash: No such file or directory
Hello world

Hex dump of first line in pulumi-go.sh:

#13 [ 9/10] RUN od -tx1 ./pulumi-go.sh | head -n 1
#13 0.572 0000000 ef bb bf 23 21 2f 62 69 6e 2f 62 61 73 68 0a 0a
Dave New
  • 38,496
  • 59
  • 215
  • 394
  • Please run these and provide output: `docker inspect IMAGE_NAME --format="{{.Config.Cmd}}"`, `docker inspect IMAGE_NAME --format="{{.ContainerConfig.Cmd}}"`. Did you change which images (Linux or Windows) Docker Desktop should run? (see [this](https://docs.docker.com/desktop/windows/#switch-between-windows-and-linux-containers)) – anemyte Sep 08 '21 at 10:02
  • Probably, because you copy project to ./ dir, but run script from workdir ./app – Makariy Sep 08 '21 at 10:11
  • 2
    Just to be sure, in your shell script did you write `$ echo Hello world` or `echo Hello world` ? – Aserre Sep 08 '21 at 10:21
  • 1
    @Makariy what do you mean ? AFAIK the WORKDIR command means every following command will be launched in the /app folder, and the COPY command then copies the local ./MyProject to the current docker directory, /app – Aserre Sep 08 '21 at 10:27
  • @Aserre Yes, sorry, it's `echo Hello world` – Dave New Sep 08 '21 at 11:23
  • @anemyte Both of those inspects return with `[]` – Dave New Sep 08 '21 at 11:24
  • Is `#!/bin/bash` the very first line, no whitespace or anything before it? – BMitch Sep 08 '21 at 11:25
  • @BMitch correct, it's just that - no whitespaces or line endings – Dave New Sep 08 '21 at 11:26
  • 1
    Did you build this with a base image matching the architecture where you run it? (E.g. arm vs amd/x86.) – BMitch Sep 08 '21 at 11:26
  • @BMitch I am unsure about that. Let me Google – Dave New Sep 08 '21 at 11:28
  • See post edit... if I RUN that script, I get `#!/bin/bash: not found` yet it executes – Dave New Sep 08 '21 at 11:50
  • 1
    Sorry for the noise, I was mistaken about the duplicate. – tripleee Sep 08 '21 at 12:13
  • 1
    Ah, I managed to reproduce your output `standard_init_linux.go:228: exec user process caused: exec format error` : are you 100% positive there is no invisible character before `#!/bin/bash` ? (tab, newline, \r, invisible space, ...) I got this result when I wrote `#!/bin/bash` at the 2nd line of the script – Aserre Sep 08 '21 at 12:26
  • @Aserre Output is `/bin/bash` – Dave New Sep 08 '21 at 12:26
  • I don't think `which bash` is useful, it's already shown that `/bin/bash` is included in the base image and works on its own. – tripleee Sep 08 '21 at 12:26
  • 1
    Could you include a hex dump of the first line of your script? `od -tx1 ./pulumi-go.sh | head -n 1` – tripleee Sep 08 '21 at 12:27
  • 2
    @tripleee I have fixed the issue... that first line had a preceding "ef bb bf" UTF8 byte order mark. I've removed this and it now works. Thank you very much for the help – Dave New Sep 08 '21 at 12:45
  • 1
    I can't close this as duplicate again, but basically then a duplicate of [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – tripleee Sep 08 '21 at 12:47
  • That post doesn't explicitly talk about the byte order mark encoding, so perhaps it's not worth marking as a duplicate. Im not even sure if dos2unix would fix this. – Dave New Sep 08 '21 at 13:00
  • There are probably secondary duplicates of that one which explicitly discuss this corner case, but too lazy to go hunt them down. – tripleee Sep 08 '21 at 13:01
  • 2
    https://stackoverflow.com/a/38038099/4957508 seems relevant here. – Jeff Schaller Sep 08 '21 at 14:10
  • 1
    It appears from https://stackoverflow.com/questions/45240387/how-can-i-remove-the-bom-from-a-utf-8-file that at least some versions of `dos2unix` can in fact solve this. – tripleee Sep 09 '21 at 05:26

0 Answers0