目录
1. alpine 笔记
1.1. 应用: adding www-data user to alpine images
works:
FROM nginx:alpine
# modified version from php:alpine image
# ensure www-data user exists
RUN set -x ; \
addgroup -g 82 -S www-data ; \
adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1
# 82 is the standard uid/gid for "www-data" in Alpine
# http://git.alpinelinux.org/cgit/aports/tree/main/apache2/apache2.pre-install?h=v3.3.2
# http://git.alpinelinux.org/cgit/aports/tree/main/lighttpd/lighttpd.pre-install?h=v3.3.2
# http://git.alpinelinux.org/cgit/aports/tree/main/nginx-initscripts/nginx-initscripts.pre-install?h=v3.3.2
1.2. 版本问题
1.2.1. apk add --no-cache
As of Alpine Linux 3.3
there exists a new --no-cache
option for apk. It allows users to install packages with an index that is updated and used on-the-fly and not cached locally.
1.3. 运行问题
1.3.1. go 程序不能运行,报 not found 之类的错误
是由于编译 go 程序的时候不完全是静态链接的,有对 glibc 动态库的依赖 (可通过 ldd
命令查看到),而 alpine linux 是基于 musl libc 的,所以无法运行。
从编译 go 程序和运行环境两方面都可以解决。
- 编译 go 程序
出发点是不依赖 CGO:
CGO_ENABLED=0 go build -ldflags "-s -w" -tags netgo -a -v
By default, if using the net
package a build will likely produce a binary with some dynamic linking, e.g. to libc. You can inspect dynamically vs. statically link by viewing the result of ldd output.bin
There are two solutions I’ve come across:
- Disable CGO, via
CGO_ENABLED=0
- Force the use of the Go implementation of net dependencies, netgo via
go build -tags netgo -a -v
, this is implemented for a certain platforms
The above assumes that the only CGO dependency is the standard library’s net
package.
还有网友提到的方法:
go build -ldflags '-linkmode external -w -extldflags "-static"'
The -linkmode
option tells Go to use the external linker, the -extldflags
option sets options to pass to the linker and the -w
flag disables DWARF debug info to improve binary size.
See go tool link
and Statically compiled Go programs, always, even with cgo, using musl for more details
- 运行环境
在 alpine 里面安装一些兼容包可以解决。
# alpine 3.8 以后才有 gcompat 包
RUN set -eux; \
apk add --no-cache \
gcompat \
;
# 下面这条命令很多帖子都提到过,但亲自试过,没啥用
RUN apk add --no-cache libc6-compat
1.3.2. Error loading shared library libstdc++.so.6
RUN apk upgrade --no-cache && \
apk add --no-cache libstdc++6
FWIW, an other interesting way to fix this problem (and stay with alpine) is to install the libstdc++ package, like this:
FROM java:8-alpine
...
RUN apk add --no-cache libstdc++6
...
At runtime, libjunixsocket-native will find what it wants:
$ ldd /tmp/libjunixsocket-native-2.0.45373916279860178862.so
ldd (0x56432e94b000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x7fcaba3d3000)
libm.so.6 => ldd (0x56432e94b000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x7fcaba1c0000)
libc.so.6 => ldd (0x56432e94b000)
But I do not know what other problems this fix could cause, because from what I can see, java is still linked to musl:
$ ldd /usr/bin/java
...
/lib/ld-musl-x86_64.so.1 (0x564ed8a3f000)
...
Anyways, I will stop spamming this thread, everything is working for me now.