浏览代码

优化docker

kuaifan 4 年之前
父节点
当前提交
9dbd1f117f
共有 11 个文件被更改,包括 208 次插入142 次删除
  1. 6 6
      .env.docker
  2. 11 11
      .gitignore
  3. 33 11
      bin/wookteam
  4. 73 11
      cmd
  5. 9 7
      database/seeds/SettingTableSeeder.php
  6. 11 9
      database/seeds/UsersTableSeeder.php
  7. 26 16
      docker-compose.yml
  8. 7 3
      docker/php.Dockerfile
  9. 7 1
      docker/wookteam.conf
  10. 12 33
      install/DOCKER.md
  11. 13 34
      install/en/DOCKER.md

+ 6 - 6
.env.docker

@@ -3,8 +3,8 @@ APP_ENV=local
 APP_KEY=
 APP_DEBUG=true
 APP_URL=http://localhost
-APP_PORT=80
-APP_PORT_SSL=443
+APP_PORT=8000
+APP_PORT_SSL=44300
 
 LOG_CHANNEL=stack
 
@@ -18,12 +18,12 @@ DB_ROOT_PASSWORD=123456
 DB_PREFIX=pre_
 
 BROADCAST_DRIVER=log
-CACHE_DRIVER=file
-QUEUE_CONNECTION=sync
-SESSION_DRIVER=file
+CACHE_DRIVER=redis
+QUEUE_CONNECTION=redis
+SESSION_DRIVER=redis
 SESSION_LIFETIME=120
 
-REDIS_HOST=127.0.0.1
+REDIS_HOST=redis
 REDIS_PASSWORD=null
 REDIS_PORT=6379
 

+ 11 - 11
.gitignore

@@ -1,17 +1,16 @@
 /node_modules
-/public/audio
-/public/css
-/public/fonts
-/public/images
-/public/js
+/public/hot
+/public/tmp
 /public/uploads/*
 /public/.well-known
 /public/.user.ini
-/public/hot
 /storage/*.key
-/docker/mariadb
-/docker/log
 /vendor
+/docker/log/
+/docker/redis/
+/docker/mariadb/
+/tmp
+._*
 .env
 .env.backup
 .idea
@@ -22,8 +21,9 @@ Homestead.json
 Homestead.yaml
 npm-debug.log
 yarn-error.log
-test.js
-test.php
+test.*
 composer.lock
 package-lock.json
-mix-manifest.json
+laravels-timer-process.pid
+.DS_Store
+vars.yaml

+ 33 - 11
bin/wookteam

@@ -22,18 +22,40 @@ class wookteamLoader
         file_put_contents($envPath, $envContent);
         return true;
     }
-}
 
-$array = getopt('', ['port:', 'ssl:']);
-$data = [];
-if (isset($array['port'])) {
-    $data['APP_PORT'] = $array['port'] ?: '8080';
-}
-if (isset($array['ssl'])) {
-    $data['APP_PORT_SSL'] = $array['ssl'] ?: '4433';
+    function modifyWookteam($type)
+    {
+        $envPath = realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR . '/docker/php.conf';
+        if (!file_exists($envPath)) {
+            return false;
+        }
+        $envContent = file_get_contents($envPath);
+        $envContent = str_replace("#command=php bin/laravels start -i", "command=php bin/laravels start -i", $envContent);
+        $envContent = str_replace("#command=./bin/inotify ./app", "command=./bin/inotify ./app", $envContent);
+        if ($type == "dev") {
+            $envContent = str_replace("command=php bin/laravels start -i", "#command=php bin/laravels start -i", $envContent);
+        } else {
+            $envContent = str_replace("command=./bin/inotify ./app", "#command=./bin/inotify ./app", $envContent);
+        }
+        file_put_contents($envPath, $envContent);
+        return true;
+    }
 }
-if ($data) {
-    $loader = new wookteamLoader();
-    $loader->modifyEnv($data);
+
+$array = getopt('', ['port:', 'ssl:', 'wookteam:']);
+$loader = new wookteamLoader();
+if (isset($array['wookteam'])) {
+    $loader->modifyWookteam($array['wookteam']);
+} else {
+    $data = [];
+    if (isset($array['port'])) {
+        $data['APP_PORT'] = $array['port'] ?: '8000';
+    }
+    if (isset($array['ssl'])) {
+        $data['APP_PORT_SSL'] = $array['ssl'] ?: '44300';
+    }
+    if ($data) {
+        $loader->modifyEnv($data);
+    }
 }
 

+ 73 - 11
cmd

@@ -1,36 +1,98 @@
 #!/usr/bin/env bash
 
+#fonts color
+Green="\033[32m"
+Red="\033[31m"
+GreenBG="\033[42;37m"
+RedBG="\033[41;37m"
+Font="\033[0m"
+
+#notification information
+OK="${Green}[OK]${Font}"
+Error="${Red}[错误]${Font}"
+
+cur_path="$(pwd)"
+
+supervisorctl_restart() {
+    RES=`docker-compose exec php /bin/bash -c "supervisorctl update $1"`
+    if [ -z "$RES" ];then
+        docker-compose exec php /bin/bash -c "supervisorctl restart $1"
+    else
+        echo -e "$RES"
+    fi
+}
+
+####################################################################################
+####################################################################################
+####################################################################################
+
 COMPOSE="docker-compose"
 
 if [ $# -gt 0 ];then
-    if [[ "$1" == "artisan" ]]; then
+    if [[ "$1" == "init" ]] || [[ "$1" == "install" ]]; then
         shift 1
-        $COMPOSE run --rm -w /var/www php php artisan "$@"
+        if [ ! -f ".env" ];then
+            cp .env.docker .env
+        fi
+        rm -rf composer.lock
+        rm -rf package-lock.json
+        $COMPOSE build php
+        $COMPOSE up -d
+        $COMPOSE restart php
+        $COMPOSE exec php /bin/bash -c "composer install"
+        $COMPOSE exec php /bin/bash -c "php artisan key:generate"
+        $COMPOSE exec php /bin/bash -c "php artisan migrate --seed"
+        $COMPOSE exec php /bin/bash -c "php bin/wookteam --port=8000 --ssl=44300"
+        $COMPOSE exec php /bin/bash -c "php bin/wookteam --wookteam=prod"
+        $COMPOSE stop
+        $COMPOSE start
+    elif [[ "$1" == "update" ]]; then
+        shift 1
+        git fetch --all
+        git reset --hard origin/master
+        git pull
+        $COMPOSE exec php /bin/bash -c "composer install"
+        $COMPOSE exec php /bin/bash -c "php artisan migrate"
+        $COMPOSE stop
+        $COMPOSE start
+    elif [[ "$1" == "dev" ]]; then
+        shift 1
+        $COMPOSE exec php /bin/bash -c "php bin/wookteam --wookteam=dev"
+        supervisorctl_restart php
+        npm run hot
+    elif [[ "$1" == "prod" ]]; then
+        shift 1
+        $COMPOSE exec php /bin/bash -c "php bin/wookteam --wookteam=prod"
+        supervisorctl_restart php
+        npm run prod
+    elif [[ "$1" == "artisan" ]]; then
+        shift 1
+        e="php artisan $@" && $COMPOSE exec php /bin/bash -c "$e"
     elif [[ "$1" == "php" ]]; then
         shift 1
-        $COMPOSE run --rm -w /var/www php php "$@"
+        e="php $@" && $COMPOSE exec php /bin/bash -c "$e"
     elif [[ "$1" == "composer" ]]; then
         shift 1
-        $COMPOSE run --rm -w /var/www php composer "$@"
+        e="composer $@" && $COMPOSE exec php /bin/bash -c "$e"
     elif [[ "$1" == "supervisorctl" ]]; then
         shift 1
-        $COMPOSE run --rm -w /var/www php supervisorctl "$@"
+        e="supervisorctl $@" && $COMPOSE exec php /bin/bash -c "$e"
     elif [[ "$1" == "test" ]]; then
         shift 1
-        $COMPOSE run --rm -w /var/www php ./vendor/bin/phpunit "$@"
+        e="./vendor/bin/phpunit $@" && $COMPOSE exec php /bin/bash -c "$e"
     elif [[ "$1" == "npm" ]]; then
         shift 1
-        $COMPOSE run --rm -w /var/www php npm "$@"
+        e="npm $@" && $COMPOSE exec php /bin/bash -c "$e"
     elif [[ "$1" == "yarn" ]]; then
         shift 1
-        $COMPOSE run --rm -w /var/www php yarn "$@"
+        e="yarn $@" && $COMPOSE exec php /bin/bash -c "$e"
     elif [[ "$1" == "mysql" ]]; then
         shift 1
-        $COMPOSE run --rm -w / mariadb mysql "$@"
+        e="mysql $@" && $COMPOSE exec mariadb /bin/sh -c "$e"
     elif [[ "$1" == "restart" ]]; then
         shift 1
-        $COMPOSE stop
-        $COMPOSE start
+        $COMPOSE stop "$@"
+        $COMPOSE start "$@"
     else
         $COMPOSE "$@"
     fi

+ 9 - 7
database/seeds/SettingTableSeeder.php

@@ -12,12 +12,14 @@ class SettingTableSeeder extends Seeder
      */
     public function run()
     {
-        
 
-        \DB::table('setting')->delete();
-        
+
+        if (\DB::table('setting')->count() > 0) {
+            return;
+        }
+
         \DB::table('setting')->insert(array (
-            0 => 
+            0 =>
             array (
                 'id' => 1,
                 'title' => 'system',
@@ -25,7 +27,7 @@ class SettingTableSeeder extends Seeder
                 'setting' => '{\\"reg\\":\\"open\\",\\"github\\":\\"show\\",\\"logo\\":\\"\\"}',
             ),
         ));
-        
-        
+
+
     }
-}
+}

+ 11 - 9
database/seeds/UsersTableSeeder.php

@@ -14,7 +14,9 @@ class UsersTableSeeder extends Seeder
     {
 
 
-        \DB::table('users')->delete();
+        if (\DB::table('users')->count() > 0) {
+            return;
+        }
 
         \DB::table('users')->insert(array (
             0 =>
@@ -30,13 +32,13 @@ class UsersTableSeeder extends Seeder
                 'encrypt' => 'OHsJ88',
                 'userpass' => '3dd0e69a6da5b87a9de356cc8f22a1e3',
                 'bgid' => 1,
-                'loginnum' => 353,
+                'loginnum' => 0,
                 'lastip' => '127.0.0.1',
-                'lastdate' => 1591343855,
+                'lastdate' => time(),
                 'lineip' => '127.0.0.1',
-                'linedate' => 1591343909,
+                'linedate' => time(),
                 'regip' => '127.0.0.1',
-                'regdate' => 1589072625,
+                'regdate' => time(),
                 'setting' => '{\\"version\\":1}',
             ),
             1 =>
@@ -52,13 +54,13 @@ class UsersTableSeeder extends Seeder
                 'encrypt' => 'AA1lsv',
                 'userpass' => '80c515b27ae42470a7c90e59586caaab',
                 'bgid' => 1,
-                'loginnum' => 345,
+                'loginnum' => 0,
                 'lastip' => '127.0.0.1',
-                'lastdate' => 1591342690,
+                'lastdate' => time(),
                 'lineip' => '127.0.0.1',
-                'linedate' => 1591343157,
+                'linedate' => time(),
                 'regip' => '127.0.0.1',
-                'regdate' => 1589072625,
+                'regdate' => time(),
                 'setting' => '{\\"version\\":1}',
             ),
         ));

+ 26 - 16
docker-compose.yml

@@ -1,8 +1,27 @@
 version: '3'
 
 services:
+  php:
+    hostname: php
+    image: "kuaifan/wookteam"
+    volumes:
+      - ./docker/php.conf:/etc/supervisor/conf.d/php.conf
+      - ./docker/log/supervisor:/var/log/supervisor
+      - ./docker/php.ini:/usr/local/etc/php/php.ini
+      - ./:/var/www
+    environment:
+      TZ: "Asia/Shanghai"
+      LANG: "C.UTF-8"
+    links:
+      - redis
+      - mariadb
+    depends_on:
+      - redis
+      - mariadb
+    restart: unless-stopped
+
   nginx:
-    image: nginx
+    image: "nginx:alpine"
     ports:
       - "${APP_PORT}:80"
       - "${APP_PORT_SSL}:443"
@@ -15,27 +34,18 @@ services:
       - php
     restart: unless-stopped
 
-  php:
-    hostname: php
-    build:
-      context: .
-      dockerfile: docker/php.Dockerfile
+  redis:
+    hostname: redis
+    image: "redis:alpine"
     volumes:
-      - ./docker/wookteam.conf:/etc/supervisor/conf.d/wookteam.conf
-      - ./docker/log/supervisor:/var/log/supervisor
-      - ./docker/php.ini:/usr/local/etc/php/php.ini
-      - ./:/var/www
+      - ./docker/redis:/data
     environment:
       TZ: "Asia/Shanghai"
-    links:
-      - mariadb
-    depends_on:
-      - mariadb
+    restart: unless-stopped
 
   mariadb:
     hostname: mariadb
     image: "bitnami/mariadb"
-    user: root
     volumes:
       - ./docker/mariadb:/bitnami/mariadb
     environment:
@@ -43,4 +53,4 @@ services:
       - "MARIADB_DATABASE=${DB_DATABASE}"
       - "MARIADB_USER=${DB_USERNAME}"
       - "MARIADB_PASSWORD=${DB_PASSWORD}"
-    restart: always
+    restart: unless-stopped

+ 7 - 3
docker/php.Dockerfile

@@ -1,4 +1,4 @@
-FROM phpswoole/swoole:4.5.9-php7.4
+FROM phpswoole/swoole:php7.4
 
 # Installation dependencies and PHP core extensions
 RUN apt-get update \
@@ -24,9 +24,13 @@ RUN apt-get update \
         inotify-tools \
         && curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - \
         && apt-get -y install nodejs \
-        && rm -r /var/lib/apt/lists/* \
         && docker-php-ext-configure gd --with-freetype --with-jpeg \
-        && docker-php-ext-install pdo_mysql gd pcntl zip
+        && docker-php-ext-install pdo_mysql gd pcntl zip \
+        && mkdir -p /usr/src/php/ext/redis \
+        && curl -L https://github.com/phpredis/phpredis/archive/5.3.2.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 \
+        && echo 'redis' >> /usr/src/php-available-exts \
+        && docker-php-ext-install redis \
+        && rm -r /var/lib/apt/lists/*
 
 # Set the WORKDIR to /var/www so all following commands run in /var/www
 WORKDIR /var/www

+ 7 - 1
docker/wookteam.conf

@@ -1,6 +1,12 @@
-[program:wookteam]
+[program:php]
 directory=/var/www
+
+# 生产环境
 command=php bin/laravels start -i
+
+# 开发环境
+#command=./bin/inotify ./app
+
 numprocs=1
 autostart=true
 autorestart=true

+ 12 - 33
install/DOCKER.md

@@ -10,40 +10,29 @@
 
 > 必须安装 `Docker` 和 `Docker Compose`
 
-#### 1、克隆项目到您的本地或服务器
+### 部署项目
 
 ```bash
-// 使用ssh
+# 1、克隆项目到您的本地或服务器
+
+# 使用ssh
 git clone git@github.com:kuaifan/wookteam.git
-// 或者你也可以使用https
+# 或者你也可以使用https
 git clone https://github.com/kuaifan/wookteam.git
 
-// 进入目录
+# 2、进入目录
 cd wookteam
 
-// 拷贝 .env
-cp .env.docker .env
+# 3、一键构建项目
+./cmd install
 ```
 
-#### 2、构建项目
-
-```bash
-./cmd composer install
-./cmd artisan key:generate
-./cmd artisan migrate --seed
-./cmd php bin/wookteam --port=8080 --ssl=4433
-./cmd up -d
-./cmd npm install
-./cmd npm run prod
-./cmd restart
-```
-
-到此安装完毕,项目地址为:**`http://IP:PORT`**(`PORT`为构建项目中的参数`8080`)。
+安装完毕,项目地址为:**`http://IP:PORT`**(`PORT`默认为`8000`)。
 
 ### 更换端口
 
 ```bash
-./cmd php bin/wookteam --port=8080 --ssl=4433
+./cmd php bin/wookteam --port=8000 --ssl=44300
 ./cmd up -d
 ```
 
@@ -83,17 +72,7 @@ cp .env.docker .env
 
 **注意:在升级之前请备份好你的数据!**
 
-- 进入目录,依次运行以下命令:
-
 ```bash
-git fetch --all
-git reset --hard origin/master
-git pull
-./cmd composer update
-./cmd artisan migrate
-
-./cmd npm install
-./cmd npm run prod
-
-./cmd restart
+# 进入目录,运行命令
+./cmd update
 ```

+ 13 - 34
install/en/DOCKER.md

@@ -10,40 +10,29 @@
 
 > `Docker` & `Docker Compose` must be installed
 
-#### 1. Clone the repository
+
+### Deployment project
 
 ```bash
-// using ssh
+# 1、Clone the repository
+
+# using ssh
 git clone git@github.com:kuaifan/wookteam.git
-// or you can use https
+# or you can use https
 git clone https://github.com/kuaifan/wookteam.git
 
-// enter directory
+# enter directory
 cd wookteam
 
-// copy .env
-cp .env.docker .env
+# 3、Build project
+./cmd install
 ```
-
-#### 2. Build image & install
-
-```bash
-./cmd composer install
-./cmd artisan key:generate
-./cmd artisan migrate --seed
-./cmd php bin/wookteam --port=8080 --ssl=4433
-./cmd up -d
-./cmd npm install
-./cmd npm run prod
-./cmd restart
-```
-
-Installed, project url: **`http://IP:PORT`** (`PORT` is the parameter `8080` in the build).
+Installed, project url: **`http://IP:PORT`**(`PORT`Default is`8000`)。
 
 ### Change port
 
 ```bash
-./cmd php bin/wookteam --port=8080 --ssl=4433
+./cmd php bin/wookteam --port=8000 --ssl=44300
 ./cmd up -d
 ```
 
@@ -83,17 +72,7 @@ Installed, project url: **`http://IP:PORT`** (`PORT` is the parameter `8080` in
 
 **Note: Please backup your data before upgrading!**
 
-- Go to the directory and run the following commands in turn:
-
 ```bash
-git fetch --all
-git reset --hard origin/master
-git pull
-./cmd composer update
-./cmd artisan migrate
-
-./cmd npm install
-./cmd npm run prod
-
-./cmd restart
+# Enter directory and run command
+./cmd update
 ```