Python 调用 Docker sdk
1、升级python3 (>3.8)和pip3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel wget http: //npm.taobao.org/mirrors/python/3.9.0/Python-3.9.0.tgz mv Python-3.9.0.tgz /usr/local/ tar -zxvf Python-3.9.0.tgz cd python-3.9.0目录 ./configure prefix=/usr/local/python3 make && make install ln -s /usr/local/python3/bin/python3.9 /usr/bin/python3 ln -s /usr/local/python3/bin/pip3.9 /usr/bin/pip3 |
2、安装
1 2 | pip3 install docker pip3 install docker[tls] |
3、使用默认套接字或环境中的配置连接到 Docker
1 2 | import docker client = docker.from_env() |
4、远程连接Docker
1 2 3 4 5 6 7 8 | vim /lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd -H fd: // --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:8088 -H unix:///var/run/docker.sock systemctl daemon-reload service docker restart import docker client = docker.APIClient(base_url= 'tcp://ip:8088' ) #这个ip是指上面操作中配置了远程访问功能docker的机器ip |
5、包含的key如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | key: Id key: Created key: Path key: Args key: State key: Image key: ResolvConfPath key: HostnamePath key: HostsPath key: LogPath key: Name key: RestartCount key: Driver key: Platform key: MountLabel key: ProcessLabel key: AppArmorProfile key: ExecIDs key: HostConfig key: GraphDriver key: Mounts key: Config key: NetworkSettings |
6、python 调用脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import docker import os ##获取认证信息 client = docker.from_env() #client = docker.APIClient(base_url='tcp://10.1.1.145:8088') #这个ip是指上面操作中配置了远程访问功能docker的机器ip def get_containers_info(con): print( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' ) ##指定某个具体的容器 container = client.containers. get (con) ##容器名 name=container.attrs[ 'Name' ] print( '容器名和CONTAINER ID:' ,name,container.short_id,end= '\n\n' ) ##容器使用的镜像 print( '镜像:' ,container.attrs[ 'Config' ][ 'Image' ],end= '\n\n' ) ##容器的网络类型 a=container.attrs[ 'HostConfig' ][ 'NetworkMode' ] print( '网络类型为:' , a,end= '\n\n' ) ##端口暴露 g=container.attrs[ 'HostConfig' ][ 'PortBindings' ] print( '端口:' ,g,end= '\n\n' ) ##容器的ip地址 #result = os.popen('docker inspect 2ce81c9cd060 | grep -w IPAddress | sed -n 2p | xargs | sed s/,//') #print(result.read()) ip=container.attrs[ 'NetworkSettings' ][ 'Networks' ] #list(ip.keys())[0]['IPAddress'] #print(list(ip.keys())[0]) v=list(ip.values())[0] print( 'ip 地址:' ,v[ 'IPAddress' ],end= '\n\n' ) c=container.attrs[ 'State' ][ 'Status' ] print( '容器状态:' ,c,end= '\n\n' ) ##存储驱动 e=container.attrs[ 'Driver' ] print( '存储驱动:' ,e,end= '\n\n' ) ##文件路径挂载 f=container.attrs[ 'Mounts' ] print( '挂载情况:' ,f,end= '\n\n' ) print( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' ) list1 = os.popen( 'docker ps -q' ).read().rstrip( '\n' ).split( '\n' ) for i in list1: get_containers_info(i) |
注:
rstrip('\n') 是删除最后一行的空格
split('\n') 以换行符为切割
7、运行效果(虚拟机测试)
GitHub - docker/docker-py: A Python library for the Docker Engine API
Containers — Docker SDK for Python 5.0.3 documentation (docker-py.readthedocs.io)