网站开发前后端分工,电子政务 和网站建设总结,vultr 一键wordpress,微信小程序开发软件运行脚本后#xff0c;界面如下#xff1a; 选择对应的编号后#xff0c;会列举所有关闭的容器或者所有开启的容器列表#xff0c;当我要启动一个容器 时输入1#xff0c;就会出现下面的页面。 然后输入指定的编号后#xff0c;就会启动对应的容器。
脚本代码如下#…运行脚本后界面如下 选择对应的编号后会列举所有关闭的容器或者所有开启的容器列表当我要启动一个容器 时输入1就会出现下面的页面。 然后输入指定的编号后就会启动对应的容器。
脚本代码如下
#!/bin/bashfunction show_running_containers() {runningContainers$(docker ps --format table {{.ID}}\t{{.Names}} | awk NR1 | nl)echo 运行中的容器echo $runningContainers
}function show_stopped_containers() {stoppedContainers$(docker ps -a --filter statusexited --format table {{.ID}}\t{{.Names}} | awk NR1 | nl)echo 关闭的容器echo $stoppedContainers
}function start_container() {show_stopped_containersread -p 请输入要启动的容器编号输入0退出: userInputif [ $userInput -eq 0 ]; thenecho 退出脚本elsecontainerId$(docker ps -a --filter statusexited -q | sed -n ${userInput}p)if [ -n $containerId ]; thenecho 正在启动容器$containerIdif docker start $containerId /dev/null; thenecho 容器启动成功elseecho 容器启动失败原因$(docker inspect -f {{.State.Status}} $containerId)fielseecho 输入无效未找到对应编号的容器fifi
}function stop_container() {show_running_containersread -p 请输入要关闭的容器编号输入0退出: userInputif [ $userInput -eq 0 ]; thenecho 退出脚本elsecontainerId$(docker ps -q | sed -n ${userInput}p)if [ -n $containerId ]; thenecho 正在关闭容器$containerIdif docker stop $containerId /dev/null; thenecho 容器关闭成功elseecho 容器关闭失败原因$(docker inspect -f {{.State.Status}} $containerId)fielseecho 输入无效未找到对应编号的容器fifi
}function restart_container() {show_running_containersread -p 请输入要重启的容器编号输入0退出: userInputif [ $userInput -eq 0 ]; thenecho 退出脚本elsecontainerId$(docker ps -q | sed -n ${userInput}p)if [ -n $containerId ]; thenecho 正在重启容器$containerIdif docker restart $containerId /dev/null; thenecho 容器重启成功elseecho 容器重启失败原因$(docker inspect -f {{.State.Status}} $containerId)fielseecho 输入无效未找到对应编号的容器fifi
}function force_delete_container() {show_running_containersread -p 请输入要强制删除的容器编号输入0退出: userInputif [ $userInput -eq 0 ]; thenecho 退出脚本elsecontainerId$(docker ps -q | sed -n ${userInput}p)if [ -n $containerId ]; thenecho 正在强制删除容器$containerIdif docker rm -f $containerId /dev/null; thenecho 容器删除成功elseecho 容器删除失败fielseecho 输入无效未找到对应编号的容器fifi
}while true; doecho 请选择操作echo 1. 启动容器echo 2. 关闭容器echo 3. 重启容器echo 4. 强制删除容器echo 0. 退出read -p 请输入操作编号: choicecase $choice in1)start_container;;2)stop_container;;3)restart_container;;4)force_delete_container;;0)echo 退出脚本break;;*)echo 输入无效请重新输入;;esac
done将代码写进一个sh文件。然后启动后即可使用。
点点关注点点赞呀。持续更新有用的知识................................................