在Alpine Linux Docker上找不到已安装的Go二进制文件。

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

Installed Go binary not found in path on Alpine Linux Docker

问题

我有一个Go二进制文件,我想在Alpine Docker镜像上运行它。

对于Docker Go二进制文件,这个方法可以正常工作。

docker run -it alpine:3.3 sh
apk add --no-cache curl

DOCKER_BUCKET=get.docker.com
DOCKER_VERSION=1.9.1
curl -fSL "https://${DOCKER_BUCKET}/builds/Linux/x86_64/docker-$DOCKER_VERSION" -o /usr/local/bin/docker
chmod +x /usr/local/bin/docker
docker help
Usage: docker [OPTIONS] COMMAND [arg...]
...

然而,对于我想安装的Go二进制文件,情况不同。

RACK_BUCKET=ec4a542dbf90c03b9f75-b342aba65414ad802720b41e8159cf45.ssl.cf5.rackcdn.com
RACK_VERSION=1.1.0-beta1
curl -fSL "https://${RACK_BUCKET}/${RACK_VERSION}/Linux/amd64/rack" -o /usr/local/bin/rack
chmod +x /usr/local/bin/rack

rack help
sh: rack: not found

/usr/local/bin/rack help
sh: /usr/local/bin/rack: not found

ls -al /usr/local/bin/
total 43375
drwxr-xr-x    2 root     root          1024 Jan 11 18:10 .
drwxr-xr-x    8 root     root          1024 Jan 11 18:09 ..
-rwxr-xr-x    1 root     root      30222575 Jan 11 18:09 docker
-rwxr-xr-x    1 root     root      14190576 Jan 11 18:10 rack

which rack
/usr/local/bin/rack

我以为可能与这个答案有关,但是当我运行ldd时,没有得到相同的错误。

ldd /usr/local/bin/rack
  	/lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)
  	libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)
  	libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)

对于为什么在Alpine Linux Docker上安装的Go二进制文件在路径中找不到,有什么想法吗?

英文:

I've got a Go binary I'm trying to run on the Alpine Docker image.

This works fine for the Docker Go binary.

docker run -it alpine:3.3 sh
apk add --no-cache curl

DOCKER_BUCKET=get.docker.com
DOCKER_VERSION=1.9.1
curl -fSL "https://${DOCKER_BUCKET}/builds/Linux/x86_64/docker-$DOCKER_VERSION" -o /usr/local/bin/docker
chmod +x /usr/local/bin/docker
docker help
Usage: docker [OPTIONS] COMMAND [arg...]
...

However, for the Go binary I want to install.

RACK_BUCKET=ec4a542dbf90c03b9f75-b342aba65414ad802720b41e8159cf45.ssl.cf5.rackcdn.com
RACK_VERSION=1.1.0-beta1
curl -fSL "https://${RACK_BUCKET}/${RACK_VERSION}/Linux/amd64/rack" -o /usr/local/bin/rack
chmod +x /usr/local/bin/rack

rack help
sh: rack: not found

/usr/local/bin/rack help
sh: /usr/local/bin/rack: not found

ls -al /usr/local/bin/
total 43375
drwxr-xr-x    2 root     root          1024 Jan 11 18:10 .
drwxr-xr-x    8 root     root          1024 Jan 11 18:09 ..
-rwxr-xr-x    1 root     root      30222575 Jan 11 18:09 docker
-rwxr-xr-x    1 root     root      14190576 Jan 11 18:10 rack

which rack
/usr/local/bin/rack

I thought it might have something to do with this answer but I don't get the same error when running ldd.

ldd /usr/local/bin/rack
  	/lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)
  	libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)
  	libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7fdd15cd0000)

Any idea with this installed Go binary is not found in path on Alpine Linux Docker?

答案1

得分: 133

RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

由于musl和glibc是兼容的,你可以创建这个符号链接,它将修复缺失的依赖关系。

英文:
RUN mkdir /lib64 && ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2

Since the musl and glibc so are compatible, you can make this symlink and it will fix the missing dependency.

答案2

得分: 41

我在alpine中使用以下选项编译了Go二进制文件:

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o [二进制文件的名称]

它成功运行了。

英文:

I compiled go binary in alpine with these options

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o [name of binary]

It worked.

答案3

得分: 25

在Debian 9 (Stretch) / Go 1.10.2下构建,并在Alpine 3.7.0下运行时:

CGO_ENABLED=0 go build

不需要设置GOOS=linuxGOARCH=amd64

英文:

When building under Debian 9 (Stretch) / Go 1.10.2 and running under Alpine 3.7.0:

CGO_ENABLED=0 go build

Neither GOOS=linux nor GOARCH=amd6 was necessary.

答案4

得分: 19

你可以安装libc6-compat

运行 apk add --no-cache libc6-compat
英文:

You can install libc6-compat

RUN apk add --no-cache libc6-compat

答案5

得分: 1

根据程序的性质,您可能希望使用静态链接选项编译您的Go程序,例如以下选项:

-x -a -tags netgo -installsuffix netgo

之后,您就不需要担心链接正确的库了。

英文:

Depending on the nature of the program, you might want to compile your go program with static link options, such as the following:

-x -a -tags netgo -installsuffix netgo

Afterwards you do not need to worry about linking the correct libraries.

答案6

得分: 0

或者,您可以使用Docker Hub上的golang:alpine镜像来编译和运行您的代码。

docker run -v ${YOUR_CODE_PATH}:/go/src/example -it golang:alpine sh
cd src/example
go build .
ldd example
	/lib/ld-musl-x86_64.so.1 (0x7f677fcf7000)
	libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7f677fcf7000)
英文:

Alternatively, you can (meanwhile) use the golang:alpine image from Docker Hub to compile and run your code.

docker run -v ${YOUR_CODE_PATH}:/go/src/example -it golang:alpine sh
cd src/example
go build .
ldd example
	/lib/ld-musl-x86_64.so.1 (0x7f677fcf7000)
	libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7f677fcf7000)

答案7

得分: 0

所以即使使用了libc6-compat,我仍然遇到找不到libresolv.so.2的问题。

运行apk add --no-cache gcompat

然后一切正常了。

参考:https://github.com/golang/go/issues/59305 和它的简单答案/解决方案 https://github.com/golang/go/issues/59305#issuecomment-1541493132

英文:

So with libc6-compat i still got issues with not finding libresolv.so.2

RUN apk add --no-cache gcompat

and everything worked

See: https://github.com/golang/go/issues/59305 and its simple answer/resoludion https://github.com/golang/go/issues/59305#issuecomment-1541493132

huangapple
  • 本文由 发表于 2016年1月12日 03:44:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/34729748.html
匿名

发表评论

匿名网友

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

确定