Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

./main.go:26:2: undefined: imagick.Initialize | ./main.go:27:8: undefined: imagick.Terminate | ./main.go:28:8: undefined: imagick.NewMagickWand #269

Closed
andysteve opened this issue Jan 1, 2022 · 18 comments

Comments

@andysteve
Copy link

andysteve commented Jan 1, 2022

Hello am using Debian with docker to install the ImageMagick library
in our project
but still am getting an error

"gopkg.in/gographics/imagick.v3/imagick"

Docker file


FROM golang:1.15 AS builder

COPY ${PWD} /app
WORKDIR /app

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .

# Toggle CGO based on your app requirement. CGO_ENABLED=1 for enabling CGO
RUN CGO_ENABLED=0 go build -ldflags '-s -w' -o /app/appbin *.go
# Use below if using vendor
# RUN CGO_ENABLED=0 go build -mod=vendor -ldflags '-s -w -extldflags "-static"' -o /app/appbin *.go

FROM debian:stable-slim

# Following commands are for installing CA certs (for proper functioning of HTTPS and other TLS)
RUN apt-get update && apt-get install -y --no-install-recommends \
		ca-certificates  \
        netbase \
        && rm -rf /var/lib/apt/lists/ \
        && apt-get autoremove -y && apt-get autoclean -y

# Add new user 'appuser'. App should be run without root privileges as a security measure
RUN adduser --home "/appuser" --disabled-password appuser \
    --gecos "appuser,-,-,-"
USER appuser

COPY --from=builder /app /home/appuser/app

WORKDIR /home/appuser/app

# Since running as a non-root user, port bindings < 1024 are not possible
# 8000 for HTTP; 8443 for HTTPS;
EXPOSE 2053
EXPOSE 2053

CMD ["./appbin"]

Error message

image

# command-line-arguments
./main.go:26:2: undefined: imagick.Initialize
./main.go:27:8: undefined: imagick.Terminate
./main.go:28:8: undefined: imagick.NewMagickWand

kindly help

@justinfx
Copy link
Member

justinfx commented Jan 1, 2022

How did you come to the idea to try and disable cgo in your build? This library is a binding around the ImageMagick C library, so cgo is a hard requirement. If you disable it, then you get errors when it does not generate any C code or link against ImageMagick.

@andysteve
Copy link
Author

andysteve commented Jan 2, 2022

Thank you so much @justinfx
have made changes as advised but still getting some errors

FROM golang:1.15 AS builder


COPY ${PWD} /app
WORKDIR /app

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .

 
RUN CGO_ENABLED=1 go build -ldflags '-s -w' -o /app/appbin *.go
 
FROM debian:stable-slim
 
RUN apt-get update && apt-get install -y --no-install-recommends \
		ca-certificates  \
        netbase \
        && rm -rf /var/lib/apt/lists/ \
        && apt-get autoremove -y && apt-get autoclean -y

# Add new user 'appuser'. App should be run without root privileges as a security measure
RUN adduser --home "/appuser" --disabled-password appuser \
    --gecos "appuser,-,-,-"
USER appuser

COPY --from=builder /app /home/appuser/app

WORKDIR /home/appuser/app

# Since running as a non-root user, port bindings < 1024 are not possible
# 8000 for HTTP; 8443 for HTTPS;
EXPOSE 2053
EXPOSE 2053

CMD ["./appbin"]

Error

image

# pkg-config --cflags  -- MagickWand MagickCore MagickWand MagickCore
Package MagickWand was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickWand.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickWand' found
Package MagickCore was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickCore' found
Package MagickWand was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickWand.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickWand' found
Package MagickCore was not found in the pkg-config search path.
Perhaps you should add the directory containing `MagickCore.pc'
to the PKG_CONFIG_PATH environment variable
No package 'MagickCore' found
pkg-config: exit status 1

Thank you so much for the help.

@justinfx
Copy link
Member

justinfx commented Jan 3, 2022

@andysteve we are back to your originally posted issue where you have left ImageMagick out of your build and runtime images. It's a hard requirement.

@andysteve
Copy link
Author

Is there a way you can help me out on this such that i include it in my build and run time images?
a will be greatful

@justinfx
Copy link
Member

Well in your previous issue (#268) you were installing the imagemagick package. And now in this new Dockerfile, you stopped installing it. So you need to update your apt command to install imagemagick again in BOTH your builder image AND your runtime image:

FROM golang:1.15 AS builder

RUN apt-get update && apt-get install -y --no-install-recommends imagemagick 

COPY ${PWD} /app
WORKDIR /app

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .

 
RUN CGO_ENABLED=1 go build -ldflags '-s -w' -o /app/appbin *.go
 
FROM debian:stable-slim
 
RUN apt-get update && apt-get install -y --no-install-recommends \
		ca-certificates  \
        netbase \
        imagemagick \
        && rm -rf /var/lib/apt/lists/ \
        && apt-get autoremove -y && apt-get autoclean -y

# Add new user 'appuser'. App should be run without root privileges as a security measure
RUN adduser --home "/appuser" --disabled-password appuser \
    --gecos "appuser,-,-,-"
USER appuser

COPY --from=builder /app /home/appuser/app

WORKDIR /home/appuser/app

# Since running as a non-root user, port bindings < 1024 are not possible
# 8000 for HTTP; 8443 for HTTPS;
EXPOSE 2053
EXPOSE 2053

CMD ["./appbin"]

@andysteve
Copy link
Author

Thank you @justinfx

let me try and get back to you.

@andysteve
Copy link
Author

have tried following up on the docker file you have provided @justinfx and am thanks full.

but still, i get an error bellow

"gopkg.in/gographics/imagick.v3/imagick"

Docker file

FROM golang:1.15 AS builder

RUN apt-get update && apt-get install -y --no-install-recommends imagemagick pkg-config libmagickwand-dev

COPY ${PWD} /app
WORKDIR /app

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .

RUN export CGO_CFLAGS_ALLOW='-Xpreprocessor'

RUN CGO_ENABLED=1 go build -ldflags '-s -w' -o /app/appbin *.go

FROM debian:stable-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
		ca-certificates  \
        netbase \
        imagemagick \
        libmagickwand-dev \
        && rm -rf /var/lib/apt/lists/ \
        && apt-get autoremove -y && apt-get autoclean -y

# Add new user 'appuser'. App should be run without root privileges as a security measure
RUN adduser --home "/appuser" --disabled-password appuser \
    --gecos "appuser,-,-,-"
USER appuser

COPY --from=builder /app /home/appuser/app

WORKDIR /home/appuser/app

# Since running as a non-root user, port bindings < 1024 are not possible
# 8000 for HTTP; 8443 for HTTPS;
EXPOSE 2053
EXPOSE 2053

CMD ["./appbin"]

Error

image

@justinfx
Copy link
Member

Your Debian docker image is likely providing you with ImageMagick 6 instead of 7. Please switch to using the v2 version of the imagick bindings instead of v3.
If you want to use v3 then you need to either build ImageMagick 7 in your builder, from source, or set up the package manager to pull an ImageMagick 7 package.

@andysteve
Copy link
Author

Thank you so much @justinfx for helping me out on this.
May the Almighty God bless you indeed.

My Docker image was finally built successfully
let me share it.

I changed to this version and it's the one that is working.

"gopkg.in/gographics/imagick.v2/imagick"

FROM golang:1.15 AS builder

RUN apt-get update && apt-get install build-essential -y --no-install-recommends imagemagick pkg-config libmagickwand-dev

COPY ${PWD} /app
WORKDIR /app

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .

RUN export CGO_CFLAGS_ALLOW='-Xpreprocessor'

RUN CGO_ENABLED=1 go build -ldflags '-s -w' -o /app/appbin *.go

FROM debian:stable-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
		ca-certificates  \
        netbase \
        imagemagick \
        libmagickwand-dev \
        && rm -rf /var/lib/apt/lists/ \
        && apt-get autoremove -y && apt-get autoclean -y

# Add new user 'appuser'. App should be run without root privileges as a security measure
RUN adduser --home "/appuser" --disabled-password appuser \
    --gecos "appuser,-,-,-"
USER appuser

COPY --from=builder /app /home/appuser/app

WORKDIR /home/appuser/app

# Since running as a non-root user, port bindings < 1024 are not possible
# 8000 for HTTP; 8443 for HTTPS;
EXPOSE 2053
EXPOSE 2053

CMD ["./appbin"]

the only error that am facing now is

ERROR_POLICY: attempt to perform an operation not allowed by the security policy PDF' @ error/constitute.c/IsCoderAuthorized/421`

image

@justinfx
Copy link
Member

justinfx commented Jan 15, 2022

the only error that am facing now is

ERROR_POLICY: attempt to perform an operation not allowed by the security policy PDF' @error/constitute.c/IsCoderAuthorized/421`

That is specific to ImageMagick GhostScript plugin. This link will probably help you:
https://stackoverflow.com/a/59193253/496445

@andysteve
Copy link
Author

Thanks @justinfx
Have tested solution that you recommended
but as if the solution in there does not work with docker

i still continue to get this error

ERROR_POLICY: attempt to perform an operation not allowed by the security policy PDF' @ error/constitute.c/IsCoderAuthorized/421`

is there a way i can solve this issue via docker image?

@justinfx
Copy link
Member

Yes I would have thought you could fix it in your runtime docker image, by editing or removing the policy file.

@andysteve
Copy link
Author

Thank you so much my friend @justinfx
I really appreciate you help towards helping me on this assignment of mine.

I managed to solve most of the bugs, with docker issues,
Docker doesnt bring any error at all

Docker

FROM golang:1.15 AS builder
RUN apt-get update && apt-get install -y --no-install-recommends imagemagick pkg-config libmagickwand-dev ghostscript

COPY ${PWD} /app
WORKDIR /app

COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .

RUN export CGO_CFLAGS_ALLOW='-Xpreprocessor'

RUN CGO_ENABLED=1 go build -ldflags '-s -w' -o /app/appbin *.go

FROM debian:stable-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
		ca-certificates  \
        netbase \
        imagemagick \
        ghostscript \
        libmagickwand-dev \
        && rm -rf /var/lib/apt/lists/ \
        && apt-get autoremove -y && apt-get autoclean -y

RUN sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml

# Add new user 'appuser'. App should be run without root privileges as a security measure
RUN adduser --home "/appuser" --disabled-password appuser \
    --gecos "appuser,-,-,-"
USER appuser

COPY --from=builder /app /home/appuser/app

WORKDIR /home/appuser/app

# Since running as a non-root user, port bindings < 1024 are not possible
# 8000 for HTTP; 8443 for HTTPS;
EXPOSE 2053
EXPOSE 2053

CMD ["./appbin"]

"gopkg.in/gographics/imagick.v2/imagick"

imagick.Initialize()
	defer imagick.Terminate()
	mw := imagick.NewMagickWand()
	defer mw.Destroy()
	err := mw.ReadImage("testDocument.pdf") 
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	mw.SetIteratorIndex(0)
	err1 := mw.SetImageFormat("jpg")
	if err1 != nil {
		fmt.Println(err1.Error())
		return
	}
	err2 := mw.WriteImage("testDocument.jpg") 
	if err2 != nil {
		fmt.Println(err2.Error())
		return
	}

the only thing that is remaining is that
mw.WriteImage() doesnt write the final image to the directory

@justinfx
Copy link
Member

Congrats on getting it working.

the only thing that is remaining is that mw.WriteImage() doesnt write the final image to the directory

Probably because you are writing it into a directory inside of your container. You likely need to google how to use volumes with docker, to have it mount a directory from your host into the container, where you can write your images and access them on the outside.

@andysteve
Copy link
Author

andysteve commented Jan 17, 2022

Thank you so much for that brother. @justinfx
let me do research on that as well

@andysteve
Copy link
Author

andysteve commented Jan 18, 2022

hello @justinfx
when am running docker i set volume which i use to access files like images and works perfectly well especially when i upload them,

but when it comes to writing image
WriteImage(path)
it doesn't work

sample of volume i use

docker run -it --net=host -v "$(pwd)"files:/files -p

it doesn't write images from docker outside docker
yet all permissions are set

though when I upload a file I can access it via the volume

kindly help

@justinfx
Copy link
Member

justinfx commented Jan 19, 2022

-v "$(pwd)"files:/files

This seems wrong and should be -v "$(pwd)"/files:/files:rw.
The default is a read-only mount, so you need to tell it to be read-write. This support is really outside the scope of the Imagick bindings and is just docker usage.

@andysteve
Copy link
Author

andysteve commented Jan 19, 2022

Thank you @justinfx for supporting me all the way from alpine up to here , I have finally got it working . One more reason to give credit to open source.

My challenge was writing the proper docker file and proper docker run command including ImageMagick to help solve the issue I was getting.
when extracting a thumbnail image from a pdf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants