anthe.sevenants

Using 'quarto preview' in VS Code with Quarto running in Docker

2023-11-23

What's the problem?

I have recently started running R, RStudio and Quarto from Docker containers. Easy deployment, but a bit of a pain to fit into my current workflow where I write a paper as a Quarto document in VS Code and preview its output using Quarto. Quarto expects to be installed on your local machine, so you can only preview quarto preview article.qmd from within the Docker container. This is a use case the VS Code extension does not expect and therefore does not support. Boo!

The solution

I have created a forwarder for Quarto and Docker, for the following, rather niche use case:

  • you use VS Code on your local machine
  • you use the official Quarto extension for VS Code
  • you would like to have Quarto and R (or Python, for what it's worth) installed inside a Docker container
  • you want to preview your Quarto document using the Quarto installation inside the Docker container

The forwarder pretends to be the Quarto binary, but instead forwards the call to Quarto to the Quarto binary inside the Docker container. I wish it worked automagically and all that, but I only have so much time so some manual set-up is required.

How to use

  1. Download the quarto script from this GitHub repository
  2. Change DOCKER_CONTAINER_NAME to the name of the container where Quarto is installed
    Change QMD_DOCKER_PATH to the path of your Quarto document inside the container
  3. Set execution rights to your quarto script: chmod +x quarto
  4. Make sure to forward port 8788 inside your docker-compose file (or however you are running your Docker container)
  5. You can now press CTRL ^ SHIFT ^ K and have your quarto preview request be forwarded to your Docker container.

The script I made has comments which explain what each command does. Hope this helps! May your articles be accepted :-).