Docker小工具
原由
- 由于最近程序选封装成docker,后需要查看日志和进入和重启容器,需要知道dockerID 或者容器名称,还需要鼠标记录复制,避免麻烦,才触发该脚本
源代码
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import docker,os
client = docker.from_env()
docker_containerid_list = []
docker_containerName_list = []
docker_index = 0
print("Please use the root user")
print("Current Docker Container Running List: ")
print("=========================================================")
for container in client.containers.list():
#print(dir(container))
container_id = str(container.id)[0:11]
container_name = str(container.name)
#print("docker index: ",str(docker_index)," ",container.name," ",container_id," ",container.ports)
print("docker index: \033[1;31m" + str(docker_index) + "\033[0m"," \033[1;35m",container.name + "\033[0m")
docker_containerid_list.append(container_id)
docker_containerName_list.append(container_name)
docker_index += 1
print("=========================================================")
#进入容器
def Indocker(dockerId):
shell = "docker exec -it " + dockerId + " bash"
print("get into docker: \033[1;32m" + docker_containerName_list[int(num)]+"\033[0m")
os.system(shell)
#dokcer log
def dockerlog(dockerId):
shell = "docker logs -f " + dockerId
print("docker logs: \033[1;32m" + docker_containerName_list[int(num)] + "\033[0m")
os.system(shell)
print("Please enter the required operation options:")
print("\033[1;31m1\033[0m、restart docker")
print("\033[1;31m2\033[0m、get into docker")
print("\033[1;31m3\033[0m、tailf docker log")
print("=========================================================")
Operationobject = input()
if int(Operationobject) == 1:
print("Please enter the Docker index number to enter the Docker")
num = input()
dockerid = docker_containerid_list[int(num)]
print("restart docker: \033[1;32m" + docker_containerName_list[int(num)] + "\033[0m")
container = client.containers.get(dockerid)
container.restart()
elif int(Operationobject) == 2:
print("Please enter the Docker index number to enter the Docker")
num = input()
try:
dockerid = docker_containerid_list[int(num)]
Indocker(dockerid)
except IndexError:
print("inputdocker index num error,exit!!!!")
elif int(Operationobject) == 3:
print("Please enter the Docker index number to enter the Docker")
num = input()
try:
dockerid = docker_containerid_list[int(num)]
dockerlog(dockerid)
except IndexError:
print("inputdocker index num error,exit!!!!")
else:
print("\033[1;31minput number error exit!!! \033[0m")
使用
- 自行把该脚本封装成二进制文件然后上传到需要使用的服务器的/usr/sbin下面
- 需要使用root用户进行运行,进入和删除容器需要root用户
[root@abchina ~]# SelectDocker
Please use the root user
Current Docker Container Running List:
=========================================================
docker index: 0 mysql-test
=========================================================
Please enter the required operation options:
1、restart docker
2、get into docker
3、tailf docker log
=========================================================
2
Please enter the Docker index number to enter the Docker
0
get into docker: mysql-test
bash-4.4# exit
[root@my-dev SelectDocker]# ./SelectDocker.py
Please use the root user
Current Docker Container Running List:
=========================================================
docker index: 0 mysql-test
=========================================================
Please enter the required operation options:
1、restart docker
2、get into docker
3、tailf docker log
=========================================================
3
Please enter the Docker index number to enter the Docker
0
docker logs: mysql-test
2022-11-17T16:30:15.678060Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.31).
下载
wget https://zhao138969.com/LinuxPackage/Python/SelectDocker
评论区