Quarto in Docker Container

09/07/2024, Sat
Categories: #Python
Tags: #quarto

Reproducible Quarto Environment

Quarto a tool to help with technical and scientific publishing which incorporates programming languages and typesetting support in all-in-one solution. Since quarto relies on python and many other dependencies, it is safer to run Quarto in a docker container to avoid significant changes on your host machine. In addition, it will help with a repeat set up when you need to bring up the same environment again.

Create the Dockerfile below to install Quarto inside Ubuntu

# Use Ubuntu version 24.04 as the base
FROM ubuntu:24.04

# Set working directory
WORKDIR /app

# Install dependencies
RUN apt-get update && apt-get install -y \
    curl \
    r-base r-base-dev \
    python3 python3-pip \
    && rm -rf /var/lib/apt/lists/*

# Remove the EXTERNALLY-MANAGED tag issued by Ubuntu
RUN rm /usr/lib/python*/EXTERNALLY-MANAGED

# Install Jupyter, Rmarkdown and knitr dependencies of Quarto
RUN pip3 install jupyter
RUN R -e "install.packages('knitr', repos='https://cran.rstudio.com/')"
RUN R -e "install.packages('rmarkdown', repos='https://cran.rstudio.com/')"

# Specify the Quarto version using an ARG
ARG QUARTO_VERSION=1.6.8

# Download and install Quarto
RUN curl -L -O https://github.com/quarto-dev/quarto-cli/releases/download/v${QUARTO_VERSION}/quarto-${QUARTO_VERSION}-linux-amd64.deb \
&& dpkg -i quarto-${QUARTO_VERSION}-linux-amd64.deb

# Expose port 8080 for Quarto preview server
EXPOSE 8080

Create a 'project' folder on the same level as the Dockerfile. Within the 'project' folder, create a 'my-document.qmd' with the contents below.

---
title: "A Sample Quarto Document"
author: "Name"
date: "September 5, 2024"

---

# Introduction

This is some sample text.

The above provides all the files required for a minimal docker Quarto environment.

Then you will need to build the image to create the container.

docker build -t my-quarto-image .

Map your host's 'project' folder to the 'app' folder in the container when running the container. The --rm will also remove the container after exiting.

docker run -it --rm -p 8080:8080 -v ./project:/app my-quarto-image
# quarto preview filename.qmd --port <your-desired-port> --host 0.0.0.0
quarto preview my-document.qmd --port 8080 --host 0.0.0.0

You should now be able to visit localhost:8080 on your host machine to view the contents of the qmd document. Live reload is also enable, and changes you make to your document will automatically reload inside your browser.