Files
vpn-dns-gateway/manual/latex/devopsLaTeXDocker.tex
T
vsun1 828f6c555b
Automated Manual Build / manual (push) Successful in 1m27s
Add automated LaTeX manual build (refs #12)
2026-10-07 18:56:18 -04:00

271 lines
9.6 KiB
TeX

\chapter{LaTeX Docker}
\ChapterLabel{LaTeXDocker}
\index{LaTeX Docker}
\section{Overview}
Overleaf compiles every project with TeX Live inside an isolated container,
so a document builds the same way no matter whose computer it came from. In
this chapter we reproduce that idea: we write a Docker image that contains
TeX Live, mount a folder holding a \texttt{.tex} file into a container, and
let the container compile it to PDF.
Unlike the Color Buttons website in \ChapterRef{DigitalOceanDockerDeployment},
this container is not a long-running service. It starts, runs
\texttt{pdflatex} once, writes the PDF back to the mounted folder, and exits.
It serves nothing on a port, so we built and ran it on our local Windows
machine (Docker Desktop, Docker 29.7.2) rather than on the droplet.
The project lives in the \texttt{latex-docker/} folder of our repository and
contains two files: a \texttt{Dockerfile} and a sample document,
\texttt{sample.tex}.
\section{The Dockerfile}
\index{Dockerfile}
We start from the slim Debian image and install TeX Live from Debian's
package repository. \texttt{texlive-latex-base} provides \texttt{pdflatex}
and the core LaTeX classes; \texttt{texlive-latex-recommended} adds common
packages such as \texttt{geometry}. Skipping recommended packages and
deleting the apt package lists keeps the image smaller.
\begin{minted}{docker}
# TeX Live compiler image: compiles a mounted .tex file to PDF, then exits.
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
texlive-latex-base texlive-latex-recommended \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /work
ENTRYPOINT ["pdflatex", "-interaction=nonstopmode", "-halt-on-error"]
CMD ["sample.tex"]
\end{minted}
The \texttt{ENTRYPOINT} fixes the command the container always runs, while
\texttt{CMD} supplies the default file name. Because \texttt{CMD} is only a
default, a different file in the mounted folder can be compiled by adding its
name to the end of \texttt{docker run}.
\texttt{-interaction=nonstopmode} and \texttt{-halt-on-error} stop LaTeX from
waiting for keyboard input, which a container does not have, and make a
broken document fail instead of hanging.
\section{The Sample Document}
The sample document exercises a title block, sections, a list, inline and
display mathematics, and a table:
\begin{minted}[breaklines]{latex}
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\title{Hello from a Docker Container}
\author{SSW 590 Group 1}
\date{\today}
\begin{document}
\maketitle
\section{Purpose}
This document was compiled by \texttt{pdflatex} from TeX Live running inside
a Docker container, the same idea Overleaf uses to compile projects in
isolated environments.
\section{Sample Content}
\begin{itemize}
\item Lists, sections, and a title block.
\item Mathematics: $e^{i\pi} + 1 = 0$ and
\begin{equation}
\sum_{k=1}^{n} k = \frac{n(n+1)}{2}.
\end{equation}
\item A table:
\end{itemize}
\begin{center}
\begin{tabular}{|l|l|}
\hline
Tool & Role \\ \hline
Docker & Container runtime \\
TeX Live & LaTeX distribution \\
pdflatex & Compiler \\ \hline
\end{tabular}
\end{center}
\end{document}
\end{minted}
\section{Building the Image}
\index{docker build}
From PowerShell in the \texttt{latex-docker} folder, we built the image and
tagged it \texttt{latex-docker}. The \texttt{-{}-no-cache} flag forces Docker
to re-run every build step instead of reusing layers from an earlier build:
\begin{minted}{powershell}
docker build --no-cache -t latex-docker .
\end{minted}
The Debian base image had already been downloaded, so its layer shows as
\texttt{CACHED}; the \texttt{apt-get} step that installs TeX Live ran again.
Excerpts from the build output (\texttt{...} marks omitted lines):
\begin{minted}[breaklines, breakanywhere]{text}
[+] Building 28.5s (8/8) FINISHED
...
=> CACHED [1/3] FROM docker.io/library/debian:bookworm-slim@sha256:3783cc01769c7b2b1b83a5c5ad96c815348e28ed7da68e2e3687004faa906251
...
=> [2/3] RUN apt-get update && apt-get install -y --no-install-recommends texlive-latex-base texlive-latex-recommended && rm -rf /var/lib/apt/lists/*
=> [3/3] WORKDIR /work
...
=> => naming to docker.io/library/latex-docker:latest
=> => unpacking to docker.io/library/latex-docker:latest
\end{minted}
\begin{figure}[H]
\centering
\includegraphics[width=\textwidth]{images/latex-docker-build.png}
\caption{Building the \texttt{latex-docker} image}
\end{figure}
We then confirmed the image exists and checked its size:
\begin{minted}[breaklines]{powershell}
docker image ls latex-docker --format "{{.Repository}} {{.Tag}} {{.ID}} {{.Size}}"
\end{minted}
\begin{minted}{text}
latex-docker latest 3d3bac935529 517MB
\end{minted}
The image ID matches the manifest list the build reported
(\texttt{sha256:3d3bac935529...}). At 517\,MB the image is far smaller than a
full TeX Live installation (several gigabytes), because it contains only the
packages this kind of document needs.
\section{Compiling the Document}
\index{docker run}
We ran the container with the current folder mounted as \texttt{/work}, the
image's working directory:
\begin{minted}{powershell}
docker run --rm -v "${PWD}:/work" latex-docker
\end{minted}
\begin{itemize}
\item \texttt{-v "\$\{PWD\}:/work"} mounts the PowerShell current directory
into the container, so \texttt{pdflatex} can read \texttt{sample.tex} and
its output lands back on our machine.
\item \texttt{-{}-rm} deletes the container once it exits, since a finished
compile leaves nothing worth keeping inside it.
\end{itemize}
Excerpts from the compiler output:
\begin{minted}[breaklines]{text}
(./sample.tex
LaTeX2e <2022-11-01> patch level 1
...
kpathsea: Running mktexpk --mfmode / --bdpi 600 --mag 1+0/600 --dpi 600 tcrm1000
...
This is METAFONT, Version 2.71828182 (TeX Live 2022/Debian) (preloaded base=mf)
...
Output written on sample.pdf (1 page, 97381 bytes).
Transcript written on sample.log.
\end{minted}
The \texttt{mktexpk} and METAFONT lines show TeX generating a font
(\texttt{tcrm1000}) on the fly. Because \texttt{-{}-rm} discards the
container, this font is regenerated on every run; installing the
\texttt{cm-super} font package in the image would avoid that at the cost of a
larger image.
\begin{figure}[H]
\centering
\includegraphics[width=0.85\textwidth]{images/latex-docker-run.png}
\caption{Compiling \texttt{sample.tex} in the container (font-generation
output trimmed)}
\end{figure}
After the run, the folder held the new PDF alongside the source:
\begin{minted}{powershell}
Get-ChildItem | Select-Object Name, Length
\end{minted}
\begin{minted}{text}
Name Length
---- ------
Dockerfile 370
sample.aux 229
sample.log 7249
sample.pdf 97381
sample.tex 824
\end{minted}
\begin{figure}[H]
\centering
\includegraphics[width=0.8\textwidth]{images/latex-docker-listing.png}
\caption{The compiled \texttt{sample.pdf} in the \texttt{latex-docker}
folder}
\end{figure}
\begin{figure}[H]
\centering
\fbox{\includegraphics[width=0.8\textwidth]{images/latex-docker-sample.png}}
\caption{The top of \texttt{sample.pdf}, compiled inside the
\texttt{latex-docker} container}
\end{figure}
\section{Verification on a Second Windows Device}
The same \texttt{Dockerfile} and \texttt{sample.tex} were also tested on a
second group member's Windows device. A PowerShell helper script,
\texttt{latex-docker/run-latex-docker-evidence.ps1}, ran the necessary
\texttt{docker build} and \texttt{docker run} commands in one go. It saved the
full command output as \texttt{docker-build.log} and \texttt{docker-run.log},
then printed a short summary suitable for a terminal screenshot. The script
checked that the run created a fresh \texttt{sample.pdf}; the screenshot shows
a one-page PDF of 97,204 bytes. Image IDs and PDF byte counts can differ
between separate builds.
\begin{minted}{powershell}
powershell -NoProfile -ExecutionPolicy Bypass -File ".\run-latex-docker-evidence.ps1"
\end{minted}
\begin{figure}[H]
\centering
\includegraphics[width=\textwidth,trim=0 0 0 63,clip]{images/latex-docker-build-test-different-device.png}
\caption{Second-device script output confirming the image build and
container compilation; the local account path is cropped from the view}
\end{figure}
The folder listing shows the generated PDF beside the source files and the
two saved command logs. Opening the PDF provides a separate visual check of
the output.
\begin{figure}[H]
\centering
\includegraphics[width=0.75\textwidth]{images/latex-docker-build-test-different-device-list.png}
\caption{Second-device folder listing with \texttt{sample.pdf} and build
and run logs}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[width=0.6\textwidth,trim=457 300 467 150,clip]{images/latex-docker-build-test-different-device-result.png}
\caption{Second-device \texttt{sample.pdf} opened after the container run;
browser controls and local account details are cropped from the view}
\end{figure}
\section{Comparison with Overleaf}
\begin{itemize}
\item \textbf{Same idea:} both compile the document inside a container
with a fixed TeX Live version, so the result does not depend on what is
installed on the author's computer.
\item \textbf{Version control:} our TeX Live version is pinned by the
Debian release in the \texttt{FROM} line (TeX Live 2022 in Debian 12),
and the \texttt{Dockerfile} is committed to our repository.
\item \textbf{Differences:} Overleaf adds an editor, collaboration, and a
full TeX Live installation. Our container is a single-purpose build step
that could run in a CI pipeline to produce the PDF automatically on every
commit.
\end{itemize}