Laradock 如何使用 apk 而不是 apt-get 添加包

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

我正在使用 Laradock 部署一个 Laravel 应用程序。

我在排队作业中生成 PDF 文件以将其附加到电子邮件中时遇到问题。挂起的作业由 php-worker 容器处理。

问题是,当您想将 PDF 附加到排队的电子邮件(因此,由 php-worker 容器处理)时,我收到以下错误:

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

这意味着 wkhtmltopdf 没有安装在 php-worker 容器中。

所以,看看 php-fpm or workspace Dockerfile,我可以看到如何像这样安装 wkhtmltopdf

#####################################
# 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

如果我将该安装代码复制到 php-worker 容器中,我会收到以下错误

/bin/sh: apt-get: not found

所以,进一步搜索,似乎 php-worker container is Alpine based,并且可能需要 apk add 因为 Alpine。

我试过以下方法:

#####################################
# 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

但是我没有运气。

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

我一直在编辑 Dockerfile based on this link,这是我到目前为止所做的修改: 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


# ...

最后,wkhtmltopdf似乎要安装:

+ 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"]

但是 当我尝试执行容器以验证确实安装了 wkhtmltopdf 时,

❯ 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

原来 它还没有安装! 因此,我在我的应用程序中得到了完全相同的错误:

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

而且,另一方面,例如,主管确实在工作:

❯ 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> 

有人知道如何在 Alpine Dockerfile 中安装 wkhtmltopdf 吗?

您使用的 PHP 图像是基于 Alpine 3.15 构建的;看起来 wkhtmltopdf 没有包含在该版本的 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]

看起来 wkhtmltopdf 仅适用于 3.14 及更早版本(我 检查 3.14 和 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

这在 release notes for 3.15 中注明:

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.

您可以使用 upstream Dockerfile 在较旧的 Alpine 版本之上干构建您自己的 PHP 基础映像,或者您可以尝试从香草 alpine:3.14 映像开始并安装 php 使用 apk.

或者只使用 Ubuntu-based 图片,它仍然打包 wkhtmltopdf.