Skip to content

编译好的 go 软件包在 Alpine 容器中运行报错问题

go 程序无法运行 not found

sh: ./xxx: not found

这是因为 alpine 容器非常简化没有这个程序需要的动态链接库,安装 libc6-compat 即可

sh
apk add --update --no-cache libc6-compat

go 程序在请求 https 资源时报如下错误

这是缺少 ca 根证书导致的,安装 ca-certificates 即可,在其他容器里也会有这个错误,安装 apt-get install ca-certificates

tls: failed to verify certificate: x509: certificate signed by unknown autho

sh
apk add --update --no-cache ca-certificates

和时区相关的错误,安装 tzdata

time: missing Location in call to Time.In

sh
apk add --update --no-cache tzdata

Alpine Dockerfile 打包

Dockerfile
FROM alpine:latest
RUN apk add --update --no-cache libc6-compat ca-certificates tzdata

Ubuntu Dockerfile 打包

Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates tzdata && rm -rf /var/lib/apt/lists/*