Install Docker on CentOS. See official page for details https://docs.docker.com/engine/installation/linux/docker-ce/centos/
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum makecache fast
yum install docker-ce
systemctl enable docker
systemctl start docker
(1). Build an image of CentOS7
docker pull centos:centos7
docker run -it centos:centos7 /bin/bash
(2). Introduce LibreOffice and IPA font
yum update -y
yum install libreoffice* ipa-*-fonts
(3). Create a working folder
mkdir /tmp/
exit
(4). Check the Docker CONTAINER ID
docker ps -a
(5). Commit with the name "office" Enter the CONTAINER ID confirmed earlier in * $ {CONTAINER ID} *.
docker commit ${CONTAINER ID} office
(6). Check if it works properly It is assumed that the "/var/tmp/test.xlsx" file exists on CentOS. If executed normally, "/var/tmp/test.pdf" will be generated.
docker run --rm=true -it -v /var/tmp:/tmp office libreoffice --headless --nologo --nofirststartwizard --convert-to pdf --outdir /tmp /tmp/test.xlsx
This is a sample to start docker with java process class.
/**
*Convert PDF via Docker
* @param excelFileName
* @param pdfFileName
* @return
* @throws InterruptedException
*/
public boolean convertPDF(String excelFileName) throws InterruptedException {
//Start command specification
String[] command = {
"docker", "run", "--rm=true", "-v", "/var/tmp:/tmp", "office", "libreoffice", "--headless"
, "--nologo", "--nofirststartwizard", "--convert-to", "pdf", "--outdir"
,"/tmp", "/tmp/"+excelFileName };
ProcessBuilder pb = new ProcessBuilder(command);
try {
//Command execution
Process process = pb.start();
//Wait for the result of command execution
int ret = process.waitFor();
//Standard output
InputStream is = process.getInputStream();
printInputStream(is);
//Standard error
InputStream es = process.getErrorStream();
printInputStream(es);
}
catch (Exception e) {
return false;
}
return true;
}
/**
*Display standard error output
* @param is
* @throws IOException
*/
public static void printInputStream(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
for (;;) {
String line = br.readLine();
if (line == null) break;
System.out.println(line);
}
} finally {
br.close();
}
}
Recommended Posts