2

I am building an image using the following Dockerfile

FROM golang:1.19.2-bullseye as builder

COPY src /src

WORKDIR /src

RUN CGO_ENABLED=1 go build -race -ldflags "-s -w" -o client-go

FROM scratch

COPY --from=builder /src/client-go /client-go

ENTRYPOINT [ "/client-go" ]

and creating the following Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test-skaffold
  name: test-skaffold
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test-skaffold
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: test-skaffold
    spec:
      containers:
      - image: pkaramol/test-skaffold
        name: test-skaffold

The deployment is done using skaffold FWIW.

The process fails as follows:

Waiting for deployments to stabilize...
 - deployment/test-skaffold: container test-skaffold terminated with exit code 1
    - pod/test-skaffold-c955979f6-ckpqg: container test-skaffold terminated with exit code 1
      > [test-skaffold-c955979f6-ckpqg test-skaffold] standard_init_linux.go:228: exec user process caused: no such file or directory
 - deployment/test-skaffold failed. Error: container test-skaffold terminated with exit code 1.

What is the failure cause?

Update

Changing the FROM in the 2nd build stage to ubuntu fixes things, but why is scratch failing to find the ENTRYPOINT ?

pkaramol
  • 16,451
  • 43
  • 149
  • 324
  • 3
    `scratch` isn't failing to find the entrypoint - it's because the executable has dynamic links to glibc amongst other things, and these `so` files aren't present on the filesystem. ```ldd /client-go linux-vdso.so.1 (0x00007ffdc51c4000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f54b13df000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f54b120a000) /lib64/ld-linux-x86-64.so.2 (0x00007f54b1407000) ``` – nj_ Oct 29 '22 at 13:49
  • 1
    Note that if you compile with cgo disabled, this won't be an issue: ```ldd /client-go not a dynamic executable ``` – nj_ Oct 29 '22 at 13:51
  • Ι am trying to build a statically linked binary but the end result is the same `go build -ldflags="-extldflags=-static" -o client-go` – pkaramol Oct 29 '22 at 14:25
  • 2
    It seems it needs **both** `CGO_ENABLED=0` and the above directive to build static binary (no idea why they are **both** needed) – pkaramol Oct 29 '22 at 14:34
  • 1
    @pkaramol Thank you for the solution on how to make the slimmiest `go` docker! Just 1.21 MB for *Hello, World!* go executable compared to 996 MB of the docker built from `go:latest`. – Pak Uula Oct 29 '22 at 14:47
  • keep in mind you'll also need CA root certificates available if you want to make any TLS connections from Go - at least if you want to validate the certificates of those connections which you absolutely should want to do. You can generally copy those over as well – erik258 Oct 29 '22 at 16:42
  • Is your issue resolved?if yes, can you post the procedure you've followed as Solution and accept itfor the greater visibility of community – Sai Chandini Routhu Dec 20 '22 at 06:20

0 Answers0