I have a dockerfile like the following:
FROM golang:1.19 AS builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 go build -v -o "hello-bin"
#FROM scratch
FROM alpine
COPY --from=builder /app/hello-bin /app/hello-bin
COPY --from=builder /app/start.sh /app/start.sh
WORKDIR /app
ENTRYPOINT [ "./start.sh" ]
which simply build a binary from a hello-world go file. However, when I try to run this container with the following docker-compose setting, it showed exec ./start.sh: no such file or directory
.
version: "3"
services:
hello:
restart: always
build:
context: .
dockerfile: Dockerfile
The structure of the directory is
❯ tree .
.
├── Dockerfile
├── docker-compose.yaml
├── go.mod
├── hello
├── init.go
└── start.sh
So the start.sh
should be loaded to the builder
container via the COPY . .
line as well, and it should be passed to the second container via the COPY --from=builder /app/start.sh /app/start.sh
.
For context, the content of start.sh
is as follows:
#!/bin/bash
_main() {
echo "start"
}
_main "$@"
My most confusing part is that, if I change it to CMD [ "ls", "-l" ]
in the Dockerfile, it actually prints out
awesomeproject3-hello-1 | -rwxr-xr-x 1 root root 1819562 May 19 02:41 hello-bin
awesomeproject3-hello-1 | -rwxrwxrwx 1 root root 51 May 19 02:39 start.sh
and also if I change it to ENTRYPOINT [ './hello-bin' ]
in the Dockerfile, the binary runs successfully as well. I just can't figure out why it says there's no ./start.sh
.
UPDATE: inspired by @larsks, I noticed if I change the header of ./start.sh
from #!/bin/bash
to #!/bin/sh
, it magically worked. I am still confused what the underlying issue here, and if I want to keep the bash header, how should I fix the docker file?