davidbyttow / govips

A lightning fast image processing and resizing library for Go
MIT License
1.28k stars 199 forks source link

undefined vips.ImageType ImageTypeJPEG ImageTypePNG ImageRef #376

Closed felixgao closed 1 year ago

felixgao commented 1 year ago

I am trying to build a docker image containing the library and code, it seems to not able to find the library after building from source.

ARG GOLANG_IMAGE_TAG="1.20.6-alpine"
FROM golang:${GOLANG_IMAGE_TAG} as base

ARG PDF2IMG_VERSION=dev

# So using 8.13.3 for now

ARG VIPS_VERSION="8.13.3"
RUN apk update && apk add --no-cache \
    automake build-base pkgconfig gcc musl-dev vips-dev glib-dev expat-dev gobject-introspection \
    libjpeg-turbo-dev libpng-dev libwebp-dev giflib-dev librsvg-dev libexif-dev lcms2-dev
ARG VIPS_URL=https://github.com/libvips/libvips/releases/download

ADD ${VIPS_URL}/v${VIPS_VERSION}/vips-${VIPS_VERSION}.tar.gz \
    /tmp/

RUN cd /tmp \
    && tar xf vips-${VIPS_VERSION}.tar.gz \
    && cd vips-${VIPS_VERSION} \
    && CFLAGS="-g -O3" CXXFLAGS="-D_GLIBCXX_USE_CXX11_ABI=0 -g -O3" \
    ./configure \
        # --prefix=/usr \
        # --disable-debug \
        # --disable-dependency-tracking \
        # --disable-introspection \
        # --disable-static \
        # --without-gsf \
        # --without-magick \
        # --without-openslide \
        # --without-pdfium \
        # --enable-gtk-doc-html=no \
        # --enable-gtk-doc=no \
        # --enable-pyvips8=no \
        && make -j4 V=0 \
        && make install

# create a working directory inside the image
WORKDIR /go/src/github.com/felixgao/pdf2img
# Cache go modules
ENV GO111MODULE="on"
ENV CPATH="/usr/local/include"
ENV LIBRARY_PATH="/usr/local/lib"
ENV PKG_CONFIG_PATH="/vips/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig:$PKG_CONFIG_PATH"
ENV GOROOT="/usr/local/go"
ENV GOPATH="/go"
ENV PATH="/go/bin:$PATH"

# Copy go mod and sum files and files i.e all files ending with .go 
COPY go.mod go.sum *.go  ./

# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

RUN go list -mod=readonly -u -m -json all 
# Build the Go app with ldflags
RUN CGO_ENABLED=0 GOOS=linux go build -a \
    -ldflags="-s -w -h -X main.Version=${PDF2IMG_VERSION}" \
    -o pdf2png

# build target image
FROM golang:${GOLANG_IMAGE_TAG} as prod
RUN apk --update add --no-cache \
    fftw glib expat libjpeg-turbo libpng \
    libwebp giflib librsvg libgsf libexif lcms2
WORKDIR /root/
COPY --from=base /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=base /root/libs/* /root/libs/
COPY --from=base /usr/local/lib/* /usr/local/lib/

WORKDIR /app/
COPY --from=base /go/src/github.com/felixgao/pdf2img/pdf2png .

#tell docker what port to expose
EXPOSE 8080

# Run the binary program produced by `go install`
CMD ["./pdf2png"]

the output of the build

❯ DOCKER_BUILDKIT=1 docker build -t pdf2img --target prod .
[+] Building 82.5s (17/22)
 => [internal] load build definition from Dockerfile                                                                                            0.0s
 => => transferring dockerfile: 2.50kB                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                               0.0s
 => => transferring context: 2B                                                                                                                 0.0s
 => [internal] load metadata for docker.io/library/golang:1.20.6-alpine                                                                         1.1s
 => [auth] library/golang:pull token for registry-1.docker.io                                                                                   0.0s
 => https://github.com/libvips/libvips/releases/download/v8.13.3/vips-8.13.3.tar.gz                                                             1.3s
 => [internal] load build context                                                                                                               0.0s
 => => transferring context: 249B                                                                                                               0.0s
 => [prod 1/8] FROM docker.io/library/golang:1.20.6-alpine@sha256:7839c9f01b5502d7cb5198b2c032857023424470b3e31ae46a8261ffca72912a              0.0s
 => CACHED [prod 2/8] RUN apk --update add --no-cache     fftw glib expat libjpeg-turbo libpng  libwebp giflib librsvg libgsf libexif lcms2     0.0s
 => CACHED [prod 3/8] WORKDIR /root/                                                                                                            0.0s
 => CACHED [base 2/9] RUN apk update && apk add --no-cache     automake build-base pkgconfig gcc musl-dev vips-dev glib-dev expat-dev gobject-  0.0s
 => CACHED [base 3/9] ADD https://github.com/libvips/libvips/releases/download/v8.13.3/vips-8.13.3.tar.gz     /tmp/                             0.0s
 => [base 4/9] RUN cd /tmp     && tar xf vips-8.13.3.tar.gz     && cd vips-8.13.3     && CFLAGS="-g -O3" CXXFLAGS="-D_GLIBCXX_USE_CXX11_ABI=0  63.5s
 => [base 5/9] WORKDIR /go/src/github.com/felixgao/pdf2img                                                                                      0.0s
 => [base 6/9] COPY go.mod go.sum *.go  ./                                                                                                      0.0s
 => [base 7/9] RUN go mod download                                                                                                              6.5s
 => [base 8/9] RUN go list -mod=readonly -u -m -json all                                                                                        5.5s
 => ERROR [base 9/9] RUN CGO_ENABLED=0 GOOS=linux go build -a     -ldflags="-s -w -h -X main.Version=dev"     -o pdf2png                        4.5s
------
 > [base 9/9] RUN CGO_ENABLED=0 GOOS=linux go build -a     -ldflags="-s -w -h -X main.Version=dev"     -o pdf2png:
#17 4.389 # github.com/felixgao/pdf_to_png
#17 4.389 ./pdf2png.go:29:29: undefined: vips.ImageType
#17 4.389 ./pdf2png.go:30:7: undefined: vips.ImageTypeJPEG
#17 4.389 ./pdf2png.go:31:7: undefined: vips.ImageTypePNG
#17 4.389 ./pdf2png.go:32:7: undefined: vips.ImageTypeTIFF
#17 4.389 ./pdf2png.go:35:41: undefined: vips.ImageType
#17 4.389 ./pdf2png.go:36:15: undefined: vips.ImageTypeJPEG
#17 4.389 ./pdf2png.go:37:15: undefined: vips.ImageTypePNG
#17 4.389 ./pdf2png.go:38:15: undefined: vips.ImageTypeTIFF
#17 4.389 ./pdf2png.go:41:25: undefined: vips.ImageRef
#17 4.389 ./pdf2png.go:41:86: undefined: vips.ImageMetadata
#17 4.389 ./pdf2png.go:41:86: too many errors
------
executor failed running [/bin/sh -c CGO_ENABLED=0 GOOS=linux go build -a     -ldflags="-s -w -h -X main.Version=${PDF2IMG_VERSION}"     -o pdf2png]: exit code: 1

This is the docker version I am using.

docker version
Client:
 Cloud integration: v1.0.22
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:46:56 2021
 OS/Arch:           darwin/arm64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.5.0 (74594)
 Engine:
  Version:          20.10.12
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.12
  Git commit:       459d0df
  Built:            Mon Dec 13 11:43:07 2021
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

here is my go.mod in case it helps

module github.com/felixgao/pdf_to_png

go 1.20

require github.com/davidbyttow/govips/v2 v2.13.0

require (
    golang.org/x/image v0.9.0 // indirect
    golang.org/x/net v0.12.0 // indirect
    golang.org/x/text v0.11.0 // indirect
)
RyabovNick commented 1 year ago

You need to build with CGO_ENABLED=1

felixgao commented 1 year ago

Thanks, that solved the problem for me.