我在Docker容器中有一个Golang程序(我使用Ubuntu 18)。 我还在我的Golang应用中将github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre用于正则表达式。 在使用此库之前,我应该这样安装libpcre++-dev

sudo apt-get install libpcre++-dev

但是我在Dockerfile中使用了golang:alpine,而在alpine软件包中这不是libpcre++-dev库。

我应该安装什么软件包而不是libpcre++-dev

ps。 我尝试安装libc6-compatpcre pcre-devlibpcrecpp,但是我看到此错误:

github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre
/go/pkg/mod/github.com/glenn-brown/golang-pkg-pcre@v0.0.0-20120522223659-48bb82a8b8ce/src/pkg/pcre/pcre.go:52:10:
fatal error: pcre.h: No such file or directory #include
^~~~~~~~ compilation terminated

我的Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
FROM golang:alpine

RUN apk update
RUN apk upgrade
RUN apk add --update --no-cache build-base gcc g++ pcre pcre-dev libc6-compat

# Install git + SSL ca certificates.
# Git is required for fetching the dependencies.
# Ca-certificates is required to call HTTPS endpoints.
RUN apk update && apk add --no-cache curl git ca-certificates tzdata \\
 && update-ca-certificates 2> /dev/null || true

我以这种方式构建我的应用程序:

1
- CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -ldflags="-w -s" -o bin/backend ./cmd/backend/main.go

编辑

我已经更改了Dockerfile(在下面添加行)

RUN apk add --update --no-cache build-base gcc g++ pcre pcre-dev libc6-compat

现在我有一个新错误:

Error loading shared library libpcre.so.1: No such file or directory
(needed by /bin/backend)

  • RUN apk add --virtual build-dependencies RUn apk add --no-cache build-base gcc尝试此操作,或者您可以安装高山sdk apk add --update alpine-sdk
  • @Adiii在最后一步(部署到服务器)上出现错误[20278]内部错误:无法创建临时目录!
  • 以上解决了这个问题吗?
  • github.com/docker/compose/issues/3262
  • @Adiii我已经修复了[20278] INTERNAL ERROR: cannot create temporary directory!,但我仍然得到了github.comglenn-browngolang-pkg-pcresrcpkgpcre gopkgmodgithub.comglenn-browngolang-pkg-pcre@v0.0.0-20120522223659-48bb82a8b8cesrcpkgpcrepcre.go:52:10: fatal error: pcre.h: No such file or directory #include ^~~~~~~~ compilation terminated
  • 您可以问另一个问题,这是另一个问题
  • @Adiii这不是一个不同的问题。 我有和以前一样的错误信息
  • 让我们继续聊天中的讨论。
  • pkgs.alpinelinux.org/packages可能是尝试查找软件包的良好起点; Docker Hub alpine图片页面上有一个链接。
  • @DavidMaze我知道这个站点,我已经测试了很多软件包,但是它们都不起作用

您可以改为使用基于Debian的golang图像之一。 无论如何,当您在此之上安装GNU libc和完整的C工具链时,在Alpine基本映像上实际上并不会节省太多空间。 您可以(并且应该)使用多阶段构建,其中最终映像仅包含您编译的二进制文件,并且可以使用Alpine基础。

结果如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Build-time image; just has the parts needed to run `go build`
FROM golang:1.12-buster AS build

# Install additional build-time tools
RUN apt-get update \\
 && apt-get install --assume-yes \\
      build-essential ca-certificates git-core tzdata \\
      libpcre++-dev

# Build your application
WORKDIR /app
COPY . .
ENV GO111MODULE=on
RUN go build -o myapp ./cmd/myapp

# Runtime image; has only what we need to run the application
FROM alpine:3.10
# Note that you'll need the shared library for libpcre++
RUN apk add ca-certificates tzdata libpcrepp
COPY --from=build /app/myapp /usr/bin/myapp
CMD ["myapp"]
  • 我有一个错误:加载共享库libpcre.so.3时出错

您可以尝试其中一种,因为两种包装

1
2
RUN apk add --virtual build-dependencies
RUn apk add --no-cache build-base gcc

build-essential is a metapackage (a package that installs many other
packages, like g++ and gcc: the GNU C & C++ compilers).

或者,您可以安装高山sdk。

You can start with alpine-sdk, which is a"metapackage that pulls in
the most essential packages used to build new packages."
http://wiki.alpinelinux.org/wiki/Developer_Documentation has more
info.

1
RUN apk add --update alpine-sdk

docker-alpine-issues-24

或者您可以使用golang:latest,它将正常工作。

1
2
3
FROM golang:latest
RUN apt-get update
RUN apt-get install libpcre++-dev -y
  • 我有一个错误:加载共享库libpcre.so.3时出错