forked from kubeflow/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.py
More file actions
257 lines (224 loc) · 8.6 KB
/
docker.py
File metadata and controls
257 lines (224 loc) · 8.6 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Copyright 2025 The Kubeflow Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Docker client adapter implementation.
This module provides the DockerClientAdapter class that implements the
BaseContainerClientAdapter interface for Docker runtime.
"""
from collections.abc import Iterator
from typing import Optional
from kubeflow.trainer.backends.container.adapters.base import BaseContainerClientAdapter
class DockerClientAdapter(BaseContainerClientAdapter):
"""Adapter for Docker client."""
def __init__(self, host: Optional[str] = None):
"""
Initialize Docker client.
Args:
host: Docker host URL, or None to use environment defaults
"""
try:
import docker # type: ignore
except ImportError as e:
raise ImportError(
"The 'docker' Python package is not installed. Install with extras: "
"pip install kubeflow[docker]"
) from e
if host:
self.client = docker.DockerClient(base_url=host)
else:
self.client = docker.from_env()
self._runtime_type = "docker"
def ping(self):
"""Test connection to Docker daemon."""
self.client.ping()
def create_network(self, name: str, labels: dict[str, str]) -> str:
"""Create a Docker network."""
try:
self.client.networks.get(name)
return name
except Exception:
pass
self.client.networks.create(
name=name,
check_duplicate=True,
labels=labels,
)
return name
def delete_network(self, network_id: str):
"""Delete Docker network."""
try:
net = self.client.networks.get(network_id)
net.remove()
except Exception:
pass
def create_and_start_container(
self,
image: str,
command: list[str],
name: str,
network_id: str,
environment: dict[str, str],
labels: dict[str, str],
volumes: dict[str, dict[str, str]],
working_dir: str,
) -> str:
"""Create and start a Docker container."""
container = self.client.containers.run(
image=image,
command=tuple(command),
name=name,
detach=True,
working_dir=working_dir,
network=network_id,
environment=environment,
labels=labels,
volumes=volumes,
auto_remove=False,
)
return container.id
def get_container(self, container_id: str):
"""Get Docker container by ID."""
return self.client.containers.get(container_id)
def container_logs(self, container_id: str, follow: bool) -> Iterator[str]:
"""Stream logs from Docker container."""
container = self.get_container(container_id)
logs = container.logs(stream=bool(follow), follow=bool(follow))
if follow:
for chunk in logs:
if isinstance(chunk, bytes):
yield chunk.decode("utf-8", errors="ignore")
else:
yield str(chunk)
else:
if isinstance(logs, bytes):
yield logs.decode("utf-8", errors="ignore")
else:
yield str(logs)
def stop_container(self, container_id: str, timeout: int = 10):
"""Stop Docker container."""
container = self.get_container(container_id)
container.stop(timeout=timeout)
def remove_container(self, container_id: str, force: bool = True):
"""Remove Docker container."""
container = self.get_container(container_id)
container.remove(force=force)
def pull_image(self, image: str):
"""Pull Docker image."""
self.client.images.pull(image)
def image_exists(self, image: str) -> bool:
"""Check if Docker image exists locally."""
try:
self.client.images.get(image)
return True
except Exception:
return False
def run_oneoff_container(self, image: str, command: list[str]) -> str:
"""Run a short-lived Docker container and return output."""
try:
output = self.client.containers.run(
image=image,
command=tuple(command),
detach=False,
remove=True,
)
if isinstance(output, (bytes, bytearray)):
return output.decode("utf-8", errors="ignore")
return str(output)
except Exception as e:
raise RuntimeError(f"One-off container failed to run: {e}") from e
def container_status(self, container_id: str) -> tuple[str, Optional[int]]:
"""Get Docker container status."""
try:
container = self.get_container(container_id)
status = container.status
# Get exit code if container has exited
exit_code = None
if status == "exited":
inspect = container.attrs if hasattr(container, "attrs") else container.inspect()
exit_code = inspect.get("State", {}).get("ExitCode")
return (status, exit_code)
except Exception:
return ("unknown", None)
def get_container_ip(self, container_id: str, network_id: str) -> Optional[str]:
"""Get container's IP address on a specific network."""
try:
container = self.get_container(container_id)
# Refresh container info
container.reload()
# Get network settings
networks = container.attrs.get("NetworkSettings", {}).get("Networks", {})
# Try to find the network by exact name or ID
if network_id in networks:
return networks[network_id].get("IPAddress")
# Fallback: return first available IP
for _net_name, net_info in networks.items():
ip = net_info.get("IPAddress")
if ip:
return ip
return None
except Exception:
return None
def list_containers(self, filters: Optional[dict[str, list[str]]] = None) -> list[dict]:
"""List Docker containers with optional filters."""
try:
containers = self.client.containers.list(all=True, filters=filters)
result = []
for c in containers:
result.append(
{
"id": c.id,
"name": c.name,
"labels": c.labels,
"status": c.status,
"created": c.attrs.get("Created", ""),
}
)
return result
except Exception:
return []
def get_network(self, network_id: str) -> Optional[dict]:
"""Get Docker network information."""
try:
network = self.client.networks.get(network_id)
return {
"id": network.id,
"name": network.name,
"labels": network.attrs.get("Labels", {}),
}
except Exception:
return None
def wait_for_container(self, container_id: str, timeout: Optional[int] = None) -> int:
"""
Wait for a Docker container to exit and return its exit code.
Args:
container_id: Container ID
timeout: Maximum time to wait in seconds, or None to wait indefinitely
Returns:
Container exit code
Raises:
TimeoutError: If timeout is reached before container exits
"""
try:
container = self.get_container(container_id)
result = container.wait(timeout=timeout)
# Docker wait() returns a dict with 'StatusCode' key
if isinstance(result, dict):
return result.get("StatusCode", 0)
return int(result)
except Exception as e:
if "timeout" in str(e).lower():
raise TimeoutError(
f"Container {container_id} did not exit within {timeout} seconds"
) from e
raise