standard_init_linux.go:228: 执行用户进程时出错:CGO项目中没有该文件或目录

huangapple go评论116阅读模式
英文:

standard_init_linux.go:228: exec user process caused: no such file or directory on CGO project

问题

我有一个引用了C库的Go服务,在尝试运行我的Docker镜像时遇到以下错误:

standard_init_linux.go:228: exec user process caused: no such file or directory

这不是https://stackoverflow.com/questions/51508150/standard-init-linux-go190-exec-user-process-caused-no-such-file-or-directory的重复问题,因为我没有带有CR行结束符的入口shell脚本。这个网站上的一些其他人建议在编译时设置CGO_ENABLED=0,但是我当然不能这样做,因为这是一个CGO项目。

C库安装在目录/usr/local/lib/sgp4下的Linux/IFORTLinux/GFORTRAN中,其中包含一堆.so文件。

环境变量LD_LIBRARY_PATH设置为/usr/local/lib/sgp4/Linux/IFORT

在我的项目中,有一组包装器.h文件,CGO注释如下:

standard_init_linux.go:228: 执行用户进程时出错:CGO项目中没有该文件或目录

我的Dockerfile如下:

FROM [redacted]/alpine-base:3.17

RUN apk update && \
    apk add curl jq

COPY etc/cfg/propagator.properties /usr/local/[redacted]/etc/cfg/
COPY bin/sgp4 /usr/local/[redacted]/bin/sgp4
COPY propagator/wrappers /usr/local/[redacted]/bin/wrappers

# Download library files
RUN mkdir -p /usr/local/sgp4/lib
RUN curl --output /usr/local/sgp4/lib/SGP4.tar [redacted]/SGP4.tar
RUN tar -xf /usr/local/sgp4/lib/SGP4.tar -C /usr/local/sgp4/lib
RUN rm /usr/local/sgp4/lib/SGP4.tar
RUN export LD_LIBRARY_PATH=/usr/local/sgp4/lib/Linux/IFORT

RUN chmod a+rx /usr/local/[redacted]/bin/sgp4

RUN addgroup [redacted] && adduser -D -H -G [redacted] [redacted]
USER [redacted]:[redacted]

HEALTHCHECK NONE

ENTRYPOINT ["/usr/local/[redacted]/bin/sgp4", "-config-file-path=/usr/local/[redacted]/etc/cfg/propagator.properties"]

我想知道我的库文件是否包含CR行结束符,所以我尝试将以下命令添加到我的Dockerfile中,但没有成功:

RUN for filey in $(find /usr/local/sgp4/lib/Linux -type f); do echo $filey; sed -i 's/\r$//g' $filey; done
RUN for filey in $(find /usr/local/sgp4/lib/Linux -type f); do echo $filey; tr -d \r < $filey > tmp.so; mv tmp.so $filey; done

注意:我在Debian上编译了二进制文件,但在Docker镜像中受到使用Alpine的限制。编译时遇到的问题在这里讨论:https://stackoverflow.com/questions/76717432/cgo-compiler-error-missing-library-links-librt-so-1-not-found

英文:

I have a Go service that references a C library, and I am getting the following error when trying to run my docker image:

standard_init_linux.go:228: exec user process caused: no such file or directory

This is not a duplicate of https://stackoverflow.com/questions/51508150/standard-init-linux-go190-exec-user-process-caused-no-such-file-or-directory as I do not have an entrypoint shell script with CR line endings. Some others on this site suggested setting CGO_ENABLED=0 at compile time, but I of course can't do that as this is a CGO project.

The C library is installed under /usr/local/lib/sgp4 in the directories Linux/IFORT and Linux/GFORTRAN, which contain a bunch of .so files.

An environment variable LD_LIBRARY_PATH is set to /usr/local/lib/sgp4/Linux/IFORT.

Within my project, there is a set of wrapper .h files, and the CGO comments as follows:

standard_init_linux.go:228: 执行用户进程时出错:CGO项目中没有该文件或目录

My Dockerfile is as follows:

FROM [redacted]/alpine-base:3.17

RUN apk update && \
    apk add curl jq

COPY etc/cfg/propagator.properties /usr/local/[redacted]/etc/cfg/
COPY bin/sgp4 /usr/local/[redacted]/bin/sgp4
COPY propagator/wrappers /usr/local/[redacted]/bin/wrappers

# Download library files
RUN mkdir -p /usr/local/sgp4/lib
RUN curl --output /usr/local/sgp4/lib/SGP4.tar [redacted]/SGP4.tar
RUN tar -xf /usr/local/sgp4/lib/SGP4.tar -C /usr/local/sgp4/lib
RUN rm /usr/local/sgp4/lib/SGP4.tar
RUN export LD_LIBRARY_PATH=/usr/local/sgp4/lib/Linux/IFORT

RUN chmod a+rx /usr/local/[redacted]/bin/sgp4

RUN addgroup [redacted] && adduser -D -H -G [redacted] [redacted]
USER [redacted]:[redacted]

HEALTHCHECK NONE

ENTRYPOINT ["/usr/local/[redacted]/bin/sgp4", "-config-file-path=/usr/local/[redacted]/etc/cfg/propagator.properties"]

Wondering if my library files contained CR line endings, I tried adding these commands to my Dockerfile, but no luck:

RUN for filey in $(find /usr/local/sgp4/lib/Linux -type f); do echo $filey; sed -i 's/\r$//g' $filey; done
RUN for filey in $(find /usr/local/sgp4/lib/Linux -type f); do echo $filey; tr -d \r < $filey > tmp.so; mv tmp.so $filey; done

Note: I compiled the binary on debian, but am constrained to using Alpine for the Docker image. Issues I encountered when compiling are discussed here: https://stackoverflow.com/questions/76717432/cgo-compiler-error-missing-library-links-librt-so-1-not-found

答案1

得分: 4

解决运行时问题的一种方法是切换到基于glibc的Docker镜像,例如Debian slim或jeanblanchard/docker-alpine-glibc,因为您的Go二进制文件及其CGO依赖是针对glibc构建的。

使用jeanblanchard/alpine-glibc,因为它仍然基于Alpine:

FROM jeanblanchard/alpine-glibc:3.18

RUN apk update && \
    apk add curl jq

COPY etc/cfg/propagator.properties /usr/local/[redacted]/etc/cfg/
COPY bin/sgp4 /usr/local/[redacted]/bin/sgp4
COPY propagator/wrappers /usr/local/[redacted]/bin/wrappers

# Download library files
RUN mkdir -p /usr/local/sgp4/lib
RUN curl --output /usr/local/sgp4/lib/SGP4.tar [redacted]/SGP4.tar
RUN tar -xf /usr/local/sgp4/lib/SGP4.tar -C /usr/local/sgp4/lib
RUN rm /usr/local/sgp4/lib/SGP4.tar
ENV LD_LIBRARY_PATH=/usr/local/sgp4/lib/Linux/IFORT

RUN chmod a+rx /usr/local/[redacted]/bin/sgp4

RUN addgroup [redacted] && adduser -D -H -G [redacted] [redacted]
USER [redacted]:[redacted]

HEALTHCHECK NONE

ENTRYPOINT ["/usr/local/[redacted]/bin/sgp4", "-config-file-path=/usr/local/[redacted]/etc/cfg/propagator.properties"]

这个基础镜像保持了Alpine的小体积特性,同时也具备了glibc的兼容性,这应该使其与您的CGO二进制文件兼容。

切换到这个Dockerfile并重新构建镜像应该可以解决"exec user process caused: no such file or directory"错误,以及ldd的"required file not found"错误。

英文:

A way to solve the runtime issue would be to switch to a glibc-based Docker image such as Debian slim or jeanblanchard/docker-alpine-glibc, because your Go binary and its CGO dependencies were built against glibc.

Using jeanblanchard/alpine-glibc, since it is still based on Alpine:

FROM jeanblanchard/alpine-glibc:3.18

RUN apk update && \
    apk add curl jq

COPY etc/cfg/propagator.properties /usr/local/[redacted]/etc/cfg/
COPY bin/sgp4 /usr/local/[redacted]/bin/sgp4
COPY propagator/wrappers /usr/local/[redacted]/bin/wrappers

# Download library files
RUN mkdir -p /usr/local/sgp4/lib
RUN curl --output /usr/local/sgp4/lib/SGP4.tar [redacted]/SGP4.tar
RUN tar -xf /usr/local/sgp4/lib/SGP4.tar -C /usr/local/sgp4/lib
RUN rm /usr/local/sgp4/lib/SGP4.tar
ENV LD_LIBRARY_PATH=/usr/local/sgp4/lib/Linux/IFORT

RUN chmod a+rx /usr/local/[redacted]/bin/sgp4

RUN addgroup [redacted] && adduser -D -H -G [redacted] [redacted]
USER [redacted]:[redacted]

HEALTHCHECK NONE

ENTRYPOINT ["/usr/local/[redacted]/bin/sgp4", "-config-file-path=/usr/local/[redacted]/etc/cfg/propagator.properties"]

This base image maintains the small footprint of Alpine but also has the compatibility of glibc, which should make it compatible with your CGO binary.

Switching to this Dockerfile and rebuilding your image should solve the "exec user process caused: no such file or directory" error, as well as the "required file not found" error from ldd.

huangapple
  • 本文由 发表于 2023年7月27日 07:59:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76775746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定