如何在node:7 Docker镜像中安装/升级至Ghostscript 9.21
Hey there, since your node:7 image is based on Debian Jessie, the default Ghostscript version in the official repo is indeed 9.06—which doesn't meet the minimum 9.21 requirement for ghostscript4js. Let's fix that with two reliable approaches:
Option 1: Compile from Source (Recommended)
This method gives you full control over the version and avoids relying on external repos. Replace the existing Ghostscript installation steps in your Dockerfile with this:
FROM node:7 ARG JOB_TOKEN RUN apt-get update && \ apt-get install -y pdftk ENV APP_DIR="/usr/src/app" \ JOB_TOKEN=${JOB_TOKEN} \ GS4JS_HOME="/usr/lib" COPY ./ ${APP_DIR} # Step 1: Set working directory WORKDIR ${APP_DIR} # Step 2: Install dependencies & compile Ghostscript 9.21 ARG CACHE_DATE=2017-01-01 RUN \ apt-get update && \ # Install build tools and dependencies needed for Ghostscript apt-get install -y build-essential make gcc g++ python python-dev python-pip python-virtualenv wget libpng-dev libjpeg-dev libtiff-dev libx11-dev && \ # Download Ghostscript 9.21 source code wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs921/ghostscript-9.21.tar.gz && \ tar xzf ghostscript-9.21.tar.gz && \ cd ghostscript-9.21 && \ # Configure and compile (install to /usr so system can find it) ./configure --prefix=/usr && \ make && \ make install && \ # Clean up to reduce image size cd .. && \ rm -rf ghostscript-9.21 ghostscript-9.21.tar.gz && \ apt-get purge -y wget && \ apt-get autoremove -y && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* # Install libgs-dev if required by ghostscript4js RUN apt-get update && \ apt-get install -y libgs-dev && \ rm -rf /var/lib/apt/lists/* # Install npm dependencies RUN npm install # Step 3: Start the app CMD ["npm", "run", "start"]
Key Notes:
- We added essential build dependencies like
libpng-devandlibjpeg-dev—Ghostscript needs these to handle image-based PDFs. - Installing to
/usrensures thegscommand and libraries are available system-wide, which ghostscript4js expects. - We clean up all temporary files and unused packages to keep the image as small as possible.
Option 2: Use Debian Jessie Backports
If you prefer a faster install without compiling, Debian Jessie's backports repo has Ghostscript 9.21. Here's how to adjust your Dockerfile:
FROM node:7 ARG JOB_TOKEN RUN apt-get update && \ apt-get install -y pdftk ENV APP_DIR="/usr/src/app" \ JOB_TOKEN=${JOB_TOKEN} \ GS4JS_HOME="/usr/lib" COPY ./ ${APP_DIR} # Step 1: Set working directory WORKDIR ${APP_DIR} # Step 2: Install dependencies & Ghostscript from backports ARG CACHE_DATE=2017-01-01 RUN \ apt-get update && \ # Add Jessie backports repo to sources echo "deb http://deb.debian.org/debian jessie-backports main" >> /etc/apt/sources.list && \ apt-get install -y build-essential make gcc g++ python python-dev python-pip python-virtualenv && \ # Install Ghostscript 9.21 from backports apt-get -t jessie-backports install -y ghostscript && \ apt-get install -y libgs-dev && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* # Install npm dependencies RUN npm install # Step 3: Start the app CMD ["npm", "run", "start"]
Heads Up:
- Make sure the backports repo URL is accessible. If you're in a region with slow access, you can swap it for a local mirror.
- This method is quicker but depends on the backports repo staying available.
To verify it worked, you can add RUN gs --version to your Dockerfile—this will print the installed version during the build process.
内容的提问来源于stack exchange,提问作者versarthur




