Pherserk Pherserk - 1 year ago 223
PHP Question

add yaml extension to php on using official Alpine Docker image

I'm using this offical php Docker image: https://github.com/docker-library/php/blob/76a1c5ca161f1ed6aafb2c2d26f83ec17360bc68/7.1/alpine/Dockerfile

Now I need to add support for yaml extension, that is not bundled with php.
I see the base image I'm using uses phpize.

I'm trying with this approach:

FROM php:7.1.5-alpine

# Install and enable yaml extension support to php
RUN apk add --update yaml yaml-dev
RUN pecl channel-update pecl.php.net
RUN pecl install yaml-2.0.0 && docker-php-ext-enable yaml


But I get this errors:

running: phpize
Configuring for:
PHP Api Version: 20160303
Zend Module Api No: 20160303
Zend Extension Api No: 320160303
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

ERROR: `phpize' failed
ERROR: Service 'php_env' failed to build: The command '/bin/sh -c pecl install yaml-2.0.0 && docker-php-ext-enable yaml' returned a non-zero code: 1


What is the most idiomatic docker way to use that image and add that support?

Should I use it as base, or is someway possible to add parameters in order to make wanted extension configurable?

Answer Source

Alpine uses apk to install packages. The compiling process is complaining about missing autoconf, which is found in Alpine's autoconf package.

I'd suggest you to run these commands:

RUN apk add --no-cache --virtual .build-deps \
    g++ make autoconf yaml-dev

RUN pecl channel-update pecl.php.net
RUN pecl install yaml-2.0.0 && docker-php-ext-enable yaml

RUN apk del --purge .build-deps

If you need to install other non-dev libraries, you can install them in a separate apk add command. This procedure will:

  1. install the build deps, using --no-cache means you're using an updated index and not cached locally (thus no need of --update or to save the pkg in the cache). --virtual means you're creating a virtual reference for all those packages that can later be deleted (because they're useless after the compiling process)

  2. do your stuff with pecl and docker-php-ext-enable

  3. delete the previous build deps

If you still encounter any missing dependency, you can see as reference this: https://pkgs.alpinelinux.org/packages

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download