129 lines
4.7 KiB
TeX
129 lines
4.7 KiB
TeX
\chapter{Latex Docker}
|
|
\ChapterLabel{Latex Docker}
|
|
|
|
|
|
\section{Goal}
|
|
Overleaf compiles \texttt{.tex} source into a PDF by running a LaTeX
|
|
engine (TeX Live) on its own servers -- the user never installs LaTeX
|
|
locally. This chapter reproduces that same idea on a small scale: we
|
|
package TeX Live inside a Docker container and use it to compile a
|
|
simple LaTeX document, entirely independent of whatever is or isn't
|
|
installed on the host machine.
|
|
|
|
\section{Project Files}
|
|
Two files are needed: the LaTeX source to compile, and the
|
|
\texttt{Dockerfile} that builds the container.
|
|
|
|
\subsection{The LaTeX Source (\texttt{hello.tex})}
|
|
\begin{minted}{latex}
|
|
\documentclass{article}
|
|
\title{Hello from a Container}
|
|
\author{DevOps Team}
|
|
\date{\today}
|
|
|
|
\begin{document}
|
|
\maketitle
|
|
|
|
This tiny document was compiled entirely inside a Docker container running
|
|
TeX Live, with no LaTeX installation on the host machine at all.
|
|
|
|
\section{Why this matters}
|
|
Overleaf itself works the same way behind the scenes: your .tex
|
|
source is sent to a server that has TeX Live installed, compiled there, and
|
|
the resulting PDF is sent back to your browser. This project reproduces
|
|
that same idea with a container we control ourselves.
|
|
|
|
\end{document}
|
|
\end{minted}
|
|
|
|
\subsection{The Dockerfile}
|
|
\begin{minted}{docker}
|
|
# Base image: official TeX Live distribution.
|
|
# This is the same LaTeX engine Overleaf runs on its own servers.
|
|
FROM texlive/texlive:latest
|
|
|
|
# Working directory inside the container.
|
|
WORKDIR /doc
|
|
|
|
# Copy our LaTeX source file into the container.
|
|
COPY hello.tex .
|
|
|
|
# Compile the document to PDF when the container runs.
|
|
# -interaction=nonstopmode: don't pause on warnings/errors, just log them
|
|
CMD ["pdflatex", "-interaction=nonstopmode", "hello.tex"]
|
|
\end{minted}
|
|
|
|
\texttt{texlive/texlive} is the official, full TeX Live image -- it is
|
|
large (several GB) but guarantees every package and font Overleaf itself
|
|
would have available, which matches the goal of this chapter (reproducing
|
|
what Overleaf does) more closely than a stripped-down image would.
|
|
|
|
\section{Building the Image}
|
|
From the directory containing both files:
|
|
\begin{minted}{bash}
|
|
$ docker build -t latex-docker .
|
|
\end{minted}
|
|
\begin{minted}{text}
|
|
[+] Building 42.1s (8/8) FINISHED
|
|
=> [1/3] FROM docker.io/texlive/texlive:latest
|
|
=> [2/3] WORKDIR /doc
|
|
=> [3/3] COPY hello.tex .
|
|
=> exporting to image
|
|
=> => naming to docker.io/library/latex-docker
|
|
\end{minted}
|
|
Docker downloads the TeX Live base image (first build only -- cached on
|
|
subsequent builds), then layers our working directory and source file on
|
|
top, and tags the result as \texttt{latex-docker}.
|
|
|
|
\section{Running the Container and Retrieving the PDF}
|
|
The container compiles the PDF \emph{inside} its own filesystem. To get
|
|
the resulting PDF back onto the host machine, we mount our project
|
|
directory -- the same one containing \texttt{hello.tex} -- into the
|
|
container at the path the \texttt{Dockerfile} uses as its working
|
|
directory:
|
|
\begin{minted}{bash}
|
|
$ docker run --rm -v "$(pwd):/doc" latex-docker
|
|
\end{minted}
|
|
\begin{minted}{text}
|
|
This is pdfTeX, Version 3.141592653-2.6-1.40.25 (TeX Live 2023)
|
|
entering extended mode
|
|
(./hello.tex
|
|
LaTeX2e <2023-11-01>
|
|
...
|
|
Output written on hello.pdf (1 page, 59343 bytes).
|
|
Transcript written on hello.log.
|
|
\end{minted}
|
|
\texttt{--rm} removes the container after it exits (we don't need to keep
|
|
it around once the PDF exists); \texttt{-v "\$(pwd):/doc"} mounts our
|
|
current host directory to \texttt{/doc} inside the container, which is the
|
|
same path the \texttt{Dockerfile} set as its working directory.
|
|
|
|
|
|
Because the mounted directory is the same one on the host, the compiled
|
|
files appear right there once the container exits:
|
|
\begin{minted}{bash}
|
|
$ ls
|
|
\end{minted}
|
|
\begin{minted}{text}
|
|
Dockerfile hello.aux hello.log hello.pdf hello.tex
|
|
\end{minted}
|
|
|
|
\section{Verifying the Output}
|
|
Opening \texttt{output/hello.pdf} confirms a one-page PDF containing the
|
|
title, author, date, and body text exactly as written in the source --
|
|
compiled with zero LaTeX installed on the host, using only the container.
|
|
|
|
\section{Relation to Overleaf}
|
|
\begin{itemize}
|
|
\item Overleaf's compile servers are, conceptually, exactly this: a
|
|
TeX Live environment that takes \texttt{.tex} source in and returns
|
|
a PDF, with the user never touching the underlying LaTeX
|
|
installation directly.
|
|
\item The \texttt{FROM texlive/texlive:latest} base image in our
|
|
\texttt{Dockerfile} is functionally the same TeX Live distribution
|
|
Overleaf itself is built on.
|
|
\item Packaging this as a container means the exact same compile
|
|
environment can be reproduced on any machine with Docker installed
|
|
-- the core value proposition of containerization applied to a
|
|
document-compilation tool instead of a web application.
|
|
\end{itemize} |