Dockerfile 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. FROM amd64/golang:1.13 AS build
  2. ARG TAG=0.0.1
  3. # 编译-环境变量
  4. ENV GO111MODULE=on
  5. ENV GOPROXY=https://goproxy.cn,direct
  6. ENV CGO_ENABLED=1
  7. ENV GOARCH=amd64
  8. ENV GOOS=linux
  9. # 工作目录
  10. ADD . /go/src/github.com/mindoc-org/mindoc
  11. WORKDIR /go/src/github.com/mindoc-org/mindoc
  12. # 编译
  13. RUN go env
  14. RUN go mod tidy -v
  15. RUN go build -o mindoc_linux_amd64 -ldflags "-w -s -X 'main.VERSION=$TAG' -X 'main.BUILD_TIME=`date`' -X 'main.GO_VERSION=`go version`'"
  16. RUN cp conf/app.conf.example conf/app.conf
  17. # 清理不需要的文件
  18. RUN rm appveyor.yml docker-compose.yml Dockerfile .travis.yml .gitattributes .gitignore go.mod go.sum main.go README.md simsun.ttc start.sh conf/*.go
  19. RUN rm -rf cache commands controllers converter .git .github graphics mail models routers utils
  20. # 测试编译的mindoc是否ok
  21. RUN ./mindoc_linux_amd64 version
  22. # 必要的文件复制
  23. ADD simsun.ttc /usr/share/fonts/win/
  24. ADD start.sh /go/src/github.com/mindoc-org/mindoc
  25. # Ubuntu 20.04
  26. FROM ubuntu:focal
  27. # 切换默认shell为bash
  28. SHELL ["/bin/bash", "-c"]
  29. COPY --from=build /usr/share/fonts/win/simsun.ttc /usr/share/fonts/win/
  30. COPY --from=build /go/src/github.com/mindoc-org/mindoc /mindoc
  31. WORKDIR /mindoc
  32. RUN chmod a+r /usr/share/fonts/win/simsun.ttc
  33. # 备份原有源
  34. RUN mv /etc/apt/sources.list /etc/apt/sources.list-backup
  35. # 最小化源,缩短apt update时间(ca-certificates必须先安装才支持换tsinghua源)
  36. RUN echo 'deb http://archive.ubuntu.com/ubuntu/ focal main restricted' > /etc/apt/sources.list
  37. RUN apt-get update
  38. RUN apt install -y ca-certificates
  39. # 更换tsinghua源(echo多行内容不能以#开头,会被docker误判为注释行,所以采用\n#开头)
  40. RUN echo $'\
  41. \n# from: https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/\n\
  42. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse\
  43. \n# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse\n\
  44. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse\
  45. \n# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse\n\
  46. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse\
  47. \n# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse\n\
  48. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse\
  49. \n# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse\
  50. \n# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse\
  51. \n# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse'\
  52. > /etc/apt/sources.list
  53. # 更新软件包信息
  54. RUN apt-get update
  55. # 安装必要的系统工具
  56. RUN apt install -y apt-transport-https ca-certificates curl wget xz-utils
  57. # 时区设置(如果不设置, calibre依赖的tzdata在安装过程中会要求选择时区)
  58. ENV TZ=Asia/Shanghai
  59. RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
  60. # tzdata的前端类型默认为readline(Shell情况下)或dialog(支持GUI的情况下)
  61. ARG DEBIAN_FRONTEND=noninteractive
  62. # 安装时区信息
  63. RUN apt install -y --no-install-recommends tzdata
  64. # 重新配置tzdata软件包,使得时区设置生效
  65. RUN dpkg-reconfigure --frontend noninteractive tzdata
  66. # 安装 calibre 依赖的包
  67. RUN apt install -y libgl-dev libnss3-dev libxcomposite-dev libxrandr-dev libxi-dev
  68. # 安装文泉驿字体
  69. RUN apt install -y fonts-wqy-microhei fonts-wqy-zenhei
  70. # 安装中文语言包
  71. RUN apt-get install -y locales language-pack-zh-hans language-pack-zh-hans-base
  72. # 设置默认编码
  73. RUN locale-gen "zh_CN.UTF-8"
  74. RUN update-locale LANG=zh_CN.UTF-8
  75. ENV LANG=zh_CN.UTF-8
  76. ENV LANGUAGE=zh_CN:en
  77. ENV LC_ALL=zh_CN.UTF-8
  78. # 安装-calibre
  79. # RUN apt-get install -y calibre # 此种方式安装省事,但会安装很多额外不需要的软件包,导致体积过大
  80. RUN mkdir -p /tmp/calibre-cache
  81. # 获取最新版本号
  82. RUN curl -s http://code.calibre-ebook.com/latest>/tmp/calibre-cache/version
  83. # 下载最新版本
  84. # RUN wget -O /tmp/calibre-cache/calibre-x86_64.txz -c https://download.calibre-ebook.com/`cat /tmp/calibre-cache/version`/calibre-`cat /tmp/calibre-cache/version`-x86_64.txz
  85. RUN wget -O /tmp/calibre-cache/calibre-x86_64.txz -c https://github.com/kovidgoyal/calibre/releases/download/v`cat /tmp/calibre-cache/version`/calibre-`cat /tmp/calibre-cache/version`-x86_64.txz
  86. # 注: 调试阶段,下载alibre-5.22.1-x86_64.txz到本地(使用 python -m http.server),加速构建
  87. # RUN wget -O /tmp/calibre-cache/calibre-x86_64.txz -c http://10.96.8.252:8000/calibre-5.22.1-x86_64.txz
  88. # 解压
  89. RUN mkdir -p /opt/calibre
  90. # RUN tar --extract --file=/tmp/calibre-cache/calibre-x86_64.txz --directory /opt/calibre
  91. RUN tar xJof /tmp/calibre-cache/calibre-x86_64.txz -C /opt/calibre
  92. ENV PATH=$PATH:/opt/calibre
  93. # 设置calibre相关环境变量
  94. ENV QTWEBENGINE_CHROMIUM_FLAGS="--no-sandbox"
  95. ENV QT_QPA_PLATFORM='offscreen'
  96. # 测试 calibre 可正常使用
  97. RUN ebook-convert --version
  98. # 清理calibre缓存
  99. RUN rm -rf /tmp/calibre-cache
  100. # refer: https://docs.docker.com/engine/reference/builder/#volume
  101. # 数据同步目录
  102. VOLUME /mindoc-sync-host
  103. # refer: https://docs.docker.com/engine/reference/builder/#expose
  104. EXPOSE 8181/tcp
  105. # 如果配置文件不存在就复制
  106. RUN cp --no-clobber /mindoc/conf/app.conf.example /mindoc/conf/app.conf
  107. ENV ZONEINFO=/mindoc/lib/time/zoneinfo.zip
  108. RUN chmod +x /mindoc/start.sh
  109. ENTRYPOINT ["/bin/bash", "/mindoc/start.sh"]
  110. # https://docs.docker.com/engine/reference/commandline/build/#options
  111. # docker build --progress plain --rm --build-arg TAG=2.0.1 --tag gsw945/mindoc:2.0.1 .
  112. # https://docs.docker.com/engine/reference/commandline/run/#options
  113. # set MINDOC=//d/mindoc # windows
  114. # export MINDOC=/home/ubuntu/mindoc-docker # linux
  115. # docker run --rm -it -p 8181:8181 -v "%MINDOC%":"/mindoc-sync-host" --name mindoc -e MINDOC_ENABLE_EXPORT=true -d gsw945/mindoc:2.0.1