Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting ext-tidy to work on alpine-based PHP images

The problem I ran into is that the libtidy that ext-tidy compiles against on recent (3.7+) Alpine distributions is 5.6.0, while Debian-based distributions use libtidy 5.2.0. When applications using ext-tidy run against libtidy 5.6.0, they produce content that libxml2 cannot deal with (e.g., when using DOMDocument::loadXML()), whereas the earlier versions work fine.

As such, I needed to find a way to:

  • Install libtidy 5.2.0
  • Compile ext-tidy against it
# DOCKER-VERSION 1.3.2
FROM php:7.2-cli-alpine3.8
# Compile-time dependencies
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/community' >> /etc/apk/repositories
RUN apk update && \
apk add --no-cache 'tidyhtml-dev==5.2.0-r1'
# Install the extension
RUN docker-php-ext-install -j$(nproc) tidy
@andrewscaya

This comment has been minimized.

Copy link

andrewscaya commented Nov 1, 2018

Would compiling from source be a viable solution for you?

If so, the modified Dockerfile would look something like this:


# DOCKER-VERSION        1.3.2

FROM php:7.2-cli-alpine3.8

# Compile-time dependencies
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/community' >> /etc/apk/repositories
RUN apk update && \
  apk add gcc g++ make cmake git
RUN cd && \
  git clone https://github.com/htacg/tidy-html5.git && \
  cd tidy-html5 && \
  git checkout 5.2.0 && \
  cd build/cmake/ && \
  cmake -DCMAKE_INSTALL_PREFIX=/usr \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_TAB2SPACE=ON \
  ../.. && \
  make && \
  make install && \
  install -v -m755 tab2space /usr/bin && \
  cd && \
  rm -rf tidy-html5

# Install the extension
RUN docker-php-ext-install -j$(nproc) tidy

I've pushed my test build to my Docker Hub: 'docker pull andrewscaya/test_php7211-tidy520'. You can pull the image and see if this is enough to "fix" the issue for now.

If I can be of any further help, please let me know. Cheers!

@weierophinney

This comment has been minimized.

Copy link
Owner Author

weierophinney commented Nov 12, 2018

That's totally a potential approach as well. Considering that most of the dev dependencies are already present to allow docker-php-ext-install to be able to do its work, that would be a nice alternative to adding the dev packages from a previous version. I'll definitely be giving this a try!

@mehdichaouch

This comment has been minimized.

Copy link

mehdichaouch commented Jun 19, 2019

In my case I used this in my Dockerfile, with an 3.8 alpine, php:5.6-alpine

## Install Tidy
ENV TIDY_VERSION=5.6.0
RUN set -x \
    && mkdir -p /usr/local/src \
    && cd /usr/local/src \
    && curl -q https://codeload.github.com/htacg/tidy-html5/tar.gz/$TIDY_VERSION | tar -xz \
    && cd tidy-html5-$TIDY_VERSION/build/cmake \
    && cmake ../.. && make install \
    && ln -s tidybuffio.h ../../../../include/buffio.h \
    && cd /usr/local/src \
    && rm -rf /usr/local/src/tidy-html5-$TIDY_VERSION
RUN docker-php-ext-install -j$(nproc) tidy
@patricknelson

This comment has been minimized.

Copy link

patricknelson commented Aug 24, 2019

I'm a Docker n00b, but @andrewscaya I found that if you can combine those last two RUN commands into a single command, you can reduce the image size by roughly 188MB or so (as long as you're continually ensuring gcc and various compilers are not maintained on your images).

The technique is to ensure you also delete all the newly installed dependencies in the same layer, saving on the final image build. In my case, I am using Alpine in the first place to help reduce my build overall, so I only keep the compile tools temporarily within the build layer itself. With the help of your code above and then adding one extra line at the bottom, I was able to slash my built image down from 371MB down to 183MB (so, great reduction):

RUN apk update && \
  apk add gcc g++ make cmake git && \
  cd && \
  git clone https://github.com/htacg/tidy-html5.git && \
  cd tidy-html5 && \
  git checkout 5.2.0 && \
  cd build/cmake/ && \
  cmake -DCMAKE_INSTALL_PREFIX=/usr \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_TAB2SPACE=ON \
  ../.. && \
  make && \
  make install && \
  install -v -m755 tab2space /usr/bin && \
  cd && \
  rm -rf tidy-html5  && \
  apk del --no-cache gcc g++ make cmake git

Note the extra line at the bottom: apk del --no-cache gcc g++ make cmake git.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.