Home >Backend Development >Golang >How to Resolve 'no such file or directory' Errors with Docker Scratch Images and Go?
"no such file or directory" with Docker Scratch Image: Resolved
When using Docker's scratch image, you may encounter the error "no such file or directory" when executing a binary. This error often stems from the binary being dynamically linked to a library that does not exist in the image.
To rectify this issue, disable CGO (C Go):
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ -ldflags="-w -s" -o $PROJ_BIN_PATH ./cmd/...
CGO links to system libraries, which can cause problems in a scratch image. By disabling CGO, you ensure that the binary is statically linked and does not depend on external libraries.
Additionally, you can verify the dynamic links in your binary using ldd:
docker build --target=0 -t your_go_image . docker run -it --rm your_go_image ldd /$PROJ_NAME
This command will list any libraries that the binary is linked to. If ldd returns "not a dynamic executable," the binary is statically linked.
The above is the detailed content of How to Resolve 'no such file or directory' Errors with Docker Scratch Images and Go?. For more information, please follow other related articles on the PHP Chinese website!