[PHP8] Install and use PECL YAML function (YAML parser) with Docker

PHP's YAML functions (https://www.php.net/manual/ja/ref.yaml.php) are not bundled with PHP by default.

To parse YAML data into a PHP array (syntax parse and transform), or from a PHP array to YAML data with a YAML function, you need to install the PECL extension Jules (https://www.php) .net / manual / ja / yaml.installation.php).

However, PECL is not installed on the latest (as of 08/28/2020) php: 8.0.0beta2 and php: 8.0.0beta2-alpine images. It seems that PEAR / PECL installer has been eliminated since PHP 7.4.

TL; DR

I need to build PECL from source, which is also a hassle, so I created an image with the pecl command. It works not only with Intel / AMD but also with ARMv5, v7, ARM64 such as Raspberry Pi.

docker pull keinos/php8-jit:latest

--Image: keinos / php8-jit @ DockerHub

$ docker run --rm keinos/php8-jit pecl version
PEAR Version: 1.10.12
PHP Version: 8.0.0-dev
Zend Engine Version: 4.0.0-dev
Running on: Linux 3fc54c34122a 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64

Also, since many pre-built packages in the PECL repository do not work, we also make it possible to install PECL packages from source.

Need yaml-dev


docker-php-ext-pecl install yaml

TS; DR

Dockerfile


FROM keinos/php8-jit:latest

USER root

COPY sample.php /app/sample.php

RUN \
    apk --no-cache add yaml-dev && \
    docker-php-ext-pecl install yaml

ENTRYPOINT [ "php", "/app/sample.php" ]

sample.php


<?php

//YAML sample string
$yaml = <<<EOD
---
invoice: 34843
date: "2001-01-23"
bill-to: &id001
  given: Chris
  family: Dumars
  address:
    lines: |-
      458 Walkman Dr.
              Suite #292
    city: Royal Oak
    state: MI
    postal: 48046
ship-to: *id001
product:
- sku: BL394D
  quantity: 4
  description: Basketball
  price: 450
- sku: BL4438H
  quantity: 1
  description: Super Hoop
  price: 2392
tax: 251.420000
total: 4443.520000
comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.
...
EOD;

//Parse a YAML string into a PHP array
$parsed = yaml_parse($yaml);

//Comparison test
$actual = json_encode($parsed);
$expect = '{"invoice":34843,"date":"2001-01-23","bill-to":{"given":"Chris","family":"Dumars","address":{"lines":"458 Walkman Dr.\n        Suite #292","city":"Royal Oak","state":"MI","postal":48046}},"ship-to":{"given":"Chris","family":"Dumars","address":{"lines":"458 Walkman Dr.\n        Suite #292","city":"Royal Oak","state":"MI","postal":48046}},"product":[{"sku":"BL394D","quantity":4,"description":"Basketball","price":450},{"sku":"BL4438H","quantity":1,"description":"Super Hoop","price":2392}],"tax":251.42,"total":4443.52,"comments":"Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338."}';

echo '- Function test ... ', ($expect === $actual) ? 'OK' : 'NG', PHP_EOL;

References

Recommended Posts

[PHP8] Install and use PECL YAML function (YAML parser) with Docker
Use Puphpeteer with Docker
Use ngrok with Docker
Php settings with Docker
Disposable PHP with Docker
Install Composer with Docker
Use GDAL with Python with Docker
Beginners install docker for mac and prepare php7.0 operating environment
Use log4j2 with YAML + Gradle
[Docker] Use whenever with Docker + Rails
Install java with Ubuntu 16.04 based Docker
Use java with MSYS and Cygwin
Install Java and Tomcat with Ansible
[PDO → DB connection with docker, PHP]
Why use orchestration tools with Docker
Use Git with SourceTree and Eclipse
Use JDBC with Java and Scala.
Hello World with Docker and C
Install Xcode and use Hello Swift!
Microservices With Docker and Cloud Performance
Challenge to install WSL2 and docker
Use cuda11.0 with pytorch using Docker
Install docker and docker-compose on Alpine Linux
Building Rails 6 and PostgreSQL environment with Docker
[Rails] How to use rails console with docker
Install Docker Desktop for Mac with Homebrew
Use Symbolic Link with Docker multi-stage builds
Remove wine3.0 with ubuntu18.04LTS and install wine5.0 (winehq)
Authentication function with Play Framework [Registration and authentication]
Communicate Gitlab and Gitlab Runner launched with Docker
Install Docker with WSL2 Memo ([Part 2] Docker introduction)
Lightweight PHP 7.4 development environment created with Docker
Install Docker and create Java runtime environment
Use ZStandard with .NET Core + Docker (Alpine)