Docker容器一键打包脚本

脚本内容

#!/bin/bash
if [[ $UID == 0 ]];then
        echo '开始打包...'
else
        echo '仅支持root用户'
        exit 1
fi
check_input(){
        if [[ -z "$1" ]];then
                echo -e "[$2]未输入"
                exit 1
        fi
}
check_status(){
        if [[ $? == 0 ]];then
                echo -e "${1}成功"
        else
                echo -e "${1}失败"
                exit 1
        fi
}
read -p "请输入容器id" con_id
check_input $con_id "容器ID"
read -p "请输入新的镜像名称[不允许大写]" img_name
check_input $img_name "镜像名称"
read -p "请输入镜像保存文件名[不允许特殊符号]" tar
check_input $tar "文件名"
docker commit $con_id $img_name
check_status "保存容器[$con_id]"
docker save -o $tar $img_name
check_status "保存镜像[$tar]"

执行效果

root@YY:~# docker ps -a
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS                    PORTS                                      NAMES
b8ae7ac42522        arm64v8/debian:latest        "bash"                   12 minutes ago      Up 12 minutes                                                        syslog
45e7c9fd4a03        debian10/apache2-ssl:v3.3    "bash"                   18 hours ago        Exited (0) 18 hours ago                                              web
cd3e7b827bab        debian10/apache2-ssl:v3.3    "bash"                   19 hours ago        Up 19 hours               0.0.0.0:81->80/tcp, 0.0.0.0:444->443/tcp   condescending_dirac
9cc933a39684        arm64v8/debian10_dvwa:v1.1   "/bin/bash"              40 hours ago        Up 18 hours                                                          dvwa
7e5e796a4e92        arm64v8/php                  "docker-php-entrypoi…"   2 weeks ago         Exited (0) 47 hours ago                                              php
4aeab4ddd6b0        c428dcf81d15                 "/bin/bash"              2 weeks ago         Exited (0) 2 weeks ago                                               share_ftp_liumou
root@YY:~# bash auto.sh 
开始打包...
请输入容器idb8ae7ac42522
请输入新的镜像名称[不允许大写]arm64v8/debian_syslog:v1.1
请输入镜像保存文件名[不允许特殊符号]docker_images_syslog.tar
sha256:5a6c4df0d3e6c5b28adbf4f8f8fbe290f4e5334b3b8785e9c505dacaf05aa582
保存容器[b8ae7ac42522]成功
保存镜像[docker_images_syslog.tar]成功
root@YY:~# ls
模板  auto.sh  code-oss_1.45.0-1586135971_arm64.deb  Desktop  docker_images_syslog.tar  Documents  Downloads  file.txt  http  list.txt  Music  Pictures  Videos
root@YY:~# 

你可能感兴趣的:(Linux,Shell,docker,docker,linux,shell)