Table of Contents

About

These are some notes on running Overleaf (a self-hosted latex on-line collaborative tool) using docker containers.

The Minefield

Here is a list of problems that have to be tackled in order to get everything working:

The Images

This section stores the docker compose files to create the images necessary for running Overleaf on docker.

Mongo

ersion: '3.9'

services:
    mongo:
      image: mongo:4.4.6
      healthcheck:
        test: echo 'db.stats().ok' | mongo localhost:27017/test --quiet
        interval: 10s
        timeout: 10s
        retries: 5
      user: 1000:1000
      volumes:
        - /mnt/docker/data/mongo/db:/data/db
        - /mnt/docker/data/mongo/configdb:/data/configdb
        - /mnt/docker/data/mongo/init:/docker-entrypoint-initdb.d/:ro
      ports:
        - 27017:27017
      environment:
        - PUID=1000
        - PGID=1000
        - TZ=Etc/UTC
        - MONGO_INITDB_ROOT_USERNAME=root
        - MONGO_INITDB_ROOT_PASSWORD=password

The host /mnt/docker/data/mongo/init folder that is mapped inside the docker container as read-only at /docker-entrypoint-initdb.d/ contains a file name init.js with the following contents:

db = db.getSiblingDB('admin');
// move to the admin db - always created in Mongo
db.auth("root", "password");
// log as root admin if you decided to authenticate in your docker-compose file...
db = db.getSiblingDB('overleaf');
// create and move to your new database
db.createUser({
'user': "overleaf",
'pwd': "overleaf",
'roles': [{
    'role': 'dbOwner',
    'db': 'overleaf'}]});
// user created
//db.createCollection('collection_test');
// add new collection

that is placed there in order to pre-create the overleaf database referenced in the monogdb connector within the overleaf docker compose file.

Redis

version: '3.9'

services:
    redis:
      image: library/redis:latest
      deploy:
        resources:
          limits:
            memory: 256M
            cpus: '0.75'
          reservations:
            memory: 50M
      healthcheck:
        test: ["CMD-SHELL", "redis-cli ping || exit 1"]
      user: 1000:1000
      ports:
        - 6379:6379
      volumes:
        - /mnt/docker/data/redis/data:/data
        - /mnt/docker/data/redis/config:/usr/local/etc/redis
      environment:
        - PUID=1000
        - PGID=1000
        - TZ=Etc/UTC

Overleaf

After mongo and redis have been installed, setting Overleaf up is little more complicated given that the container itself has a complex setup. The first step is to run the container as per the following docker compose definition whilst changing the necessary environment variables to match.

# after starting, run inside the container: 
#   # in order to set the email to an admin
#   cd /var/www/sharelatex; grunt user:create-admin --email=yourownemail
#   # in order to install the full TeXLive distribution
#   tlmgr option repository https://ftp.math.utah.edu/pub/tex/historic/systems/texlive/2022/tlnet-final
#   tlmgr install scheme-full
version: '3.9'

services:
    overleaf:
      image: sharelatex/sharelatex:3.5.13
      healthcheck:
        test: curl -f http://localhost:80/ || exit 1
      ports:
        - 7643:80
      stop_grace_period: 60s
      volumes:
        - /mnt/docker/data/overleaf:/var/lib/sharelatex
      environment:
        - SHARELATEX_FPH_DISPLAY_NEW_PROJECTS=false
        - SHARELATEX_FPH_INITIALIZE_NEW_PROJECTS=false
        - SHARELATEX_SECURE_COOKIE=false
        - SHARELATEX_BEHIND_PROXY=true
        - SHARELATEX_APP_NAME="Overleaf Community Edition"
        - SHARELATEX_MONGO_URL=mongodb://overleaf:overleaf@docker/overleaf
        - SHARELATEX_REDIS_HOST=docker
        - REDIS_HOST=docker
        - ENABLE_CONVERSIONS=true
        - EMAIL_CONFIRMATION_DISABLED=false
        - SHARELATEX_ADMIN_EMAIL=your@email.com
        - TEXMFVAR=/var/lib/sharelatex/tmp/texmf-var
        - SHARELATEX_SITE_URL=https://latex...
        - SHARELATEX_NAV_TITLE=OverleafCE
        - SHARELATEX_EMAIL_SMTP_HOST=smtp.gmail.com
        - SHARELATEX_EMAIL_SMTP_PORT=587
        - SHARELATEX_EMAIL_SMTP_USER=your@email.com
        - SHARELATEX_EMAIL_SMTP_PASS=dfelecwkuldjffxfehd
        - SHARELATEX_EMAIL_SMTP_TLS_REJECT_UNAUTH=true
        - SHARELATEX_EMAIL_SMTP_IGNORE_TLS=false
        - SHARELATEX_EMAIL_FROM_ADDRESS=your@email.com
        - SHARELATEX_EMAIL_SMTP_LOGGER=true
        - TZ=Etc/UTC

Note that Overleaf has to be able to set ownership and permissions for the local folder /mnt/docker/data/overleaf that is mounted within the container at /var/lib/sharelatex which might be a problem depending on the setup. For example, if /mnt/docker/data/overleaf is to be found on an NFS share, then the NFS share should be defined using no_root_squash in order to make sure that the chown command will work. Here is how one would export /mnt/docker/data/overleaf using NFS:

/mnt/docker/data/overleaf     docker1(rw,sync,insecure,no_subtree_check,no_root_squash)

The typical Overleaf showstopper consists in permission errors. Fortunately, the Overleaf docker container can be monitored by issuing:

docker logs OVERLEAF_CONTAINER --follow

where:

Similarly, one very useful source of information is to start a shell within the docker container:

docker exec -it OVERLEAF_CONTAINER bash

where:

and then look under /var/log/sharelatex, specifically, inside the files:

In case Overleaf manages to start and you are able to connect to the web-interface, then the E-Mail that has been used to create an account has to be set as an administrator. This can be done by issuing the command:

cd /var/www/sharelatex; grunt user:create-admin --email=yourownemail

where:

Now, the next part involves installing the full LaTeX TeXLive distribution, similarly, by entering the container shell and issuing the commands:

tlmgr option repository https://ftp.math.utah.edu/pub/tex/historic/systems/texlive/2022/tlnet-final
tlmgr install scheme-full

where:

Finally, the image is now complete and can be used such that it is best to create a separate image with the changes committed.

First, list all containers:

docker container ls

then commit the container to a separate image:

docker commit CONTAINER_ID IMAGE_NAME

where:

From now on, the Overleaf compose file can be changed, and instead of:

      image: sharelatex/sharelatex:3.5.13

the new image can be used:

      image: overleaf:latexfull

Although it is out of the scope of this tutorial, in case that Overleaf will be running in a swarm, it is best to deploy a private registry to save the modified image such that it can be retrieved by the swarm.

Generating Google Application Passwords

For the environment section within the Overleaf docker compose file, namely, the definitions that define and outbound E-Mail:

        - SHARELATEX_EMAIL_SMTP_HOST=smtp.gmail.com
        - SHARELATEX_EMAIL_SMTP_PORT=587
        - SHARELATEX_EMAIL_SMTP_USER=your@email.com
        - SHARELATEX_EMAIL_SMTP_PASS=dfelecwkuldjffxfehd
        - SHARELATEX_EMAIL_SMTP_TLS_REJECT_UNAUTH=true
        - SHARELATEX_EMAIL_SMTP_IGNORE_TLS=false
        - SHARELATEX_EMAIL_FROM_ADDRESS=your@email.com
        - SHARELATEX_EMAIL_SMTP_LOGGER=true

use Google via SMTP. For this to work, the "Security" section of the Google account has to be visited, two-factor authentication (2FA) must be enabled and then by clinking on the "Two-factor Authentication" and scrolling down, a section should be provided to create an application password.

The application password created should be set as the value of the SHARELATEX_EMAIL_SMTP_PASS environment variable:

        - SHARELATEX_EMAIL_SMTP_PASS=dfelecwkuldjffxfehd