4

I have a Golang program inside a docker container (I use Ubuntu 18). Also I use github.com/glenn-brown/golang-pkg-pcre/src/pkg/pcre for regex in my Golang app. Before using this library I should install libpcre++-dev this way:

sudo apt-get install libpcre++-dev

But I use golang:alpine in my Dockerfile and this is no libpcre++-dev library in alpine packages.

What package should I install instead of libpcre++-dev?

p.s. I have tried to install libc6-compat, pcre pcre-dev, libpcrecpp but I see this error:

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

My Dockerfile:

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

I build my app this way:

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

EDIT

I have change my Dockerfile (add line below)

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

And now I have a new error:

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

Klimbo
  • 1,308
  • 3
  • 15
  • 34
  • 1
    `RUN apk add --virtual build-dependencies RUn apk add --no-cache build-base gcc` try this or you can install the alpine sdk `apk add --update alpine-sdk` – Adiii Sep 21 '19 at 09:49
  • @Adiii On last step (deploy to server) I have an error [20278] INTERNAL ERROR: cannot create temporary directory! – Klimbo Sep 21 '19 at 10:05
  • does the above solve the issue? – Adiii Sep 21 '19 at 10:07
  • https://github.com/docker/compose/issues/3262 – Adiii Sep 21 '19 at 10:07
  • @Adiii I have fixed `[20278] INTERNAL ERROR: cannot create temporary directory!` but I still get `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` – Klimbo Sep 21 '19 at 10:28
  • you can ask another question this is different issue – Adiii Sep 21 '19 at 10:30
  • @Adiii It is not a different problem. I have the same error message as I had before – Klimbo Sep 21 '19 at 10:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199765/discussion-between-adiii-and-klimbo). – Adiii Sep 21 '19 at 10:32
  • https://pkgs.alpinelinux.org/packages might be a good starting point to try to find packages; there's a link from the [Docker Hub `alpine` image page](https://hub.docker.com/_/alpine/). – David Maze Sep 21 '19 at 14:05
  • @DavidMaze I know this site and I have tested a lot of packages but all of them does not work – Klimbo Sep 21 '19 at 14:07

2 Answers2

3

You can try one of these, as both package

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).

Or you can install the alpine 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.

RUN apk add --update alpine-sdk

docker-alpine-issues-24

Or you can use golang:latest which will work fine.

FROM golang:latest
RUN apt-get update 
RUN apt-get install libpcre++-dev -y
Adiii
  • 54,482
  • 7
  • 145
  • 148
1

You can use one of the Debian-based golang images instead. By the time you're installing GNU libc and a full C toolchain on top of this anyways, there's not really going to be much space savings over the Alpine base image. You can (and should) use a multi-stage build where the final image just contains your compiled binary, and that can use an Alpine base.

The result would look something like:

# 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"]
David Maze
  • 130,717
  • 29
  • 175
  • 215