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/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e7e9b81 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,32 @@ +version: "3" +services: + codefever: + image: rexshi/cc: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; + } +}