美文网首页
docker操作中几个常用shell脚本

docker操作中几个常用shell脚本

作者: 我的宠物 | 来源:发表于2018-05-05 15:15 被阅读1349次

    一、批量构建镜像

    buildDockerImages.sh

    #!/usr/bin/env bash
    
    echo "==================== start build docker images =========================="
    cd /www/docker/images/
    old_IFS=$IFS
    IFS=','
    cmds=(
       'docker build -t xxx-base-env .',
       'docker build -t redis:3.2 -f redis.dockerfile .',
       'docker build -t mysql:5.6 -f mysql.dockerfile .',
       'docker build -t elasticsearch:5.6.5 -f es.dockerfile .',
       'docker build -t openresty -f openresty.dockerfile .'
    )
    for cmd in ${cmds[*]};do
         eval "$cmd"
    done
    IFS=$old_IFS
    echo "==================== end build docker images    =========================="
    

    二、批量删除无用的镜像,保留有用的镜像

    clearDockerImages.sh

    #!/usr/bin/env bash
    echo "==================== start clean docker images  =========================="
    docker images | awk '
        NR>1{
            if($1 == "xxx-base-env" ||
                $1 == "mysql:5.6" ||
                $1 == "mysql:5.7" ||
                $1 == "redis:3.2" ||
                $1 == "elasticsearch:5.6.5" ||
                $1 == "openresty" ||
                $1 == "monitoringartist/zabbix-xxl:3.2.6")
               {
                    print "skiped ..."
               }else{
                    system("docker rmi " $3)
               }
        }
    ';
    echo "==================== end clean docker images    =========================="
    

    三、批量删除docker容器的log

    clearDockerContainerLogs.sh

    #!/usr/bin/env bash
    echo "==================== start clean docker containers logs =========================="
    logs=$(find /var/lib/docker/containers/ -name *-json.log)
    for log in $logs
        do
            echo "clean logs : $log"
            cat /dev/null > $log
        done
    echo "==================== end clean docker containers logs   =========================="
    

    相关文章

      网友评论

          本文标题:docker操作中几个常用shell脚本

          本文链接:https://www.haomeiwen.com/subject/xlxerftx.html