在服务器上用docker部署sharelatex

在服务器上用docker部署sharelatex,搭建共享latex写作平台。 Modified: 2024-09-28 23:03:28 Created: 2023-10-18 14:06:38 Tags: #latex #sharelatex #docker #docker-compse

Overleaf是一个在线的latex协作编辑平台。这里介绍Sharelatex--一个和overleaf有相同功能的工具,在服务器上用docker部署的方式。

1. 重新打包sharelatex的docker镜像

sharelatex现在已经改名叫做overleaf,官方推荐用overleaf toolkit来安装。overleaf toolkit要修改linux环境,比较繁琐。这里用docker来部署,降低对原来linux环境的影响。

参考官方给出的docker-compose.yaml文件

但是直接docker-compose,并不能排版中文,主要原因是缺少xelatex相关包。由于是在docker容器中,操作并不方便。这里直接以原始sharelatex的docker镜像为基础生成新的docker镜像。

在新的docker镜像中,将会安装texlive的更新,并且将xelatex相关包添加到环境变量中去。对应的Dockerfile如下:

Dockerfile
FROM sharelatex/sharelatex
RUN tlmgr option repository https://mirrors.tuna.tsinghua.edu.cn/CTAN/systems/texlive/tlnet
RUN tlmgr update --self --all
RUN tlmgr install scheme-full
WORKDIR /
ENTRYPOINT ["/sbin/my_init"]

在保存Dockerfile的文件夹下,生成新的docker镜像:

docker build --tag chinglin/sharelatex:latest --progress=plain . 2>&1 | tee build.log

2. 解决mongodb问题

需要在mongodb下面再加一个镜像,初始化mongodb

mongoinit:
  restart: "no"
  image: mongo:4.4
  depends_on:
    mongo:
      condition: service_healthy
  entrypoint:
    [
      "mongo",
      "--host",
      "mongo:27017",
      "--eval",
      'rs.initiate({ _id: "overleaf", members: [ { _id: 0, host: "mongo:27017" } ] })',
    ]

3. docker-compose 运行

参考官方给出的docker-compose.yaml文件,加上我们上面的魔改:

docker-compose.yaml
version: '2.2'
services:
    sharelatex:
        restart: always
        # Server Pro users:
        # image: quay.io/sharelatex/sharelatex-pro
        image: chinglin/sharelatex
        container_name: sharelatex
        depends_on:
            mongo:
                condition: service_healthy
            redis:
                condition: service_started
        ports:
            - 8089:80
        links:
            - mongo
            - redis
        stop_grace_period: 60s
        volumes:
            - ./data/sharelatex_data:/var/lib/sharelatex
            # - ./data/sharelatex_texlive:/texlive
            ########################################################################
            ####  Server Pro: Uncomment the following line to mount the docker  ####
            ####             socket, required for Sibling Containers to work    ####
            ########################################################################
            # - /var/run/docker.sock:/var/run/docker.sock
        environment:

            SHARELATEX_APP_NAME: Overleaf Community Edition
            SHARELATEX_MONGO_URL: mongodb://mongo/sharelatex

            # Same property, unfortunately with different names in
            # different locations
            SHARELATEX_REDIS_HOST: redis
            REDIS_HOST: redis

            ENABLED_LINKED_FILE_TYPES: 'project_file,project_output_file'

            # Enables Thumbnail generation using ImageMagick
            ENABLE_CONVERSIONS: 'true'

            # Disables email confirmation requirement
            EMAIL_CONFIRMATION_DISABLED: 'true'

            # temporary fix for LuaLaTex compiles
            # see https://github.com/overleaf/overleaf/issues/695
            TEXMFVAR: /var/lib/sharelatex/tmp/texmf-var

            ## Set for SSL via nginx-proxy
            #VIRTUAL_HOST: 103.112.212.22

            # SHARELATEX_SITE_URL: http://sharelatex.mydomain.com
            # SHARELATEX_NAV_TITLE: Our ShareLaTeX Instance
            # SHARELATEX_HEADER_IMAGE_URL: http://somewhere.com/mylogo.png
            # SHARELATEX_ADMIN_EMAIL: [email protected]

            # SHARELATEX_LEFT_FOOTER: '[{"text": "Powered by <a href=\"https://www.sharelatex.com\">ShareLaTeX</a> 2016"},{"text": "Another page I want to link to can be found <a href=\"here\">here</a>"} ]'
            # SHARELATEX_RIGHT_FOOTER: '[{"text": "Hello I am on the Right"} ]'

            # SHARELATEX_EMAIL_FROM_ADDRESS: "[email protected]"

            # SHARELATEX_EMAIL_AWS_SES_ACCESS_KEY_ID:
            # SHARELATEX_EMAIL_AWS_SES_SECRET_KEY:

            # SHARELATEX_EMAIL_SMTP_HOST: smtp.mydomain.com
            # SHARELATEX_EMAIL_SMTP_PORT: 587
            # SHARELATEX_EMAIL_SMTP_SECURE: false
            # SHARELATEX_EMAIL_SMTP_USER:
            # SHARELATEX_EMAIL_SMTP_PASS:
            # SHARELATEX_EMAIL_SMTP_TLS_REJECT_UNAUTH: true
            # SHARELATEX_EMAIL_SMTP_IGNORE_TLS: false
            # SHARELATEX_EMAIL_SMTP_NAME: '127.0.0.1'
            # SHARELATEX_EMAIL_SMTP_LOGGER: true
            # SHARELATEX_CUSTOM_EMAIL_FOOTER: "This system is run by department x"

            # ENABLE_CRON_RESOURCE_DELETION: true

            ################
            ## Server Pro ##
            ################

            # SANDBOXED_COMPILES: 'true'

            # SANDBOXED_COMPILES_SIBLING_CONTAINERS: 'true'
            # SANDBOXED_COMPILES_HOST_DIR: '/var/sharelatex_data/data/compiles'

            # DOCKER_RUNNER: 'false'

            # Works with test LDAP server shown at bottom of docker compose
            # SHARELATEX_LDAP_URL: 'ldap://ldap:389'
            # SHARELATEX_LDAP_SEARCH_BASE: 'ou=people,dc=planetexpress,dc=com'
            # SHARELATEX_LDAP_SEARCH_FILTER: '(uid={{username}})'
            # SHARELATEX_LDAP_BIND_DN: 'cn=admin,dc=planetexpress,dc=com'
            # SHARELATEX_LDAP_BIND_CREDENTIALS: 'GoodNewsEveryone'
            # SHARELATEX_LDAP_EMAIL_ATT: 'mail'
            # SHARELATEX_LDAP_NAME_ATT: 'cn'
            # SHARELATEX_LDAP_LAST_NAME_ATT: 'sn'
            # SHARELATEX_LDAP_UPDATE_USER_DETAILS_ON_LOGIN: 'true'
            # SHARELATEX_TEMPLATES_USER_ID: "578773160210479700917ee5"
            # SHARELATEX_NEW_PROJECT_TEMPLATE_LINKS: '[ {"name":"All Templates","url":"/templates/all"}]'
            # SHARELATEX_PROXY_LEARN: "true"

    mongo:
        restart: always
        image: mongo:4.4
        container_name: mongo
        command: "--replSet overleaf"
        expose:
            - 27017
        volumes:
            - ./data/mongo_data:/data/db
        healthcheck:
            test: echo 'db.stats().ok' | mongo localhost:27017/test --quiet
            interval: 10s
            timeout: 10s
            retries: 5

    mongoinit:
        restart: "no"
        image: mongo:4.4
        depends_on:
          mongo:
            condition: service_healthy
        entrypoint:
          [
            "mongo",
            "--host",
            "mongo:27017",
            "--eval",
            'rs.initiate({ _id: "overleaf", members: [ { _id: 0, host: "mongo:27017" } ] })',
          ]

    redis:
        restart: always
        image: redis:6.2
        container_name: redis
        expose:
            - 6379
        volumes:
            - ./data/redis_data:/data

    # ldap:
    #    restart: always
    #    image: rroemhild/test-openldap
    #    container_name: ldap
    #    expose:
    #        - 389

    # See https://github.com/jwilder/nginx-proxy for documentation on how to configure the nginx-proxy container,
    # and https://github.com/overleaf/overleaf/wiki/HTTPS-reverse-proxy-using-Nginx for an example of some recommended
    # settings. We recommend using a properly managed nginx instance outside of the Overleaf Server Pro setup,
    # but the example here can be used if you'd prefer to run everything with docker-compose

    # nginx-proxy:
    #     image: jwilder/nginx-proxy
    #     container_name: nginx-proxy
    #     ports:
    #       #- "80:80"
    #       - "443:443"
    #     volumes:
    #       - /var/run/docker.sock:/tmp/docker.sock:ro
    #       - /home/sharelatex/tmp:/etc/nginx/certs

运行docker-compose:

docker-compose up -d

为了能够让搭建好的服务在其它机器上能够访问,还需要开启服务器上对应端口,这里是8090端口。

4. 添加用户

前面已经通过docker-compose启动了sharelatex,此时还需要进入docker容器添加账户,需要在服务器上运行:

docker exec -i -t sharelatex /bin/bash
grunt user:create-admin --email [email protected]

此时出现一个网址。请把网址的localhost部分换成服务器的网址,在浏览器中输入账户和密码则可以登陆。此时就可以进行愉快的写作了。

5. 使用感受

好处:

  1. 开箱即用,无需每台电脑上都安装配置texlive。
  2. 可以共享,便于协作。
  3. 数据部署在自己服务器中(表面上看起来会更加安全)。

缺点:

  1. 网页前端的编辑器功能太naive,毕竟不能做一个好的编辑器。
  2. 还是编辑器前端的问题,似乎没有办法利用目前大语言模型自动生成代码的功能。

5. 总结

本文介绍了在服务器上搭建sharelatex的方式,通过重新打包的方式解决了sharelatex无法中文编译的问题,通过修改官方docker-compose.yaml文件修复了mongodb的问题。