Docker搭建Markdown共享编辑器

发布于 2021-11-09  248 次阅读


服务地址

前情提要

  • 腾讯云双十一特惠,一次买了个8M带宽、2核4G的云服务器……不拿它整点活可惜了
  • 目前的想法是搞个markdown共享编辑器、个人网盘啥的

搭建动机

主流的共享编辑器有很多,这里安利一个我个人比较喜欢的:HackmdCodimd

上述的链接需要翻墙,非常不方便,所以我想在自己的服务器里面也造一个……

前期准备

  • 一个VPS
  • 一个SSL证书(可选)
  • VPS中安装了dockerdocker-compose等服务
sudo apt install docker
sudo apt install docker.io
sudo apt install docker-compose
sudo service docker start
# 下载、启动docker等

部署方式

  • 官方推荐通过容器部署,这里也是介绍容器部署方式,同时添加nginx代理
  • 其他部署方式可以参考官方文档

编写yml文件

创建一个空目录,在该目录中创建docker-compose.yml

内容直接利用官方的:

version: "3"
services:
  database:
    image: postgres:11.6-alpine
    environment:
      - POSTGRES_USER=codimd
      - POSTGRES_PASSWORD=change_password
      - POSTGRES_DB=codimd
    volumes:
      - "database-data:/var/lib/postgresql/data"
    restart: always
  codimd:
    image: hackmdio/hackmd:2.4.1
    environment:
      - CMD_DB_URL=postgres://codimd:change_password@database/codimd
      - CMD_USECDN=false
    depends_on:
      - database
    ports:
      - "3000:3000"
    volumes:
      - upload-data:/home/hackmd/app/public/uploads
    restart: always
volumes:
  database-data: {}
  upload-data: {}

此时通过docker-compose up -d启动容器,这个服务默认端口是3000。在浏览器输入你的域名和端口http://xxxx.xxx.xxx:3000就能看到这个编辑器了。

如果看不到的话记得检查一下云服务器厂商的防火墙关没关……

image.png

当然,这个是最基础的版本,我们可以继续优化一下。

添加Nginx代理

目录结构:

.
├── docker-compose.yml
└── proxy
    └── conf.d
        └── hackmd.conf

docker-compose.yml中添加关于nginx相关内容,大致内容如下:

version: "3"
services:
  database:
    image: postgres:11.6-alpine
    environment:
      - POSTGRES_USER=codimd
      - POSTGRES_PASSWORD=change_password
      - POSTGRES_DB=codimd
    volumes:
      - "database-data:/var/lib/postgresql/data"
    restart: always
  codimd:
    image: hackmdio/hackmd:latest
    environment:
      - CMD_DB_URL=postgres://codimd:change_password@database/codimd
      - CMD_USECDN=false
    depends_on:
      - database
    volumes:
      - upload-data:/home/hackmd/app/public/uploads
    restart: always

  proxy:
    image: nginx
    restart: unless-stopped
    expose:
      - "80"
    ports:
     - 3000:80
    # 修改端口
    volumes:
      - ./proxy/conf.d:/etc/nginx/conf.d:ro
    links:
      - codimd:codimd
    depends_on:
      - codimd

volumes:
  database-data: {}
  upload-data: {}

同时配置hackmd.conf,作为nginx的配置文件,大致内容如下:

upstream @codimd {
    server codimd:3000;
    keepalive 300;
}

# for socket.io (http upgrade)
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
  listen 80;
  server_name  xxx.xxx.xx;

  location / {
    # set header for proxy protocol
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;

    proxy_read_timeout 300;
    proxy_connect_timeout 300;
    proxy_pass http://@codimd;
  }
}

然后再重新部署容器:

docker-compose down
# 删除上一个服务

docker-compose up -d
# 启动容器

此时我们的nginx代理已经开始运行了:

image.png

启用SSL(可选)

参考官方文档

注册SSL证书,并将目录放置到proxy目录中:

.
├── docker-compose.yml
└── proxy
    ├── conf.d
    │   └── hackmd.conf
    └── ssl_certs
        ├── cert.cer
        ├── cert.key
        └── fullchain.cer

修改docker-compose.yml

version: "3"
services:
  database:
    image: postgres:11.6-alpine
    environment:
      - POSTGRES_USER=codimd
      - POSTGRES_PASSWORD=change_password
      - POSTGRES_DB=codimd
    volumes:
      - "database-data:/var/lib/postgresql/data"
    restart: always
  codimd:
    image: hackmdio/hackmd:latest
    environment:
      - CMD_DB_URL=postgres://codimd:change_password@database/codimd
      - CMD_USECDN=false
    depends_on:
      - database
    volumes:
      - upload-data:/home/hackmd/app/public/uploads
    restart: always

  proxy:
    image: nginx
    restart: unless-stopped
    expose:
      - "80"
    ports:
     - 3000:443
     # 修改端口
    volumes:
      - ./proxy/conf.d:/etc/nginx/conf.d:ro
      - ./proxy/ssl_certs:/etc/nginx/ssl_certs:ro
    links:
      - codimd:codimd
    depends_on:
      - codimd

volumes:
  database-data: {}
  upload-data: {}

修改hackmd.conf

把域名和证书设置为自己的:

# setup a upstream point to CodiMD server
upstream @codimd {
    server codimd:3000;
    keepalive 300;
}

# for socket.io (http upgrade)
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name your.domain.name;
    # enforce https
    return 301 https://$server_name:443$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name your.domain.name;

    ssl_certificate /etc/nginx/ssl_certs/cert.cer;
    ssl_certificate_key /etc/nginx/ssl_certs/cert.key;

    location / {
        proxy_http_version 1.1;
        
        # set header for proxy protocol
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        
        # setup for image upload
        client_max_body_size 8192m;
        
        # adjust proxy buffer setting
        proxy_buffers 8 32k; 
        proxy_buffer_size 32k; 
        proxy_busy_buffers_size 64k;
  
        proxy_max_temp_file_size 8192m;
        
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_pass http://@codimd;
      }
}

然后重新构造容器:

docker-compose down
# 删除上一个服务

docker-compose up -d
# 部署容器

docker-compose restart
# 重启容器

此时已经可以https访问了:

image.png

一些BUG

证书相关

如果http能访问,但是https不能访问,可以检查一下证书路径是否设置正确、nginx监听端口是否正确等。多看docker-compose logs,看看具体报错

docker-compose logs
docker-compose logs --tail 10
docker-compose logs --tail 10 | grep proxy

代理相关

检查一下nginx的配置文件有没有写错……

之前我本人写的xxx.conf就有bug,一直登录不上

主页能访问,但是无法登陆

再次检查一下nginx的配置文件有没有写错……

如果完全不会写,可以直接抄这篇博客里面的,修改一个个人信息配置就行。

原文作者:xiabee

原文链接:https://blog.xiabee.cn/posts/markdown-docker/

发表日期:November 7th 2021, 8:00:23 pm

更新日期:November 7th 2021, 4:09:18 pm

版权声明:本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可


Be all you can be.