Why my golang docker container exits immediately (code 127)?
To trim the binary size, I used
LDFLAGS='-w -s', pack with
upx, then build from scratch. The thing is when starting, it exited immediately with code 127. Why?
1FROM scratch 2 3WORKDIR /app 4 5COPY build/linux/<binary> . 6 7ENTRYPOINT [ "/app/<binary>" ]
10fbce782a9bd quantonganh/<binary>:T276-dockerize "/app/<binary>" 6 seconds ago Exited (127) 4 seconds ago relaxed_thompson
and there is no logs at all:
1❯ docker logs 0fbce782a9bd
IIRC, 127 means that “The supplied command cannot be found”, but I’m sure my binary exists, so what’s the problem?
To get into this container, we need
/bin/bash, so I switched from
alpine as a temporary:
1FROM alpine:3.10 2 3RUN apk add --no-cache bash 4...
then I tried to run that binary:
1❯ docker run -it --entrypoint /bin/bash <image> /app/<binary> 2/app/<binary>: /app/<binary>: cannot execute binary file
Why “cannot execute”?
Let’s see what the file type is:
1❯ docker run -it --entrypoint /bin/bash <image> -s 2bash-5.0# apk add file 3bash-5.0# file /app/<binary> 4/app/binary: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=9goElUU1Hmb_TUJeOeZr/vWwqHpXL1j9jP4lLwfaC/JmUJczr0kCVHg1u9gMuT/orDzu6T6STfr9G_YODTe, stripped
What? Why “dynamically linked”?
Turned out that I forgot to specify
CGO_ENABLED=0 when building, so it was dynamically linked:
1bash-5.0# ldd /app/<binary> 2 /lib64/ld-linux-x86-64.so.2 (0x7f1f8b561000) 3 libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7f1f8b561000) 4 libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f1f8b561000)
These does not exist, so the container exited with code 127 immediately.
- Docker Compose healthcheck
- condition form of depends_on in docker-compose version 3
- Let's Encrypt too many certificates already issued
- plugins/docker failed to resolve Keycloak hostname?
- How to run a pipeline step only when pushing to a new branch?