Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ImageMagick policy on a Dockerfile

There seems to have been a change recently on ImageMagick which breaks conversion from pdf to png. My investigation let me to the policy.xml file and this line:

<policy domain="coder" rights="none" pattern="PDF" />

which needs to be changed to this

<policy domain="coder" rights="read|write" pattern="PDF" />

(Note: I didn't have this problem before).

I am able to change that on my personal machine but now I need to change it on a docker container configured through a Dockerfile. How can I do that?

I have tried something using environment variables without success. You can see my dockerfile here:

https://github.com/VivianePons/public-notebooks

Thank you for your help

like image 232
Viviane Avatar asked Nov 19 '18 14:11

Viviane


People also ask

What's wrong with ImageMagick's Docker test?

When i tested into docker container what's goes wrong, i see error from imagemagick about lost fonts. If you use lightweight version of smth images like linux-alpine or smth else, your images cannot have any important tools like your local machine or VM.

How to modify a docker image?

There are two ways you can modify a docker image. Through Dockerfiles. Using the command docker container commit. I'll explain both methods, and at the end, I'll also add which use case would be better for the method in context. How to confirm that Docker has been installed successfully?

How do I publish my Docker images to a registry?

In any case, you want to publish your images to a Docker registry. They can be published to a cloud-based registry, like the Docker Hub which, by the way, is the default if you don’t explicitly specify the registry. First create a free Docker ID, then login:

How to check if an image is available in Docker Hub?

Now go to Docker Hub and check the image is there: In Docker Hub with free registration, you can have one private repository, with unlimited public repositories. Otherwise, you may want to run your own Docker registry, which can be done with one command:


2 Answers

This change to your Dockerfile should work

ARG imagemagic_config=/etc/ImageMagick-6/policy.xml

RUN if [ -f $imagemagic_config ] ; then sed -i 's/<policy domain="coder" rights="none" pattern="PDF" \/>/<policy domain="coder" rights="read|write" pattern="PDF" \/>/g' $imagemagic_config ; else echo did not see file $imagemagic_config ; fi
like image 90
Scott Stensland Avatar answered Oct 20 '22 08:10

Scott Stensland


Runtime bind mount

At runtime (docker run), use a bind mounted volume to overwrite the policy.xml inside the container.

A good, straightforward example of doing this can be found in the How to use this image section of the official Nginx image on Docker Hub.

docker run --name some-nginx -v /some/content:/usr/share/nginx/html -d nginx

Here, the -v flag is your bind mount. -v /some/content:/usr/share/nginx/html says "replace /usr/share/nginx/html in the container with the contents of /some/content on my host. The end result for the user -- if /some/content has an index.html or other default home page as determined in the nginx.conf of the container, Nginx will require no configuration changes because the default Nginx configuration is already looking for index.html at that file system location.

NOTE: Bind mounting isn't additive. The directory on the image will be entirely replaced by whats being mounted.

Dockerfile COPY

Or at build time (docker build) use COPY in your Dockerfile to bake your updated policy.xml right into the container, no bind mounting required.

COPY ./path/to/policy.xml /path/in/docker/image/policy.xml

like image 5
bluescores Avatar answered Oct 20 '22 08:10

bluescores