This guide shows how to run GroupDocs.Conversion 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 convert 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, output file name, output format, 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:
'use strict';// Import required modules
constgroupdocs=require('@groupdocs/groupdocs.conversion');constpath=require('path')// Get the source, output, and license paths from the command line arguments
const[sourcePath,outputPath,licensePath]=process.argv.slice(2);// Get the license path from the command line arguments
if(licensePath){constlicense=newgroupdocs.License()license.setLicense(path.join(__dirname,licensePath));}// Create a new converter instance and load the source file
constconverter=newgroupdocs.Converter(sourcePath);// Set the convert options for PDF format
constoptions=newgroupdocs.PdfConvertOptions();// Convert the file and save to the output path
converter.convert(outputPath,options);// Exit the process
process.exit(0);
{"name":"GroupDocs.Conversion for Node.js via Java Example","version":"1.0.0","private":true,"main":"convert-file.js","license":"MIT","scripts":{"start":"node convert-file.js","dev":"node convert-file.js"},"engines":{"node":">=20"},"dependencies":{"@groupdocs/groupdocs.conversion":"^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/convert-file.js"]CMD["--help"]