Python 中 docker SDK 的使用
官网:https://docker-py.readthedocs.io/en/stable/client.html
安装模块
1 | pip3 install docker -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com |
修改 Docker 配置文件
1 | 登录docker所在服务器,修改docker.service文件 |
Docker SDK 常用方法
build 构建 image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import docker
# 创建 Docker 客户端
# client = docker.from_env()
client = docker.DockerClient(base_url="tcp://192.168.40.11:2375")
# 构建镜像
image, logs = client.images.build(
path="path/to/your/dockerfile/directory",
tag="your-image-name",
rm=True, # 是否在构建完成后删除中间容器
)
# 打印构建日志
for line in logs:
print(line)push 推送 image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19import docker
# 创建 Docker 客户端
# client = docker.from_env()
client = docker.DockerClient(base_url='tcp://192.168.40.11:2375')
# 镜像名称
image_name = 'your-image-name'
# 登录到 Docker 镜像仓库(例如 Docker Hub)
res_dict = client.login(
username='your-username',
password='your-password',
registry='192.168.40.11:8083'
)
# 推送镜像
for line in client.images.push(image_name, stream=True, decode=True):
print(line)rm 删除 image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19import docker
# 创建 Docker 客户端
# client = docker.from_env()
client = docker.DockerClient(base_url='tcp://192.168.40.11:2375')
# 镜像名称
image_name = 'your-image-name'
# 删除镜像
try:
client.images.remove(image_name, force=True)
print(f"Image '{image_name}' removed successfuly.")
except docker.errors.ImageNotFound:
print(f"Image '{image_name}' not found.")
except docker.errors.APIError as e:
print(f"Error occurred while removing image '{image_name}': {e}")