How to add imagick to Docker alpine:latest without pecl

Updated: 19th July 2024
Tags: php docker

I was searching and using ai bots half a day how to to this. In the end it was as easy as pie.

For example your Dockerfile

### Base image
FROM alpine:latest

WORKDIR /var/www

# Install packages
RUN apk update && apk add --no-cache \
  curl \
  nginx \
  php82 \
  php82-pdo \
  php82-pdo_mysql \
  php82-ctype \
  php82-curl \
  php82-dom \
  php82-fileinfo \
  php82-fpm \
  php82-gd \
  php82-intl \
  php82-mbstring \
  php82-mysqli \
  php82-opcache \
  php82-openssl \
  php82-phar \
  php82-session \
  php82-sodium \
  php82-tokenizer \
  php82-xml \
  php82-simplexml \
  php82-xmlreader \
  php82-xmlwriter \
  php82-bcmath \
  php82-exif \
  gcc musl-dev make \
  supervisor

#.....

Instead of installing, using pecl , just add php82-pecl-imagick. It will install extension and imagemagick and turn on the extension. Awesome!

So in the end you should have

### Base image
FROM alpine:latest

WORKDIR /var/www

# Install packages and remove default server definition
RUN apk update && apk add --no-cache \
  curl \
  nginx \
  php82 \
  php82-pdo \
  php82-pdo_mysql \
  php82-ctype \
  php82-curl \
  php82-dom \
  php82-fileinfo \
  php82-fpm \
  php82-gd \
  php82-intl \
  php82-mbstring \
  php82-mysqli \
  php82-opcache \
  php82-openssl \
  php82-phar \
  php82-session \
  php82-sodium \
  php82-tokenizer \
  php82-xml \
  php82-simplexml \
  php82-xmlreader \
  php82-xmlwriter \
  php82-bcmath \
  php82-exif \
  gcc musl-dev make \
  supervisor \
  php82-pecl-imagick

#.....