简体   繁体   中英

Laradock how to add package using apk instead of apt-get

I am using Laradock to deploy a Laravel app.

I am facing a problem with generating a PDF file to attach it in an email in a queued job . The pending jobs are handle by the php-worker container.

The problem is that when you want to attach a PDF to an email, which is queued (therefore, handled by the php-worker container) I get the following error:

"sh: /usr/local/bin/wkhtmltopdf: not found

which means that the wkhtmltopdf is not installed in the php-worker container.

So, taking a look at either the php-fpm or workspace Dockerfile, I can see how to install the wkhtmltopdf like so:

#####################################
# wkhtmltopdf:
#####################################

USER root

ARG INSTALL_WKHTMLTOPDF=false

RUN if [ ${INSTALL_WKHTMLTOPDF} = true ]; then \
    apt-get install -yqq \
      libxrender1 \
      libfontconfig1 \
      libx11-dev \
      libjpeg62 \
      libxtst6 \
      fontconfig \
      libjpeg62-turbo \
      xfonts-base \
      xfonts-75dpi \
      wget \
    && wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.stretch_amd64.deb \
    && dpkg -i wkhtmltox_0.12.6-1.stretch_amd64.deb \
    && apt -f install \
;fi

If I copy that installation code into the php-worker container, I get the following error

/bin/sh: apt-get: not found

So, searching further, it seems the php-worker container is Alpine based , and probably needs apk add because of Alpine.

I have tried the following:

#####################################
# wkhtmltopdf:
#####################################

USER root

ARG INSTALL_WKHTMLTOPDF=false

RUN if [ ${INSTALL_WKHTMLTOPDF} = true ]; then \
    apk add --no-cache \
      libxrender1 \
      libfontconfig1 \
      libx11-dev \
      libjpeg62 \
      libxtst6 \
      fontconfig \
      libjpeg62-turbo \
      xfonts-base \
      xfonts-75dpi \
      wget \
      wkhtmltopdf \
;fi

But I haven't got luck.

ERROR: unable to select packages: wkhtmltopdf (no such package): required by: world[wkhtmltopdf]

I have been editing the Dockerfile based on this link and this is what I've modified so far: Dockerfile

#
#--------------------------------------------------------------------------
# Image Setup
#--------------------------------------------------------------------------
#

ARG LARADOCK_PHP_VERSION
FROM php:${LARADOCK_PHP_VERSION}-alpine3.14

LABEL maintainer="Mahmoud Zalt <mahmoud@zalt.me>"

ARG LARADOCK_PHP_VERSION

# If you're in China, or you need to change sources, will be set CHANGE_SOURCE to true in .env.

ARG CHANGE_SOURCE=false
RUN if [ ${CHANGE_SOURCE} = true ]; then \
  # Change application source from dl-cdn.alpinelinux.org to aliyun source
  sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories \
  ;fi

RUN apk --update add wget \
  curl \
  git \
  build-base \
  libmcrypt-dev \
  libxml2-dev \
  linux-headers \
  pcre-dev \
  zlib-dev \
  autoconf \
  cyrus-sasl-dev \
  libgsasl-dev \
  oniguruma-dev \
  libressl \
  libressl-dev \
  supervisor

# ...................

#####################################
# wkhtmltopdf:
#####################################

USER root

ARG INSTALL_WKHTMLTOPDF=false

RUN set -xe; \
if [ ${INSTALL_WKHTMLTOPDF} = true ]; then \
# Install dependencies for wkhtmltopdf
  apk add --update --no-cache --wait 10 \
  && apk --no-cache upgrade \
  && apk add --no-cache \
  bash \
  libstdc++ \
  libx11 \
  libxrender \
  libxext \
  libssl1.1 \
  ca-certificates \
  fontconfig \
  freetype \
  ttf-dejavu \
  ttf-droid \
  ttf-freefont \
  ttf-liberation \
  xvfb \
  #libQt5WebKit \ This throws error. Commented out.
  #libQt5WebKitWidgets \ This throws error. Commented out.
  #ttf-ubuntu-font-family \ This throws error. Commented out.
  && apk add --update --no-cache --virtual .build-deps \
  msttcorefonts-installer \
  vim \
\
# Install microsoft fonts
&& update-ms-fonts \
&& fc-cache -f \
\
# Clean up when done
&& rm -rf /tmp/* \
&& apk del .build-deps \

   && wget http://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/wkhtmltopdf-0.12.6-r0.apk \
   && apk add --allow-untrusted wkhtmltopdf-0.12.6-r0.apk \
   && echo 'WKHTMLTOPDF INSTALLED?' \
   && which wkhtmltopdf \
#   && ln -s /usr/bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf \
   && cp /usr/bin/wkhtmltoimage /usr/local/bin/ \
   && cp /usr/bin/wkhtmltopdf /usr/local/bin/  \
   && chmod +x /usr/local/bin/wkhtmltoimage \
   && chmod +x /usr/local/bin/wkhtmltopdf \
   && echo 'wkhtmltopdf version: ' \
   && /usr/local/bin/wkhtmltopdf -V \
   && echo 'whoami & permissions'  \
   && whoami \
   && ls -lah /usr/bin/ \
   && ls -lah /usr/local/bin/ \

;fi


#
#-----------------------------
# Set PHP memory_limit to infinity
#-------------------------------
#

RUN echo 'set php memory to -1:' \
&& sed -i 's/memory_limit = .*/memory_limit=-1 /' /usr/local/etc/php/php.ini-production  \
&& sed -i 's/memory_limit = .*/memory_limit=-1 /' /usr/local/etc/php/php.ini-development \
&& cp /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini


# ...

Finally, the wkhtmltopdf seems to be installed:

+ apk add --allow-untrusted wkhtmltopdf-0.12.6-r0.apk
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/43) Installing icu-libs (67.1-r2)
(2/43) Installing libpcre2-16 (10.36-r0)
(3/43) Installing qt5-qtbase (5.15.3_git20210406-r0)
(4/43) Installing hicolor-icon-theme (0.17-r1)
(5/43) Installing wayland-libs-server (1.19.0-r0)
(6/43) Installing mesa-gbm (21.1.2-r0)
(7/43) Installing wayland-libs-client (1.19.0-r0)
(8/43) Installing qt5-qtdeclarative (5.15.3_git20210531-r0)
(9/43) Installing libxcomposite (0.4.5-r0)
(10/43) Installing wayland-libs-cursor (1.19.0-r0)
(11/43) Installing wayland-libs-egl (1.19.0-r0)
(12/43) Installing libxkbcommon (1.2.1-r0)
(13/43) Installing qt5-qtwayland (5.15.3_git20210510-r0)
(14/43) Installing mesa-egl (21.1.2-r0)
(15/43) Installing libevdev (1.11.0-r1)
(16/43) Installing mtdev (1.1.6-r0)
(17/43) Installing eudev-libs (3.2.10-r0)
(18/43) Installing libinput-libs (1.18.0-r0)
(19/43) Installing xcb-util-wm (0.4.1-r1)
(20/43) Installing xcb-util (0.4.0-r3)
(21/43) Installing xcb-util-image (0.4.0-r1)
(22/43) Installing xcb-util-keysyms (0.4.0-r1)
(23/43) Installing xcb-util-renderutil (0.3.9-r1)
(24/43) Installing libxkbcommon-x11 (1.2.1-r0)
(25/43) Installing qt5-qtbase-x11 (5.15.3_git20210406-r0)
(26/43) Installing qt5-qtsvg (5.15.3_git20200406-r0)
(27/43) Installing qt5-qtlocation (5.15.3_git20201109-r0)
(28/43) Installing qt5-qtsensors (5.15.3_git20201028-r1)
(29/43) Installing qt5-qtwebchannel (5.15.3_git20201028-r0)
(30/43) Installing libxv (1.0.11-r2)
(31/43) Installing alsa-lib (1.2.5-r2)
(32/43) Installing cdparanoia-libs (10.2-r9)
(33/43) Installing gstreamer (1.18.4-r0)
(34/43) Installing libogg (1.3.5-r0)
(35/43) Installing opus (1.3.1-r1)
(36/43) Installing orc (0.4.32-r0)
(37/43) Installing libtheora (1.1.1-r16)
(38/43) Installing libvorbis (1.3.7-r0)
(39/43) Installing gst-plugins-base (1.18.4-r0)
(40/43) Installing hyphen (2.8.8-r1)
(41/43) Installing libxslt (1.1.35-r0)
(42/43) Installing qt5-qtwebkit (5.212.0_alpha4-r14)
(43/43) Installing wkhtmltopdf (0.12.6-r0)
Executing busybox-1.33.1-r7.trigger
OK: 877 MiB in 254 packages
WKHTMLTOPDF INSTALLED?
+ echo 'WKHTMLTOPDF INSTALLED?'
+ which wkhtmltopdf
/usr/bin/wkhtmltopdf
+ cp /usr/bin/wkhtmltoimage /usr/local/bin/
+ cp /usr/bin/wkhtmltopdf /usr/local/bin/
+ chmod +x /usr/local/bin/wkhtmltoimage
+ chmod +x /usr/local/bin/wkhtmltopdf
+ echo 'wkhtmltopdf version: '
+ /usr/local/bin/wkhtmltopdf -V
wkhtmltopdf version: 
wkhtmltopdf 0.12.6

+ echo 'whoami & permissions'
+ whoami
whoami & permissions
root
+ ls -lah /usr/bin/
-rwxr-xr-x    1 root     root         979 Jun  1  2021 supervisorctl
-rwxr-xr-x    1 root     root         975 Jun  1  2021 supervisord
-rwxr-xr-x    1 root     root      114.1K Jun 11  2020 wkhtmltoimage
-rwxr-xr-x    1 root     root      162.1K Jun 11  2020 wkhtmltopdf
+ ls -lah /usr/local/bin/
-rwxr-xr-x    1 root     root      114.1K May 25 16:37 wkhtmltoimage
-rwxr-xr-x    1 root     root      162.1K May 25 16:37 wkhtmltopdf

Step 82/86 : COPY supervisord.conf /etc/supervisord.conf
 ---> de059f102569
Step 83/86 : ENTRYPOINT ["/usr/bin/supervisord", "-n", "-c",  "/etc/supervisord.conf"]

BUT when I try to execute the container to verify that the wkhtmltopdf is indeed installed,

❯ docker container exec php-worker /usr/local/bin/wkhtmltopdf -V                                                                                                        ─╯
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "/usr/local/bin/wkhtmltopdf": stat /usr/local/bin/wkhtmltopdf: no such file or directory: unknown

turns out that it's not been installed! And therefore, I get the exact same error in my application:

"sh: /usr/local/bin/wkhtmltopdf: not found

And, on the other hand, for example, the supervisor does work:

❯ docker container exec php-worker supervisorctl                                                                                                                        ─╯
laravel-scheduler:laravel-scheduler_00   RUNNING   pid 52576, uptime 18:27:24
laravel-worker:laravel-worker_00         RUNNING   pid 52577, uptime 18:27:24

supervisor> 

Does anybody know how to install wkhtmltopdf in Alpine Dockerfile for real?

The PHP images you're using are built on Alpine 3.15; it looks like wkhtmltopdf isn't package in that version of Alpine:

$ docker run --rm alpine:3.15 sh -c 'apk add --update wkhtmltopdf'
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
ERROR: unable to select packages:
  wkhtmltopdf (no such package):
    required by: world[wkhtmltopdf]

It looks like wkhtmltopdf is only available in 3.14 and earlier (I checked 3.14 and 3.13):

$ docker run --rm alpine:3.14 sh -c 'apk add --update wkhtmltopdf'
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/103) Installing dbus-libs (1.12.20-r2)
(2/103) Installing libgcc (10.3.1_git20210424-r2)
[...]
(103/103) Installing wkhtmltopdf (0.12.6-r0)
Executing busybox-1.33.1-r7.trigger
OK: 196 MiB in 117 packages

This is noted in the release notes for 3.15 , which say:

QtWebKit was removed due to lack of upstream support

qt5-qtwebkit, kdewebkit, wkhtmltopdf, and py3-pdfkit have been removed due to known vulnerabilities and lack of upstream support for qtwebkit. Other programs have been adjusted to use qt5-qtwebengine where appropriate. The most direct replacement for wkhtmltopdf is weasyprint, which is available in the Alpine Linux community repository. puppeteer and pandoc are also options, depending on your needs. See #12888 for more information.

You could dry building your own PHP base image on top of an older Alpine release using the upstream Dockerfile , or you could try starting with the vanilla alpine:3.14 image and installing php using apk .

Or just stick with an Ubuntu-based image, which still packages wkhtmltopdf .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM