diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..dd0e815 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,73 @@ +FROM webdevops/php-nginx:7.4 +MAINTAINER rexshi + +EXPOSE 80 22 +ENV GO111MODULE=off + +RUN apt-get update -y \ +&& apt-get install libyaml-dev git golang-go zip sendmail mailutils mariadb-client vim -y \ +&& pecl install yaml \ +&& docker-php-ext-enable yaml + +# Nodejs +RUN cd /usr/local \ +&& wget https://nodejs.org/dist/v16.15.1/node-v16.15.1-linux-x64.tar.xz \ +&& tar -xf node-v16.15.1-linux-x64.tar.xz \ +&& rm -rf node-v16.15.1-linux-x64.tar.xz \ +&& mv node-v16.15.1-linux-x64 node \ +&& ln -s /usr/local/node/bin/node /usr/local/bin/node \ +&& ln -s /usr/local/node/bin/npm /usr/local/bin/npm \ +&& ln -s /usr/local/node/bin/npx /usr/local/bin/npx \ +&& ln -s /usr/local/node/bin/corepack /usr/local/bin/corepack \ +&& corepack enable + +# SSH +RUN docker-service enable ssh && docker-service enable cron + +# Codefever repo +RUN mkdir -p /data/www \ +&& cd /data/www \ +&& git clone https://github.com/PGYER/codefever.git codefever-community \ +&& cd codefever-community + +# Nginx +COPY ./misc/docker/vhost.conf-template /opt/docker/etc/nginx/vhost.conf + +# Go +RUN cd /data/www/codefever-community/http-gateway \ +&& go get gopkg.in/yaml.v2 \ +&& go build main.go \ +&& cd /data/www/codefever-community/ssh-gateway/shell \ +&& go get gopkg.in/yaml.v2 \ +&& go build main.go + +# Codefever worker +COPY misc/docker/supervisor-codefever-modify-authorized-keys.conf /opt/docker/etc/supervisor.d/codefever-modify-authorized-keys.conf +COPY misc/docker/supervisor-codefever-http-gateway.conf /opt/docker/etc/supervisor.d/codefever-http-gateway.conf + +# Configs +RUN useradd -rm git \ + && mkdir /usr/local/php/bin \ + && ln -s /usr/local/bin/php /usr/local/php/bin/php \ + && cd /data/www/codefever-community/misc \ + && cp ./codefever-service-template /etc/init.d/codefever \ + && cp ../config.template.yaml ../config.yaml \ + && cp ../env.template.yaml ../env.yaml \ + && chmod 0777 ../config.yaml ../env.yaml \ + && mkdir ../application/logs \ + && chown -R git:git ../application/logs \ + && chmod -R 0777 ../application/logs \ + && chmod -R 0777 ../git-storage \ + && mkdir ../file-storage \ + && chown -R git:git ../file-storage \ + && chown -R git:git ../misc \ + && chmod +x /opt/docker/etc/supervisor.d/codefever-modify-authorized-keys.conf \ + && chmod +x /opt/docker/etc/supervisor.d/codefever-http-gateway.conf \ + && cd ../application/libraries/composerlib/ \ + && php ./composer.phar install + +# Cron +RUN docker-cronjob '* * * * * sh /data/www/codefever-community/application/backend/codefever_schedule.sh' + +# Entrypoint +COPY misc/docker/docker-entrypoint.sh /opt/docker/provision/entrypoint.d/20-codefever.sh diff --git a/doc/zh-cn/installation/install_via_docker.md b/doc/zh-cn/installation/install_via_docker.md index 4a4c23c..6bf34b5 100644 --- a/doc/zh-cn/installation/install_via_docker.md +++ b/doc/zh-cn/installation/install_via_docker.md @@ -19,6 +19,10 @@ docker run -d --privileged=true --name codefever -p 80:80 -p 22:22 -it pgyer/cod 3. 如果服务异常你可以登录 Shell 去人工维护, 也可以直接重启容器重启服务。 4. 默认管理员用户: `root@codefever.cn`, 密码: `123456`。登录后请修改密码并绑定 MFA 设备。 +### 使用 docker-compose 安装 + +参见: [使用 docker-compose 安装](install_via_docker_compose.md) + ### 服务维护 服务维护请参见 [管理员设置/概览和系统服务](../admin/dashboard.md) 中的 `系统服务状态及维护` 一节 diff --git a/doc/zh-cn/installation/install_via_docker_compose.md b/doc/zh-cn/installation/install_via_docker_compose.md new file mode 100644 index 0000000..efa41c8 --- /dev/null +++ b/doc/zh-cn/installation/install_via_docker_compose.md @@ -0,0 +1,43 @@ +# 使用 docker-compose 安装 + +## 安装步骤 + +### Docker 镜像说明 + +`CodeFever Community 版本` 的用于 docker-compose 使用的镜像位于 `pgyer/codefever-community-lite`,与 pgyer/codefever-community` 的主要区别是不包含数据库服务,这样做的目的是让用户可以将数据库服务独立于容器之外,适用于想自己备份数据库或想独立连接到外部数据库服务的场景。 + +### 拉取镜像并启动 + +首先 clone 整个 CodeFever Community 仓库到您的服务器,然后进入项目的根目录,并执行以下命令: + +```shell +docker-compose run -d +``` + +1. 服务启动后尝试访问 `http://127.0.0.1` 或 `http://` 登录 +2. 如果你希望使用 `22` 端口作为 `Git` 的 `SSH 协议`端口,你需要在启动镜像前将宿主系统的 `SSH 服务` `端口` 先修改成其他端口 +3. 如果服务异常你可以登录 Shell 去人工维护, 也可以直接重启容器重启服务。 +4. 默认管理员用户: `root@codefever.cn`, 密码: `123456`。登录后请修改密码并绑定 MFA 设备。 + +### 构建自己的镜像 + +进入项目的根目录,并执行以下命令: + +```shell +docker build -t you-name/codefever-community-lite:latest . +``` + +### 服务维护 + +服务维护请参见 [管理员设置/概览和系统服务](../admin/dashboard.md) 中的 `系统服务状态及维护` 一节 + +## 此文档适用条件 + +如果你的情况符合以下条件,你需要使用 [从零开始安装](install_from_scratch.md) 的方式安装 `CodeFever` 否则请跳过本章节继续使用 `Docker 镜像安装` 方式安装。 + +- 学习和技术交流 +- 需要做定制化修改 +- `Docker 镜像安装` 不能满足处理 `Bug` 和提交 `PR` 的需求 +- Docker 镜像不能在当前 `操作系统` 或 `硬件架构` 上使用 + +参见: [从零开始安装](install_from_scratch.md) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ec6aaa2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +version: "3" +services: + codefever: + image: pgyer/codefever-community-lite:latest + ports: + - "80:80" + - "22:22" + depends_on: + - db + volumes: + - /data/git-storage:/data/www/codefever-community/git-storage + - /data/logs:/data/www/codefever-community/application/logs + environment: + DB_HOST: "db" + DB_PORT: "3306" + DB_USER: "root" + DB_PASS: "123456" # Need to be the same as MYSQL_ROOT_PASSWORD + DB_NAME: "codefever_community" + + db: + image: mysql:5.7.31 + restart: always + #privileged: true + volumes: + - /data/mysql:/var/lib/mysql + environment: + MYSQL_ROOT_PASSWORD: "123456" + command: [ + '--character-set-server=utf8mb4', + '--collation-server=utf8mb4_general_ci', + '--max_connections=3000' + ] diff --git a/misc/docker/docker-entrypoint.sh b/misc/docker/docker-entrypoint.sh new file mode 100755 index 0000000..d05cd59 --- /dev/null +++ b/misc/docker/docker-entrypoint.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +set +u + +# db +ENV_FILE=/data/www/codefever-community/env.yaml +DB_HOST=${DB_HOST:-"db"} +DB_PORT=${DB_PORT:-"3306"} +DB_USER=${DB_USER:-"root"} +DB_PASS=${DB_PASS:-"123456"} +DB_NAME=${DB_NAME:-"codefever_community"} + +sed -i "s/host: localhost/host: ${DB_HOST}/" ${ENV_FILE} +sed -i "s/port: 3306/port: ${DB_PORT}/" ${ENV_FILE} +sed -i "s/username: root/username: ${DB_USER}/" ${ENV_FILE} +sed -i "s/password: 123456/password: ${DB_PASS}/" ${ENV_FILE} +sed -i "s/db: codefever_community/db: ${DB_NAME}/" ${ENV_FILE} + +sleep 10 + +# init db +if ! mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" -e "use ${DB_NAME}"; then + mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" -e"set global sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';"; + cd /data/www/codefever-community/misc/ + sh ./create_db.sh +fi + +# git-cli +sed -i 's!git: /usr/local/git/bin/git!git: /usr/bin/git!' /data/www/codefever-community/env.yaml +sed -i 's!shell: /usr/local/git/bin/git-shell!shell: /usr/bin/git-shell!' /data/www/codefever-community/env.yaml +sed -i 's!http: /usr/local/git/libexec/git-core/git-http-backend!http: /usr/lib/git-core/git-http-backend!' /data/www/codefever-community/env.yaml + +chmod -R 0777 /data/www/codefever-community/git-storage +chmod -R 0777 /data/www/codefever-community/application/logs diff --git a/misc/docker/supervisor-codefever-http-gateway.conf b/misc/docker/supervisor-codefever-http-gateway.conf new file mode 100644 index 0000000..f49ccb7 --- /dev/null +++ b/misc/docker/supervisor-codefever-http-gateway.conf @@ -0,0 +1,14 @@ +[group:codefever-http-gateway] +programs=codefever-http-gateway +priority=20 + +[program:codefever-http-gateway] +command = /data/www/codefever-community/http-gateway/main +process_name=%(program_name)s +startsecs = 0 +autostart = true +autorestart = true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 diff --git a/misc/docker/supervisor-codefever-modify-authorized-keys.conf b/misc/docker/supervisor-codefever-modify-authorized-keys.conf new file mode 100644 index 0000000..0ac6333 --- /dev/null +++ b/misc/docker/supervisor-codefever-modify-authorized-keys.conf @@ -0,0 +1,14 @@ +[group:codefever-modify-authorized-keys] +programs=codefever-modify-authorized-keys +priority=20 + +[program:codefever-modify-authorized-keys] +command = php /data/www/codefever-community/misc/modify_authorized_keys.php modify_authorized_keys_shell_running +process_name=%(program_name)s +startsecs = 0 +autostart = true +autorestart = true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 diff --git a/misc/docker/vhost.conf-template b/misc/docker/vhost.conf-template new file mode 100644 index 0000000..2e36ac8 --- /dev/null +++ b/misc/docker/vhost.conf-template @@ -0,0 +1,73 @@ +server_names_hash_max_size 512; +server_names_hash_bucket_size 128; + +server { + listen 80; + server_name localhost; + index index.php index.html index.htm; + root /data/www/codefever-community/www/; + + location ~* ^\/[0-9a-z_]+\/[0-9a-z_]+\.git { + proxy_pass http://127.0.0.1:27555; + } + + location ^~ /static/ { + rewrite "(.*)\.map(.*)" "/static/not/found" last; + rewrite "^/static/\d{14}/(.*)$" "/static/$1" last; + try_files $uri /app$uri 404; + } + + location ~* ^/(user|file|feature|doc|community|lang|captcha|boss|service|pricing|api(v\d+)?)?(\/.*)?$ { + try_files $uri $uri/ /index.php; + } + + location / { + try_files $uri /app/index.html; + } + + location ~ \.php$ { + fastcgi_pass 127.0.0.1:9000; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param ENVIRONMENT "production"; + include fastcgi_params; + fastcgi_buffer_size 128k; + fastcgi_buffers 4 256k; + fastcgi_busy_buffers_size 256k; + fastcgi_read_timeout 300s; + } +} + +server { + listen 80; + server_name localhost-dev; + index index.php index.html index.htm; + root /data/www/codefever-community/www/; + + location ~* ^\/[0-9a-z_]+\/[0-9a-z_]+\.git { + proxy_pass http://127.0.0.1:27555; + } + + location ^~ /sockjs-node { + proxy_pass http://127.0.0.1:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } + + location ^~ /__webpack_dev_server__/ { + proxy_pass http://127.0.0.1:3000; + } + + location ~ ^/static/\d+/ { + proxy_pass http://localhost; + } + + location ~* ^/(user|file|feature|doc|community|lang|captcha|boss|service|pricing|api(v\d+)?)?(\/.*)?$ { + proxy_pass http://localhost; + } + + location / { + proxy_pass http://127.0.0.1:3000; + } +}