使用官方 Alpine Docker 映像将 yaml 扩展添加到 php

2022-01-14 00:00:00 docker dockerfile alpine php phpize

我正在使用这个官方的 php Docker 镜像:https://github.com/docker-library/php/blob/76a1c5ca161f1ed6aafb2c2d26f83ec17360bc68/7.1/alpine/Dockerfile

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

现在我需要添加对 yaml 扩展的支持,它不与 php 捆绑在一起.我看到我正在使用的基本图像使用 phpize.

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.

我正在尝试这种方法:

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

但我得到这个错误:

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

使用该图像并添加该支持的最惯用的 docker 方式是什么?

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?

推荐答案

Alpine 使用 apk 安装包.编译过程抱怨缺少 autoconf,它位于 Alpine 的 autoconf 包中.

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

如果您需要安装其他非开发库,您可以在单独的 apk add 命令中安装它们.此过程将:

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

  1. 安装构建依赖,使用 --no-cache 意味着您正在使用更新的索引而不是本地缓存(因此不需要 --update 或将 pkg 保存在缓存中).--virtual 表示您正在为所有那些以后可以删除的包创建一个虚拟引用(因为它们在编译过程之后无用)

  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)

用 pecl 和 docker-php-ext-enable 做你的事情

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

删除之前的构建deps

delete the previous build deps

如果您仍然遇到任何缺少的依赖项,您可以参考以下内容:https://pkgs.alpinelinux.org/包

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

相关文章