Python 调用 Docker sdk

1、升级python3 (>3.8)和pip3

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、安装

pip3   install docker
pip3  install docker[tls]

3、使用默认套接字或环境中的配置连接到 Docker

import docker
client = docker.from_env()

4、远程连接Docker

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如下:

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 调用脚本

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) 

posted @ 2022-07-18 22:37  凡人半睁眼  阅读(178)  评论(0编辑  收藏  举报