本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或从新修改使用,但须要注明来源。 署名 4.0 国际 (CC BY 4.0)html
本文做者: 苏洋mysql
建立时间: 2020年02月04日 统计字数: 5934字 阅读时间: 12分钟阅读 本文连接: soulteary.com/2020/02/04/…git
使用 Docker 和 Traefik v1 搭建轻量代码仓库(Gogs) 一文中,提到了 Gogs。本文将介绍它的加强版本:Gitea 以及如何搭配 Traefik v2 一块儿使用。web
若是你有了解过以前到文章,大概三分钟左右能够搭建完毕。redis
官方提供了一份表格,展现了Gitea 与其余“代码仓库”的差别,有兴趣能够看看。sql
本文将使用到 Traefik 和 Docker,若是不太熟悉,能够阅读以往的文章以作了解:Docker、Traefik。docker
咱们使用 SSH 和 HTTP 协议进行数据上传下载(git clone
/ git push
),因此须要让 Traefik 提供 TCP 协议服务,这里建议单独新建一个入口点。数据库
由于在 Traefik v2 中,每个用户可以访问到的服务都须要一个入口点(entrypoint),若是咱们不单独指定入口点背后的服务类型,那么入口点会先尝试看看它背后对接的服务是不是 TCP,若是不是的话,再尝试使用 HTTP 协议提供服务,因此若是不分离两种协议的话,对总体服务的性能会有必定影响。编程
参考 Traefik 2 使用指南,愉悦的开发体验 一文中的配置,在 traefik.toml
中添加一个新的入口点:json
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.https]
address = ":443"
[entryPoints.git]
address = ":22"
复制代码
若是是使用 docker-compose
启动的 Traefik 服务,那么须要将对应的端口也进行开放:
traefik:
container_name: traefik
image: traefik:v2.1.3
restart: always
ports:
- 80:80
- 443:443
- 22:22
复制代码
在 Gogs 那篇文章中提到过,不推荐使用 SQLite 做为数据库,不过若是你备份比较勤快,以为问题不大,那么能够试试下面的配置:
version: '3.6'
services:
gitea:
image: gitea/gitea:1.10.3
environment:
- USER_UID=1000
- USER_GID=1000
- APP_NAME=Private
- RUN_MODE=prod
- RUN_USER=git
- SSH_DOMAIN=gitea.lab.com
- SSH_PORT=22
- SSH_LISTEN_PORT=22
# 若是不但愿使用 SSH 协议
#- DISABLE_SSH=true
- HTTP_PORT=3000
- ROOT_URL=https://gitea.lab.com
- LFS_START_SERVER=false
- DB_TYPE=sqlite3
- INSTALL_LOCK=false
- DISABLE_GRAVATAR=true
networks:
- traefik
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.docker.network=traefik"
- "traefik.http.routers.giteaweb.middlewares=https-redirect@file"
- "traefik.http.routers.giteaweb.entrypoints=http"
- "traefik.http.routers.giteaweb.rule=Host(`gitea.lab.com`)"
- "traefik.http.routers.giteassl.middlewares=content-compress@file"
- "traefik.http.routers.giteassl.entrypoints=https"
- "traefik.http.routers.giteassl.tls=true"
- "traefik.http.routers.giteassl.rule=Host(`gitea.lab.com`)"
- "traefik.http.services.giteabackend.loadbalancer.server.scheme=http"
- "traefik.http.services.giteabackend.loadbalancer.server.port=3000"
- "traefik.tcp.routers.giteassh.rule=HostSNI(`*`)"
- "traefik.tcp.routers.giteassh.tls=true"
- "traefik.tcp.routers.giteassh.entrypoints=git"
- "traefik.tcp.routers.giteassh.service=gitea"
- "traefik.tcp.services.gitea.loadbalancer.server.port=22"
volumes:
# 标准 Linux 系统下使用
# - /etc/localtime:/etc/localtime:ro
# - /etc/timezone:/etc/timezone:ro
- ./repositories:/data/git/repositories
- ./data:/data/gitea/
logging:
driver: "json-file"
options:
max-size: "10m"
extra_hosts:
- "git.lab.com:127.0.0.1"
healthcheck:
test: ["CMD-SHELL", "wget -q --spider --proxy off localhost:3000 || exit 1"]
interval: 5s
networks:
traefik:
external: true
复制代码
将内容保存为 docker-compose.yml
,使用 docker-compose up -d
启动服务,访问上面配置的域名,会看到 Gitea 的欢迎界面。
打开页面看到服务已经正确启动起来了,点击注册/登录按钮,首次使用会被重定向到 /install
目录。
若是你肯定要使用 SQLite ,能够填写下管理员帐号,而后点击当即安装便可。
接下来就是配置仓库,正常推送数据啦。
这里推荐云服务低配置数据库实例,不过若是低频率使用,使用 docker-compose
启动一个实例也问题不大,以 MySQL 为例。
version: '3.6'
services:
gitea:
image: gitea/gitea:1.10.3
environment:
- USER_UID=1000
- USER_GID=1000
- APP_NAME=Private
- RUN_MODE=prod
- RUN_USER=git
- SSH_DOMAIN=gitea.lab.com
- SSH_PORT=22
- SSH_LISTEN_PORT=22
# 若是不但愿使用 SSH 协议
#- DISABLE_SSH=true
- HTTP_PORT=3000
- ROOT_URL=https://gitea.lab.com
- LFS_START_SERVER=false
- DB_TYPE=mysql
- DB_HOST=db
- DB_NAME=gitea
- DB_USER=gitea
- DB_PASSWD=gitea
- DB_CHARSET=utf8
- INSTALL_LOCK=false
- DISABLE_GRAVATAR=true
networks:
- traefik
- gitea
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.docker.network=traefik"
- "traefik.http.routers.giteaweb.middlewares=https-redirect@file"
- "traefik.http.routers.giteaweb.entrypoints=http"
- "traefik.http.routers.giteaweb.rule=Host(`gitea.lab.com`)"
- "traefik.http.routers.giteassl.middlewares=content-compress@file"
- "traefik.http.routers.giteassl.entrypoints=https"
- "traefik.http.routers.giteassl.tls=true"
- "traefik.http.routers.giteassl.rule=Host(`gitea.lab.com`)"
- "traefik.http.services.giteabackend.loadbalancer.server.scheme=http"
- "traefik.http.services.giteabackend.loadbalancer.server.port=3000"
- "traefik.tcp.routers.giteassh.rule=HostSNI(`*`)"
- "traefik.tcp.routers.giteassh.tls=true"
- "traefik.tcp.routers.giteassh.entrypoints=git"
- "traefik.tcp.routers.giteassh.service=gitea"
- "traefik.tcp.services.gitea.loadbalancer.server.port=22"
volumes:
# 标准 Linux 系统下使用
# - /etc/localtime:/etc/localtime:ro
# - /etc/timezone:/etc/timezone:ro
- ./repositories:/data/git/repositories
- ./data:/data/gitea/
logging:
driver: "json-file"
options:
max-size: "10m"
extra_hosts:
- "git.lab.com:127.0.0.1"
healthcheck:
test: ["CMD-SHELL", "wget -q --spider --proxy off localhost:3000 || exit 1"]
interval: 5s
db:
image: mysql:5.7.16
restart: always
networks:
- gitea
expose:
- 3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
MYSQL_ROOT_PASSWORD: gitea
MYSQL_DATABASE: gitea
MYSQL_USER: gitea
MYSQL_PASSWORD: gitea
TZ: Asia/Shanghai
volumes:
- ./mysql:/var/lib/mysql
# 标准 Linux 系统下使用
# - /etc/localtime:/etc/localtime:ro
# - /etc/timezone:/etc/timezone:ro
# healthcheck:
# test: ["CMD-SHELL", "/etc/init.d/mysql status"]
# interval: 30s
cache:
image: redis:5.0-alpine
restart: always
networks:
- gitea
expose:
- 6379
healthcheck:
test: ["CMD", "redis-cli", "ping"]
environment:
TZ: Asia/Shanghai
# volumes:
# 标准 Linux 系统下使用
# - /etc/localtime:/etc/localtime:ro
# - /etc/timezone:/etc/timezone:ro
networks:
gitea:
internal: true
traefik:
external: true
复制代码
一样的,将内容保存为 docker-compose.yml
,使用 docker-compose up -d
启动服务,访问上面配置的域名,而后参考上文进行配置安装便可。
更多的配置变量能够从这里找到:config-cheat-sheet,不过并非每一个变量均可以直接使用,具体是否使用须要翻看代码或者进行尝试。
当服务完整启动以后,在 Traefik 的控制面板中将看到 TCP 路由和服务的完整状态,相似下面这样:
先写到这里,其实已经聊过很多软件,后续有机会聊聊这类软件的用户集成,以及权限管理。
--EOF
我如今有一个小小的折腾群,里面汇集了一些喜欢折腾的小伙伴。
在不发广告的状况下,咱们在里面会一块儿聊聊软件、HomeLab、编程上的一些问题,也会在群里不按期的分享一些技术沙龙的资料。
喜欢折腾的小伙伴欢迎扫码添加好友。(请注明来源和目的,不然不会经过审核)