6

Served many pages and forums and didn't find a solution. I have the simple docker container with executable .sh file. When I build and run it from Windows - all is fine. Now trying to build it from Ubuntu 18.04 with Docker version 19.03.5.

For rebuild use .sh script that I execute as bash install.sh

go build -o main.sh ./main

docker stop stats
docker container rm stats
docker image rm stats
docker build -t stats .
docker run --name stats -p 8080:8080 stats

My Dockerfile is:

FROM alpine:3.10.1

ARG appPath="app"

RUN mkdir /app/
COPY main.sh /app/main.sh
RUN chmod +x /app/main.sh
COPY resources  /app/resources

RUN apk add --no-cache bash
WORKDIR /app

EXPOSE 8080
CMD /bin/bash -c 'ls' && /app/main.sh dev

The output 'install' call is the following:

bash install.sh
stats
stats
Untagged: stats:latest
Sending build context to Docker daemon  38.98MB
Step 1/10 : FROM alpine:3.10.1
 ---> b7b28af77ffe
Step 2/10 : ARG appPath="app"
 ---> Using cache
 ---> 08baa7336701
Step 3/10 : RUN mkdir /app/
 ---> Using cache
 ---> fb9870e78322
Step 4/10 : COPY main.sh /app/main.sh
 ---> Using cache
 ---> 79bf713855e3
Step 5/10 : RUN chmod +x /app/main.sh
 ---> Using cache
 ---> 88bf70f9c6ec
Step 6/10 : COPY resources  /app/resources
 ---> Using cache
 ---> 2ebf95627a9e
Step 7/10 : RUN apk add --no-cache bash
 ---> Using cache
 ---> 39cd823e7f2f
Step 8/10 : WORKDIR /app
 ---> Using cache
 ---> 37e6fcea2d65
Step 9/10 : EXPOSE 8080
 ---> Using cache
 ---> 4250094c65f6
Step 10/10 : CMD /bin/bash -c 'ls' && /app/main.sh dev
 ---> Using cache
 ---> ed41a1efb15b
Successfully built ed41a1efb15b
Successfully tagged stats:latest
main.sh
resources
/bin/sh: /app/main.sh: not found

I don't understand what is wrong. main.sh is there. If I try to execute CMD /bin/bash -c 'ls' && bash main.sh dev then receive main.sh: main.sh: cannot execute binary file.

What is wrong there and how can I fix it?

UPD:

Renamed file that this was binary and shouldn't have .sh in the end.

And tryed again wi the following:

install.sh

#!/bin/bash
go build -o myapp ./main

docker stop stats
docker container rm stats
docker image rm stats
docker build -t stats .
docker run --name stats -p 8080:8080 stats

Dockerfile

FROM alpine:3.10.1

RUN mkdir /app/
COPY myapp /app/myapp
RUN chmod +x /app/myapp
COPY resources  /app/resources

RUN apk add --no-cache bash
WORKDIR /app

EXPOSE 8080
CMD /bin/bash -c 'ls' && ./myapp dev

Output was the following:

....    
Step 9/9 : CMD /bin/bash -c 'ls' && ./myapp dev
 ---> Running in 52fc24d26747
Removing intermediate container 52fc24d26747
 ---> 8d4f415e6dcd
Successfully built 8d4f415e6dcd
Successfully tagged stats:latest
myapp
resources
/bin/sh: ./myapp: not found

Tryed /app/myapp, ./myapp and it doesn't work.

flybox
  • 224
  • 3
  • 12

1 Answers1

9

I'm guessing the error message is misleading, and that the real problem is that some library or other Go runtime component is missing from inside your Docker image. Try building your app statically (which means all the things it needs will be bundled into the binary).

CGO_ENABLED=0 go build -o myapp ./main

(And yeah, don't call it main.sh if it's not a shell script. Unix doesn't care, but you are confusing the hell out of your human readers. And actually, don't call it that even if it is. You don't need to know whether grep is a binary, a shell script, or a Python program; the same convention makes sense for your own tools.)

tripleee
  • 175,061
  • 34
  • 275
  • 318