This guide shows how to run GroupDocs.Comparison 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 compare two documents in a Docker container using the sample code. The final Docker image can be run as an application by passing the source files for comparison, 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.
Project Structure
π basic-example/
βββ π work/
β βββ π proposal_v1.docx # Input document β first version
β βββ π proposal_v2.docx # Input document β second version
β βββ π GroupDocs.Comparison.lic # The license file (optional)
βββ π .dockerignore # Files to exclude from Docker build context
βββ π Dockerfile # Docker container definition
βββ π package.json # Node.js project file
βββ π diff-files.js # Main application code
βββ π README.md # App readme
Letβs look at the most essential parts of the application:
'use strict';constgroupdocs=require('@groupdocs/groupdocs.comparison');// Get the source, target, output, and license paths from the command line arguments
const[sourcePath,targetPath,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=newgroupdocs.License()license.setLicense(licensePath);}// Create a new comparer instance and add the source and target files
constcomparer=newgroupdocs.Comparer(sourcePath);// Add the target file to the comparer
comparer.add(targetPath);// Compare the source and target files and save the output to the output path
comparer.compare(outputPath);
{"name":"GroupDocs.Comparison for Node.js via Java Example","version":"1.0.0","private":true,"main":"diff-files.js","license":"MIT","scripts":{"start":"node diff-files.js","dev":"node diff-files.js"},"engines":{"node":">=20"},"dependencies":{"@groupdocs/groupdocs.comparison":"^25.11.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/diff-files.js"]CMD["--help"]