从零开始 - Docker部署前后端分离项目(四)

2022-08-22 15:04:55 浏览数 (1)

一、start.sh的编写

代码语言:javascript复制
#! /bin/bash

project=mwj

delete(){
    # 删除操作
    echo "开始删除项目"
    echo "删除项目只会删除容器,不会删除卷"
    echo "要删除数据卷请手动删除"
    docker-compose -p $project down
}

deploy(){
    # 部署操作
    echo "开始部署项目"
    echo "注意重新部署项目会重新构建镜像!"
    docker-compose -p $project ps | grep -E "${project}_.*" &> /dev/null
    if (($?==0))
    then
        echo "已存在项目${project}!"
        read -p "要重新部署项目吗?(y/n)" confirm
        if [[ $confirm == "y" ]]
        then
            delete
            docker-compose -p $project up -d --build
        fi
    else
        docker-compose -p $project up -d --build
    fi
    
}

restart(){
    # 重启操作
    echo "开始重启项目"
    docker-compose -p $project ps | grep -E "${project}_.*" &> /dev/null
    if (($?!=0))
    then
        echo "不存在项目${project}"
    else
        docker-compose -p $project restart
    fi
    
}

close(){
    echo "开始暂停项目"
    docker-compose -p $project stop
}



start(){
    while true
    do
        echo "小仔自动化测试平台部署系统"
        select name in "部署项目" "重启项目" "暂停项目" "删除项目" "退出菜单"
        do 
            case $name in
                "部署项目")
                    deploy
                    break
                    ;;
                "重启项目")
                    restart
                    break
                    ;;
                "暂停项目")
                    close
                    break
                    ;;
                "删除项目")
                    delete
                    break
                    ;;
                "退出菜单")
                    break
                    ;;
             esac
         done
         if [ $name = "退出菜单" ]; then
             break
         fi
    done
    

}

start

二、删除已构建的docker项目

删除之前的容器镜像

连名带卷删除容器 docker-compose -p mwj down -v

代码语言:javascript复制
-p,–project-name NAME指定项目名称,默认将使用所在目录名称作为项目名。
up 启动服务
-d:在后台运行服务容器;
build 构建(重新构建)项目中的服务容器
down 停止容器
-v:打印版本信息并退出。

删除

代码语言:javascript复制
docker-compose -p mwj down -v
docker ps 
docker volume ls #查看卷
docker volume prune #删除卷
代码语言:javascript复制
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform# docker-compose -p mwj down -v
Stopping mwj_nginx_1      ... done
Stopping mwj_django_app_1 ... done
Stopping mwj_mariadb_1    ... done
Stopping mwj_redis_1      ... done
Removing mwj_nginx_1      ... done
Removing mwj_django_app_1 ... done
Removing mwj_mariadb_1    ... done
Removing mwj_redis_1      ... done
Removing network mwj_default
Removing volume mwj_redis_data
Removing volume mwj_mariadb_data
Removing volume mwj_app_logs
Removing volume mwj_nginx_logs
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform# docker volume ls
DRIVER    VOLUME NAME
local     5edf2c1082976dc0ddbc9f3149a156eede692b71a0254fe707cd6e7712769b93
local     c6e344fd8b2e445e7aca8315bce06335f1bbd45e36c237d62532112e6ea285d0
local     django_logs
local     mariadb_data
local     nginx_logs
local     redis_data
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform# docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
5edf2c1082976dc0ddbc9f3149a156eede692b71a0254fe707cd6e7712769b93
nginx_logs
c6e344fd8b2e445e7aca8315bce06335f1bbd45e36c237d62532112e6ea285d0
mariadb_data
redis_data
django_logs

Total reclaimed space: 212MB
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform# docker volume ls
DRIVER    VOLUME NAME
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform#

运行start.sh

代码语言:javascript复制
chmod  777 start.sh
./strat.sh 
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform# ./start.sh
小仔自动化测试平台部署系统
1) 部署项目
2) 重启项目
3) 暂停项目
4) 删除项目
5) 退出菜单
#?

选择对应的选项即可自动部署

运行完之后

代码语言:javascript复制
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform# docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED       STATUS       PORTS                                                                          NAMES
215336c6a95f   mwj_nginx         "/docker-entrypoint.…"   2 hours ago   Up 2 hours   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8001->8001/tcp, :::8001->8001/tcp   mwj_nginx_1
b3e570ceb947   mwj_android_pla   "./entrypoint.sh"        2 hours ago   Up 2 hours   8000/tcp                                                                       mwj_android_pla_1
7bcec1565705   mariadb           "docker-entrypoint.s…"   2 hours ago   Up 2 hours   3306/tcp                                                                       mwj_mariadb_1
e1f1e6f5dd4e   redis:alpine      "docker-entrypoint.s…"   2 hours ago   Up 2 hours   6379/tcp                                                                       mwj_redis_1
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform# docker images
REPOSITORY        TAG          IMAGE ID       CREATED       SIZE
mwj_nginx         latest       17de390fa2d7   2 hours ago   42.2MB
mwj_android_pla   latest       fee44cdcedc3   2 hours ago   273MB
nginx             alpine       f246e6f9d0b2   2 weeks ago   23.5MB
redis             alpine       f934e82c14d1   3 weeks ago   28.4MB
mariadb           latest       ea81af801379   4 weeks ago   383MB
python            3.8-alpine   926e859df334   4 weeks ago   46.8MB
root@iZuf65o6jbmtrat2vx6hciZ:~/AndroidPlatform#

0 人点赞