This guide shows how to run GroupDocs.Viewer for Node.js via Java inside a Docker container using a minimal example.
Limitations
Linux containers are recommended and fully supported.
Ensure adequate fonts are installed in the container for accurate text layout.
For Metered licensing, outbound internet access is required to report consumption. File-license mode works fully offline.
Dependencies
We are going to build the Docker image in two stages to minimize the final image size. In the first stage we build the application, and in the second stage we run it.
The key dependencies for the build stage are Node.js, JDK (Java Development Kit), and build tools. The final runtime stage requires only Node.js and JRE (Java Runtime).
Basic Example
This is a practical example showing how to render a document in a Docker container using the sample code. The final Docker image can be run as an application by passing the source file for rendering, the output file name, and an optional license file. See the Temporary License page for more details.
Note
You can download this sample application from here.
Let’s look at the most essential parts of the application:
import{Viewer,License,HtmlViewOptions}from'@groupdocs/groupdocs.viewer';// Get the input file, output path, and license paths from the command line arguments
const[inputPath,outputPath,licensePathArg]=process.argv.slice(2);// Get the license path from the command line arguments or the environment variable
constlicensePath=licensePathArg||process.env.GROUPDOCS_LICENSE_PATH;if(licensePath){constlicense=newLicense();license.setLicense(licensePath);}// Create a new viewer instance
constviewerInstance=newViewer(inputPath);// Render the file to HTML
constviewOptions=HtmlViewOptions.forEmbeddedResources(outputPath+'/page_{0}.html');viewerInstance.view(viewOptions);
{"name":"GroupDocs.Viewer for Node.js via Java Example","version":"1.0.0","type":"module","private":true,"main":"render-file.js","license":"MIT","scripts":{"start":"node render-file.js","dev":"node render-file.js"},"engines":{"node":">=20"},"dependencies":{"@groupdocs/groupdocs.viewer":"^25.12.0"}}
# Build stage – compiles native module and installs dependenciesFROM node:20-bullseye AS buildRUN apt-get update && apt-get install -y \
openjdk-17-jdk-headless \
build-essential# Clean up cacheRUN rm -rf /var/lib/apt/lists/*# Set JAVA_HOME and update PATHENVJAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64ENVPATH=$JAVA_HOME/bin:$PATH# Set working directoryWORKDIR /app# Copy package filesCOPY package*.json ./# Install dependenciesRUN npm install --omit=dev# Copy the rest of the appCOPY . .# Final stage – slim runtime imageFROM node:20-bullseye-slim AS final# Install Java runtimeRUN apt-get update && apt-get install -y \
openjdk-17-jre-headless # Clean up cacheRUN rm -rf /var/lib/apt/lists/*# Set JAVA_HOME and update PATHENVJAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64ENVPATH=$JAVA_HOME/bin:$PATH# Set working directoryWORKDIR /app# Copy built application and node_modules from the build stageCOPY --from=build /app .# Set the entry point and default commandENTRYPOINT["node","/app/render-file.js"]CMD["--help"]