# gopkg.in/gographics/imagick.v2/imagick | /go/pkg/mod/gopkg.in/gographics/imagick.v2@v2.6.0/imagick/affine_matrix.go:8:10: fatal error: wand/MagickWand.h: No such file or directory
andysteve opened this issue · comments
Hello
Am new to golang,
working on a project to take a thumbnail from the first page of a pdf
I need to use the imagick package to help do the task
but am going an error below when I build my docker image
import "gopkg.in/gographics/imagick.v2/imagick"
Docker File
FROM golang:alpine AS build
RUN apk --no-cache add gcc g++ make git pkgconfig imagemagick
WORKDIR /go/src/app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN export CGO_CFLAGS_ALLOW='-Xpreprocessor'
RUN GOOS=linux go build -tags no_pkgconfig -ldflags="-s -w" -o ./bin/web-app ./main.go
FROM alpine:3.13
RUN apk --no-cache add ca-certificates
WORKDIR /usr/bin
COPY --from=build /go/src/app/bin /go/bin
EXPOSE 2000
ENTRYPOINT /go/bin/web-app --port 2000
Error Message
---> Running in d98ec8ab6cd6
# gopkg.in/gographics/imagick.v2/imagick
/go/pkg/mod/gopkg.in/gographics/imagick.v2@v2.6.0/imagick/affine_matrix.go:8:10: fatal error: wand/MagickWand.h: No such file or directory
8 | #include <wand/MagickWand.h>
| ^~~~~~~~~~~~~~~~~~~
compilation terminated.
-------------------------------------------------------------------
Kindly help with this error
Thanks
The issue here is that in your multi-stage docker build, you are only including ImageMagick C libs in the build image, and not in the runtime image. So then you go to run your Go binary and the dynamic linker cannot find the external ImageMagick dependency.
You can see a similar solution, but using a larger debian image:
https://github.com/gographics/imagick/blob/master/Dockerfile
To fix your problem, you will need to include ImageMagick in the second apk
, which is the runtime image.
Actually, that problem I mentioned is not what you are currently encountering, but you WILL encounter it later after you fix the actual problem.
I see that you are using this -tags no_pkgconfig
in your build, which is telling your build to NOT use pkg-config to find ImageMagick. It expects that you would have manually set compile and link flags, which you didn't. Seeing as you added imagemagick from the apk
package, you should get rid of this tag and let it find imagemagick using pkg-config
. Once you build stage is actually completing, your runtime image is still going to need ImageMagick
Thank you @justinfx am so glad for your reply.
have tried out what you have explained.
docker modification
FROM golang:alpine AS build
RUN apk --no-cache add gcc g++ make git pkgconfig imagemagick
WORKDIR /go/src/app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN export CGO_CFLAGS_ALLOW='-Xpreprocessor'
RUN GOOS=linux go build -ldflags="-s -w" -o ./bin/web-app ./main.go
FROM alpine:3.13
RUN apk --no-cache add ca-certificates pkgconfig imagemagick
WORKDIR /usr/bin
COPY --from=build /go/src/app/bin /go/bin
EXPOSE 2000
ENTRYPOINT /go/bin/web-app --port 2000
here is an error I get
Ok well it would seem then that the imagemagick package provided by apk is not provided a well-known pkg-config file. I don't know the right thing for alpine because I actually have found it to cause crashes with imagemagick because of its musl compiler. I would recommend maybe switching to something like debian for building (ie https://github.com/gographics/imagick/blob/master/Dockerfile) and distroless for runtime.
Thank you so much am going to try that.