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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use crate::DockerBuilder;
use crate::error::DockerError;
use bollard::exec::{CreateExecOptions, StartExecOptions};
use bollard::models::{NetworkCreateRequest, VolumeCreateOptions};
use bollard::query_parameters::{
CreateImageOptionsBuilder, InspectContainerOptions, ListNetworksOptions, ListVolumesOptions,
LogsOptionsBuilder, RemoveVolumeOptions,
};
use futures_util::{StreamExt, TryStreamExt};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time::sleep;
impl DockerBuilder {
/// Creates a network with extra creation settings
///
/// # Errors
///
/// Will return a `DockerError::BollardError` if the operation fails after the given number of retries
///
/// # Examples
/// ```no_run
/// use docktopus::DockerBuilder;
/// use std::collections::HashMap;
/// use std::time::Duration;
///
/// # async fn example(builder: DockerBuilder) -> Result<(), docktopus::DockerError> {
/// // Create a network with retries
/// let mut labels = HashMap::new();
/// labels.insert("env".to_string(), "prod".to_string());
///
/// builder
/// .create_network_with_retry("my-network", 3, Duration::from_secs(1), Some(labels))
/// .await?;
/// # Ok(()) }
/// ```
pub async fn create_network_with_retry(
&self,
name: &str,
max_retries: u32,
initial_delay: Duration,
labels: Option<HashMap<String, String>>,
) -> Result<(), DockerError> {
let mut delay = initial_delay;
let mut attempts = 0;
while attempts < max_retries {
let result = self
.client()
.create_network(NetworkCreateRequest {
name: name.to_string(),
driver: Some("bridge".to_string()),
labels: labels.clone(),
..Default::default()
})
.await;
match result {
Ok(_) => return Ok(()),
Err(e) => {
if attempts == max_retries - 1 {
return Err(DockerError::BollardError(e));
}
attempts += 1;
sleep(delay).await;
delay *= 2;
}
}
}
Ok(())
}
/// Removes a Docker network with the specified name
///
/// This method attempts to remove a Docker network by its name. It will fail if the network
/// does not exist or if there are containers still connected to it.
///
/// # Arguments
///
/// * `name` - Name of the network to remove
///
/// # Returns
///
/// Returns a `Result` containing unit `()` on success, or a `DockerError` if removal fails
///
/// # Errors
///
/// Will return a `DockerError::BollardError` if the operation fails
///
/// # Examples
///
/// ```rust,no_run
/// use docktopus::DockerBuilder;
///
/// # async fn example() -> Result<(), docktopus::DockerError> {
/// let builder = DockerBuilder::new().await?;
/// builder.remove_network("my-network").await?;
/// # Ok(()) }
/// ```
pub async fn remove_network(&self, name: &str) -> Result<(), DockerError> {
self.client()
.remove_network(name)
.await
.map_err(DockerError::BollardError)
}
/// Pulls a Docker image with optional platform specification
///
/// This method attempts to pull a Docker image from a registry. It supports specifying
/// a target platform for multi-architecture images.
///
/// # Arguments
///
/// * `image` - Name of the image to pull (e.g., "ubuntu:latest")
/// * `platform` - Optional platform specification (e.g., "linux/amd64", "linux/arm64")
///
/// # Returns
///
/// Returns a `Result` containing unit `()` on success, or a `DockerError` if the pull fails
///
/// # Errors
///
/// Will return a `DockerError::BollardError` if the pull fails
///
/// # Examples
///
/// ```rust,no_run
/// use docktopus::DockerBuilder;
///
/// # async fn example() -> Result<(), docktopus::DockerError> {
/// let builder = DockerBuilder::new().await?;
///
/// // Pull with default platform
/// builder.pull_image("ubuntu:latest", None).await?;
///
/// // Pull with specific platform
/// builder
/// .pull_image("ubuntu:latest", Some("linux/arm64"))
/// .await?;
/// # Ok(()) }
/// ```
pub async fn pull_image(&self, image: &str, platform: Option<&str>) -> Result<(), DockerError> {
let create_opts = CreateImageOptionsBuilder::default()
.from_image(image)
.platform(platform.unwrap_or(""))
.build();
let mut pull_stream = self.client.create_image(Some(create_opts), None, None);
while let Some(pull_result) = pull_stream.next().await {
if let Err(e) = pull_result {
return Err(DockerError::BollardError(e));
}
}
Ok(())
}
/// Lists all Docker networks
///
/// This method retrieves a list of all Docker networks present on the system.
///
/// # Returns
///
/// Returns a `Result` containing a `Vec<String>` of network names on success, or a `DockerError` if the operation fails
///
/// # Errors
///
/// Will return a `DockerError::BollardError` if the operation fails
///
/// # Examples
///
/// ```rust,no_run
/// use docktopus::DockerBuilder;
///
/// # async fn example() -> Result<(), docktopus::DockerError> {
/// let builder = DockerBuilder::new().await?;
/// let networks = builder.list_networks().await?;
/// for network in networks {
/// println!("Found network: {}", network);
/// }
/// # Ok(()) }
/// ```
pub async fn list_networks(&self) -> Result<Vec<String>, DockerError> {
let networks = self
.client()
.list_networks(None::<ListNetworksOptions>)
.await
.map_err(DockerError::BollardError)?;
Ok(networks.into_iter().filter_map(|n| n.name).collect())
}
/// Creates a Docker volume with the specified name
///
/// This method creates a new Docker volume with the given name using the local driver.
///
/// # Arguments
///
/// * `name` - Name to assign to the new volume
///
/// # Returns
///
/// Returns `Ok(())` on successful volume creation, or a `DockerError` if creation fails
///
/// # Errors
///
/// Will return a `DockerError::BollardError` if the operation fails
///
/// # Examples
///
/// ```rust,no_run
/// use docktopus::DockerBuilder;
///
/// # async fn example() -> Result<(), docktopus::DockerError> {
/// let builder = DockerBuilder::new().await?;
/// builder.create_volume("my_volume").await?;
/// # Ok(()) }
/// ```
pub async fn create_volume(&self, name: &str) -> Result<(), DockerError> {
self.client()
.create_volume(VolumeCreateOptions {
name: Some(name.to_string()),
driver: Some("local".to_string()),
..Default::default()
})
.await
.map_err(DockerError::BollardError)?;
Ok(())
}
/// Removes a Docker volume with the specified name
///
/// This method removes an existing Docker volume with the given name.
///
/// # Arguments
///
/// * `name` - Name of the volume to remove
///
/// # Returns
///
/// Returns `Ok(())` on successful volume removal, or a `DockerError` if removal fails
///
/// # Errors
///
/// Will return a `DockerError::BollardError` if the operation fails
///
/// # Examples
///
/// ```rust,no_run
/// use docktopus::DockerBuilder;
///
/// # async fn example() -> Result<(), docktopus::DockerError> {
/// let builder = DockerBuilder::new().await?;
/// builder.remove_volume("my_volume").await?;
/// # Ok(()) }
/// ```
pub async fn remove_volume(&self, name: &str) -> Result<(), DockerError> {
self.client()
.remove_volume(name, None::<RemoveVolumeOptions>)
.await
.map_err(DockerError::BollardError)
}
/// Lists all Docker volumes with optional filters
///
/// This method retrieves a list of all Docker volumes on the system, with optional filtering
/// capabilities.
///
/// # Returns
///
/// Returns a `Result` containing a vector of volume names as strings, or a `DockerError` if the operation fails
///
/// # Errors
///
/// Will return a `DockerError::BollardError` if the operation fails
///
/// # Examples
///
/// ```rust,no_run
/// use docktopus::DockerBuilder;
///
/// # async fn example() -> Result<(), docktopus::DockerError> {
/// let builder = DockerBuilder::new().await?;
/// let volumes = builder.list_volumes().await?;
/// for volume in volumes {
/// println!("Found volume: {}", volume);
/// }
/// # Ok(()) }
/// ```
pub async fn list_volumes(&self) -> Result<Vec<String>, DockerError> {
let volumes = self
.client()
.list_volumes(None::<ListVolumesOptions>)
.await
.map_err(DockerError::BollardError)?;
Ok(volumes
.volumes
.unwrap_or_default()
.into_iter()
.map(|v| v.name)
.collect())
}
/// Waits for a container to be in a running state
///
/// This method polls the container status until it is running or the maximum number of retries
/// is reached. It will retry up to 5 times with a 500ms delay between attempts.
///
/// # Arguments
///
/// * `container_id` - ID of the container to wait for
///
/// # Returns
///
/// Returns `Ok(())` if the container is running, or a `DockerError` if the container fails to start
/// after maximum retries.
///
/// # Errors
///
/// * Unable to inspect the container
/// * The container is not running after 5 retries
///
/// # Examples
///
/// ```rust,no_run
/// use docktopus::DockerBuilder;
///
/// # async fn example() -> Result<(), docktopus::DockerError> {
/// let builder = DockerBuilder::new().await?;
/// builder.wait_for_container("container_id").await?;
/// # Ok(()) }
/// ```
pub async fn wait_for_container(&self, container_id: &str) -> Result<(), DockerError> {
let mut retries = 5;
while retries > 0 {
let inspect = self
.client()
.inspect_container(container_id, None::<InspectContainerOptions>)
.await
.map_err(DockerError::BollardError)?;
if let Some(state) = inspect.state {
if let Some(running) = state.running {
if running {
return Ok(());
}
}
}
sleep(Duration::from_millis(500)).await;
retries -= 1;
}
Err(DockerError::ValidationError(format!(
"Container {} not running after retries",
container_id
)))
}
/// Retrieves logs from a Docker container
///
/// This method fetches both stdout and stderr logs from the specified container with timestamps.
/// The logs are returned as a single string with each log line separated by newlines.
///
/// # Arguments
///
/// * `container_id` - ID of the container to get logs from
///
/// # Returns
///
/// Returns `Ok(String)` containing the container logs, or a `DockerError` if there was an error
/// retrieving the logs.
///
/// # Errors
///
/// Will return a `DockerError::BollardError` if the operation fails
///
/// # Examples
///
/// ```rust,no_run
/// use docktopus::DockerBuilder;
///
/// # async fn example() -> Result<(), docktopus::DockerError> {
/// let builder = DockerBuilder::new().await?;
/// let logs = builder.get_container_logs("container_id").await?;
/// println!("Container logs: {}", logs);
/// # Ok(()) }
/// ```
pub async fn get_container_logs(&self, container_id: &str) -> Result<String, DockerError> {
let mut output = String::new();
let logs_opts = LogsOptionsBuilder::default()
.stdout(true)
.stderr(true)
.timestamps(true)
.follow(false)
.tail("all")
.build();
let mut stream = self.client().logs(container_id, Some(logs_opts));
while let Some(log) = stream.try_next().await.map_err(DockerError::BollardError)? {
output.push_str(&log.to_string());
output.push('\n');
}
Ok(output)
}
/// Executes a command inside a Docker container
///
/// This method executes the specified command inside the specified container and returns the
/// output as a string.
///
/// # Arguments
///
/// * `container_id` - ID of the container to execute the command in
/// * `cmd` - Command to execute in the container
/// * `env` - Environment variables to set in the container
///
/// # Errors
///
/// Will return a `DockerError::BollardError` if the operation fails
pub async fn exec_in_container(
&self,
container_id: &str,
cmd: Vec<&str>,
env: Option<HashMap<String, String>>,
) -> Result<String, DockerError> {
let exec = self
.client()
.create_exec(
container_id,
CreateExecOptions::<String> {
attach_stdout: Some(true),
attach_stderr: Some(true),
cmd: Some(cmd.into_iter().map(ToString::to_string).collect()),
env: env.map(|e| e.into_iter().map(|(k, v)| format!("{}={}", k, v)).collect()),
..Default::default()
},
)
.await
.map_err(DockerError::BollardError)?;
let output = self
.client()
.start_exec(&exec.id, None::<StartExecOptions>)
.await
.map_err(DockerError::BollardError)?;
match output {
bollard::exec::StartExecResults::Attached { mut output, .. } => {
let mut bytes = Vec::new();
while let Some(chunk) =
output.try_next().await.map_err(DockerError::BollardError)?
{
bytes.extend_from_slice(&chunk.into_bytes());
}
Ok(String::from_utf8_lossy(&bytes).into_owned())
}
_ => Ok(String::new()),
}
}
}