4

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?

Michele Carino
  • 1,043
  • 2
  • 11
  • 25

2 Answers2

8

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 --update yaml-dev
RUN apk add --no-cache --virtual .build-deps \
    g++ make autoconf

RUN pecl channel-update pecl.php.net
RUN pecl install yaml && 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

Tim
  • 2,805
  • 25
  • 21
Jack
  • 1,689
  • 1
  • 13
  • 20
  • 2
    To get `pecl`, readers will need to also do `apk add php7-dev`. – halfer Jun 29 '17 at 10:23
  • It's not working for me. I did a phpinfo() and I have in the "Additional .ini files parsed" field the value "/usr/local/etc/php/conf.d/docker-php-ext-yaml.ini", but the extension seems to not be enabled. When I try an example from the php documentation, I get this error message : "Call to undefined function yaml_emit()". Any ideas ? – Paul Laffitte Aug 21 '17 at 20:59
  • Ok, I feel stupid but I really want to share. I deleted my image and buildt it again, now it's working. Just building again your image without deleting it before can be source of problems. Indeed, when you build again a same image, it use your old builds to create the new one. I'm kind of a newbie with docker, and I didn't except it to work like this, it's very good to know. So thanks for your solution, it's now working perfectly ! – Paul Laffitte Aug 21 '17 at 21:48
  • Juste something else that I forgot and that you should add in your answer. It's not working if the yaml.so file isn't executable. I had to add this line in my run command : chmod +x $(php-config --extension-dir)/yaml.so I hope that it will be helpful – Paul Laffitte Aug 22 '17 at 22:04
  • This must be something new: at the time of writing it worked like what I posted out of the box. I'm going to double check it. – Jack Aug 25 '17 at 07:49
  • 2
    the `apk del --purge .build-deps` breaks it for more me, since it is removing libyaml. I added a `apk add --no-cache yaml` to add the yaml outside of the virtual package to make it work for me – x539 Sep 01 '18 at 09:04
0

The latest version, at least since 2.0.4, requires yaml-dev to be installed in order to run. So move that to the packages you want to keep in the image. Also, on alpine 3.11 in combination of php-alpine repository, I got the problem that it wants you to add the location of your php.ini file.

just add the following: RUN pear config-set php_ini /etc/php7/php.ini

where you change the path into the path of the location of your php.ini if needed.

SomeOne_1
  • 808
  • 10
  • 10