-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun.rs
More file actions
2998 lines (2642 loc) · 100 KB
/
run.rs
File metadata and controls
2998 lines (2642 loc) · 100 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Docker run command implementation.
//!
//! This module provides a comprehensive implementation of the `docker run` command
//! with support for common options and an extensible architecture for any additional options.
use super::{CommandExecutor, DockerCommand, EnvironmentBuilder, PortBuilder};
use crate::command::port::{PortCommand, PortMapping as PortMappingInfo};
use crate::error::{Error, Result};
use crate::stream::{OutputLine, StreamResult, StreamableCommand};
use async_trait::async_trait;
use std::path::PathBuf;
use tokio::process::Command as TokioCommand;
use tokio::sync::mpsc;
/// Docker run command builder with fluent API
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)]
pub struct RunCommand {
/// The Docker image to run
image: String,
/// Command executor for extensibility
pub executor: CommandExecutor,
/// Container name
name: Option<String>,
/// Run in detached mode
detach: bool,
/// Environment variables
environment: EnvironmentBuilder,
/// Port mappings
ports: PortBuilder,
/// Volume mounts
volumes: Vec<VolumeMount>,
/// Working directory
workdir: Option<PathBuf>,
/// Entrypoint override
entrypoint: Option<String>,
/// Command to run in container
command: Option<Vec<String>>,
/// Interactive mode
interactive: bool,
/// Allocate TTY
tty: bool,
/// Remove container on exit
remove: bool,
// Resource Limits
/// Memory limit
memory: Option<String>,
/// Number of CPUs
cpus: Option<String>,
/// CPU shares (relative weight)
cpu_shares: Option<i64>,
/// CPU CFS period
cpu_period: Option<i64>,
/// CPU CFS quota
cpu_quota: Option<i64>,
/// CPUs in which to allow execution
cpuset_cpus: Option<String>,
/// MEMs in which to allow execution
cpuset_mems: Option<String>,
/// Memory + swap limit
memory_swap: Option<String>,
/// Memory soft limit
memory_reservation: Option<String>,
// Security & User Context
/// Username or UID
user: Option<String>,
/// Give extended privileges
privileged: bool,
/// Container host name
hostname: Option<String>,
// Lifecycle Management
/// Restart policy
restart: Option<String>,
// System Integration
/// Set platform if server is multi-platform capable
platform: Option<String>,
/// Runtime to use for this container
runtime: Option<String>,
/// Container isolation technology
isolation: Option<String>,
/// Pull image before running
pull: Option<String>,
/// Write the container ID to the file
cidfile: Option<String>,
/// Container NIS domain name
domainname: Option<String>,
/// Container MAC address
mac_address: Option<String>,
// Logging & Drivers
/// Logging driver for the container
log_driver: Option<String>,
/// Optional volume driver for the container
volume_driver: Option<String>,
// Namespaces
/// User namespace to use
userns: Option<String>,
/// UTS namespace to use
uts: Option<String>,
/// PID namespace to use
pid: Option<String>,
/// IPC mode to use
ipc: Option<String>,
/// Cgroup namespace to use
cgroupns: Option<String>,
/// Optional parent cgroup for the container
cgroup_parent: Option<String>,
// Advanced Memory & Performance
/// Kernel memory limit
kernel_memory: Option<String>,
/// Tune container memory swappiness (0 to 100)
memory_swappiness: Option<i32>,
/// Tune host's OOM preferences (-1000 to 1000)
oom_score_adj: Option<i32>,
/// Tune container pids limit
pids_limit: Option<i64>,
/// Size of /dev/shm
shm_size: Option<String>,
// Process Control
/// Signal to stop the container
stop_signal: Option<String>,
/// Timeout (in seconds) to stop a container
stop_timeout: Option<i32>,
/// Override the key sequence for detaching a container
detach_keys: Option<String>,
// Simple Flags
/// Proxy received signals to the process
sig_proxy: bool,
/// Mount the container's root filesystem as read only
read_only: bool,
/// Run an init inside the container
init: bool,
/// Disable OOM Killer
oom_kill_disable: bool,
/// Disable any container-specified HEALTHCHECK
no_healthcheck: bool,
/// Skip image verification
disable_content_trust: bool,
/// Publish all exposed ports to random ports
publish_all: bool,
/// Suppress the pull output
quiet: bool,
// High-Impact List Options
// DNS & Network
/// Custom DNS servers
dns: Vec<String>,
/// DNS options
dns_option: Vec<String>,
/// DNS search domains
dns_search: Vec<String>,
/// Add host-to-IP mappings (host:ip)
add_host: Vec<String>,
// Security & Capabilities
/// Add Linux capabilities
cap_add: Vec<String>,
/// Drop Linux capabilities
cap_drop: Vec<String>,
/// Security options
security_opt: Vec<String>,
// Device & Filesystem
/// Add host devices to container
device: Vec<String>,
/// Mount tmpfs directories
tmpfs: Vec<String>,
/// Expose ports without publishing them
expose: Vec<String>,
// Environment & Labels
/// Read environment from files
env_file: Vec<PathBuf>,
/// Set metadata labels
label: Vec<String>,
/// Read labels from files
label_file: Vec<PathBuf>,
// Additional List/Vec Options
/// Network aliases for the container
network_alias: Vec<String>,
/// Additional groups for the user
group_add: Vec<String>,
/// Attach to STDIN, STDOUT or STDERR
attach: Vec<String>,
/// Log driver options
log_opt: Vec<String>,
/// Storage driver options
storage_opt: Vec<String>,
/// Ulimit options
ulimit: Vec<String>,
/// Mount volumes from other containers
volumes_from: Vec<String>,
/// Add link to another container (deprecated)
link: Vec<String>,
/// Container IPv4/IPv6 link-local addresses
link_local_ip: Vec<String>,
// Health Check Options
// Health checks
/// Command to run to check health
health_cmd: Option<String>,
/// Time between running the check (ms|s|m|h)
health_interval: Option<String>,
/// Consecutive failures needed to report unhealthy
health_retries: Option<i32>,
/// Maximum time to allow one check to run (ms|s|m|h)
health_timeout: Option<String>,
/// Start period for the container to initialize before health-checking (ms|s|m|h)
health_start_period: Option<String>,
/// Time between health checks during the start period (ms|s|m|h)
health_start_interval: Option<String>,
// Advanced options
/// Advanced mount configuration
mount: Vec<String>,
/// Connect to a network
network: Vec<String>,
/// GPU devices to add to the container
gpus: Option<String>,
// Map-based options (stored as Vec<String> in key=value format)
/// Add custom annotations
annotation: Vec<String>,
/// Kernel parameters to set
sysctl: Vec<String>,
// Advanced System Options
// Block I/O controls
/// Block IO weight (relative weight)
blkio_weight: Option<u16>,
/// Block IO weight per device
blkio_weight_device: Vec<String>,
/// Limit read rate (bytes per second) from a device
device_read_bps: Vec<String>,
/// Limit write rate (bytes per second) to a device
device_write_bps: Vec<String>,
/// Limit read rate (IO per second) from a device
device_read_iops: Vec<String>,
/// Limit write rate (IO per second) to a device
device_write_iops: Vec<String>,
// Real-time CPU scheduling
/// Limit CPU real-time period in microseconds
cpu_rt_period: Option<i64>,
/// Limit CPU real-time runtime in microseconds
cpu_rt_runtime: Option<i64>,
// Advanced networking
/// Container IPv4 address
ip: Option<String>,
/// Container IPv6 address
ip6: Option<String>,
// Advanced system options
/// Cgroup rule for devices
device_cgroup_rule: Vec<String>,
}
/// Volume mount configuration
#[derive(Debug, Clone)]
pub struct VolumeMount {
/// Source path on host or volume name
pub source: String,
/// Target path in container
pub target: String,
/// Mount type (bind, volume, tmpfs)
pub mount_type: MountType,
/// Read-only mount
pub readonly: bool,
}
/// Type of volume mount
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MountType {
/// Bind mount from host filesystem
Bind,
/// Named volume
Volume,
/// Temporary filesystem
Tmpfs,
}
impl std::fmt::Display for VolumeMount {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let readonly_suffix = if self.readonly { ":ro" } else { "" };
write!(f, "{}:{}{}", self.source, self.target, readonly_suffix)
}
}
/// Container ID returned by docker run
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContainerId(pub String);
impl ContainerId {
/// Get the container ID as a string
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
/// Get the short form of the container ID (first 12 characters)
///
/// Returns the first 12 characters of the container ID, or the full ID
/// if it's shorter than 12 characters. This method is Unicode-safe.
#[must_use]
pub fn short(&self) -> &str {
// Find the byte index of the 12th character (or end of string)
let end_idx = self
.0
.char_indices()
.nth(12)
.map_or(self.0.len(), |(idx, _)| idx);
&self.0[..end_idx]
}
/// Get port mappings for this container
///
/// This queries Docker for the actual mapped ports of the running container.
/// Useful when using dynamic port allocation (e.g., `-p 6379` without specifying host port).
///
/// # Example
///
/// ```no_run
/// use docker_wrapper::{DockerCommand, RunCommand};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Run Redis with dynamic port allocation
/// let container_id = RunCommand::new("redis:alpine")
/// .name("my-redis")
/// .port_dyn(6379) // Dynamic port allocation
/// .detach()
/// .rm()
/// .execute()
/// .await?;
///
/// // Get the actual mapped port
/// let port_mappings = container_id.port_mappings().await?;
/// if let Some(mapping) = port_mappings.first() {
/// println!("Redis is available at {}:{}", mapping.host_ip, mapping.host_port);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The container doesn't exist or has been removed
/// - The Docker daemon is not running
/// - There's a communication error with Docker
pub async fn port_mappings(&self) -> Result<Vec<PortMappingInfo>> {
let result = PortCommand::new(&self.0).run().await?;
Ok(result.port_mappings)
}
/// Get a specific port mapping for this container
///
/// # Example
///
/// ```no_run
/// use docker_wrapper::{DockerCommand, RunCommand};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let container_id = RunCommand::new("nginx:alpine")
/// .port_dyn(80)
/// .detach()
/// .rm()
/// .execute()
/// .await?;
///
/// // Get the mapping for port 80
/// if let Some(mapping) = container_id.port_mapping(80).await? {
/// println!("Nginx is available at {}:{}", mapping.host_ip, mapping.host_port);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The container doesn't exist or has been removed
/// - The Docker daemon is not running
/// - There's a communication error with Docker
pub async fn port_mapping(&self, container_port: u16) -> Result<Option<PortMappingInfo>> {
let result = PortCommand::new(&self.0).port(container_port).run().await?;
Ok(result.port_mappings.into_iter().next())
}
}
impl std::fmt::Display for ContainerId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl RunCommand {
/// Create a new run command for the specified image
#[allow(clippy::too_many_lines)]
pub fn new(image: impl Into<String>) -> Self {
Self {
image: image.into(),
executor: CommandExecutor::new(),
name: None,
detach: false,
environment: EnvironmentBuilder::new(),
ports: PortBuilder::new(),
volumes: Vec::new(),
workdir: None,
entrypoint: None,
command: None,
interactive: false,
tty: false,
remove: false,
// Resource Limits
memory: None,
cpus: None,
cpu_shares: None,
cpu_period: None,
cpu_quota: None,
cpuset_cpus: None,
cpuset_mems: None,
memory_swap: None,
memory_reservation: None,
// Security & User Context
user: None,
privileged: false,
hostname: None,
// Lifecycle Management
restart: None,
// System Integration
platform: None,
runtime: None,
isolation: None,
pull: None,
cidfile: None,
domainname: None,
mac_address: None,
// Logging & Drivers
log_driver: None,
volume_driver: None,
// Namespaces
userns: None,
uts: None,
pid: None,
ipc: None,
cgroupns: None,
cgroup_parent: None,
// Advanced Memory & Performance
kernel_memory: None,
memory_swappiness: None,
oom_score_adj: None,
pids_limit: None,
shm_size: None,
// Process Control
stop_signal: None,
stop_timeout: None,
detach_keys: None,
// Simple Flags
sig_proxy: true, // Default is true in Docker
read_only: false,
init: false,
oom_kill_disable: false,
no_healthcheck: false,
disable_content_trust: true, // Default is true in Docker
publish_all: false,
quiet: false,
// High-Impact List Options
// DNS & Network
dns: Vec::new(),
dns_option: Vec::new(),
dns_search: Vec::new(),
add_host: Vec::new(),
// Security & Capabilities
cap_add: Vec::new(),
cap_drop: Vec::new(),
security_opt: Vec::new(),
// Device & Filesystem
device: Vec::new(),
tmpfs: Vec::new(),
expose: Vec::new(),
// Environment & Labels
env_file: Vec::new(),
label: Vec::new(),
label_file: Vec::new(),
// Additional List/Vec Options
network_alias: Vec::new(),
group_add: Vec::new(),
attach: Vec::new(),
log_opt: Vec::new(),
storage_opt: Vec::new(),
ulimit: Vec::new(),
volumes_from: Vec::new(),
link: Vec::new(),
link_local_ip: Vec::new(),
// Health Check Options
health_cmd: None,
health_interval: None,
health_retries: None,
health_timeout: None,
health_start_period: None,
health_start_interval: None,
mount: Vec::new(),
network: Vec::new(),
gpus: None,
annotation: Vec::new(),
sysctl: Vec::new(),
// Advanced System Options
blkio_weight: None,
blkio_weight_device: Vec::new(),
device_read_bps: Vec::new(),
device_write_bps: Vec::new(),
device_read_iops: Vec::new(),
device_write_iops: Vec::new(),
cpu_rt_period: None,
cpu_rt_runtime: None,
ip: None,
ip6: None,
device_cgroup_rule: Vec::new(),
}
}
/// Set the container name
#[must_use]
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
/// Run in detached mode (background)
#[must_use]
pub fn detach(mut self) -> Self {
self.detach = true;
self
}
/// Add an environment variable
#[must_use]
pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.environment = self.environment.var(key, value);
self
}
/// Add multiple environment variables
#[must_use]
pub fn envs(mut self, vars: std::collections::HashMap<String, String>) -> Self {
self.environment = self.environment.vars(vars);
self
}
/// Add a port mapping
#[must_use]
pub fn port(mut self, host_port: u16, container_port: u16) -> Self {
self.ports = self.ports.port(host_port, container_port);
self
}
/// Add a dynamic port mapping (Docker assigns host port)
#[must_use]
pub fn dynamic_port(mut self, container_port: u16) -> Self {
self.ports = self.ports.dynamic_port(container_port);
self
}
/// Alias for `dynamic_port()` - Add a dynamic port mapping (Docker assigns host port)
#[must_use]
pub fn port_dyn(self, container_port: u16) -> Self {
self.dynamic_port(container_port)
}
/// Add a volume mount
#[must_use]
pub fn volume(mut self, source: impl Into<String>, target: impl Into<String>) -> Self {
self.volumes.push(VolumeMount {
source: source.into(),
target: target.into(),
mount_type: MountType::Volume,
readonly: false,
});
self
}
/// Add a bind mount
#[must_use]
pub fn bind(mut self, source: impl Into<String>, target: impl Into<String>) -> Self {
self.volumes.push(VolumeMount {
source: source.into(),
target: target.into(),
mount_type: MountType::Bind,
readonly: false,
});
self
}
/// Add a read-only volume mount
#[must_use]
pub fn volume_ro(mut self, source: impl Into<String>, target: impl Into<String>) -> Self {
self.volumes.push(VolumeMount {
source: source.into(),
target: target.into(),
mount_type: MountType::Volume,
readonly: true,
});
self
}
/// Set working directory
#[must_use]
pub fn workdir(mut self, workdir: impl Into<PathBuf>) -> Self {
self.workdir = Some(workdir.into());
self
}
/// Override entrypoint
#[must_use]
pub fn entrypoint(mut self, entrypoint: impl Into<String>) -> Self {
self.entrypoint = Some(entrypoint.into());
self
}
/// Set command to run in container
#[must_use]
pub fn cmd(mut self, command: Vec<String>) -> Self {
self.command = Some(command);
self
}
/// Enable interactive mode
#[must_use]
pub fn interactive(mut self) -> Self {
self.interactive = true;
self
}
/// Allocate a TTY
#[must_use]
pub fn tty(mut self) -> Self {
self.tty = true;
self
}
/// Remove container automatically when it exits
#[must_use]
pub fn remove(mut self) -> Self {
self.remove = true;
self
}
/// Alias for `remove()` - Remove container automatically when it exits (--rm flag)
#[must_use]
pub fn rm(self) -> Self {
self.remove()
}
/// Convenience method for interactive TTY mode
#[must_use]
pub fn it(self) -> Self {
self.interactive().tty()
}
// Resource Limits
/// Set memory limit (e.g., "1g", "512m")
#[must_use]
pub fn memory(mut self, memory: impl Into<String>) -> Self {
self.memory = Some(memory.into());
self
}
/// Set number of CPUs (e.g., "2.0", "1.5")
#[must_use]
pub fn cpus(mut self, cpus: impl Into<String>) -> Self {
self.cpus = Some(cpus.into());
self
}
/// Set CPU shares (relative weight)
#[must_use]
pub fn cpu_shares(mut self, shares: i64) -> Self {
self.cpu_shares = Some(shares);
self
}
/// Set CPU CFS period in microseconds
#[must_use]
pub fn cpu_period(mut self, period: i64) -> Self {
self.cpu_period = Some(period);
self
}
/// Set CPU CFS quota in microseconds
#[must_use]
pub fn cpu_quota(mut self, quota: i64) -> Self {
self.cpu_quota = Some(quota);
self
}
/// Set CPUs in which to allow execution (e.g., "0-3", "0,1")
#[must_use]
pub fn cpuset_cpus(mut self, cpus: impl Into<String>) -> Self {
self.cpuset_cpus = Some(cpus.into());
self
}
/// Set MEMs in which to allow execution (e.g., "0-3", "0,1")
#[must_use]
pub fn cpuset_mems(mut self, mems: impl Into<String>) -> Self {
self.cpuset_mems = Some(mems.into());
self
}
/// Set memory + swap limit (e.g., "2g", "-1" for unlimited)
#[must_use]
pub fn memory_swap(mut self, swap: impl Into<String>) -> Self {
self.memory_swap = Some(swap.into());
self
}
/// Set memory soft limit (e.g., "500m")
#[must_use]
pub fn memory_reservation(mut self, reservation: impl Into<String>) -> Self {
self.memory_reservation = Some(reservation.into());
self
}
// Security & User Context
/// Set username or UID (format: <name|uid>[:<group|gid>])
#[must_use]
pub fn user(mut self, user: impl Into<String>) -> Self {
self.user = Some(user.into());
self
}
/// Give extended privileges to this container
#[must_use]
pub fn privileged(mut self) -> Self {
self.privileged = true;
self
}
/// Set container host name
#[must_use]
pub fn hostname(mut self, hostname: impl Into<String>) -> Self {
self.hostname = Some(hostname.into());
self
}
// Lifecycle Management
/// Set restart policy (e.g., "always", "unless-stopped", "on-failure", "no")
#[must_use]
pub fn restart(mut self, restart: impl Into<String>) -> Self {
self.restart = Some(restart.into());
self
}
// System Integration
/// Set platform if server is multi-platform capable (e.g., "linux/amd64")
#[must_use]
pub fn platform(mut self, platform: impl Into<String>) -> Self {
self.platform = Some(platform.into());
self
}
/// Set runtime to use for this container
#[must_use]
pub fn runtime(mut self, runtime: impl Into<String>) -> Self {
self.runtime = Some(runtime.into());
self
}
/// Set container isolation technology
#[must_use]
pub fn isolation(mut self, isolation: impl Into<String>) -> Self {
self.isolation = Some(isolation.into());
self
}
/// Set pull image policy ("always", "missing", "never")
#[must_use]
pub fn pull(mut self, pull: impl Into<String>) -> Self {
self.pull = Some(pull.into());
self
}
/// Write the container ID to the specified file
#[must_use]
pub fn cidfile(mut self, cidfile: impl Into<String>) -> Self {
self.cidfile = Some(cidfile.into());
self
}
/// Set container NIS domain name
#[must_use]
pub fn domainname(mut self, domainname: impl Into<String>) -> Self {
self.domainname = Some(domainname.into());
self
}
/// Set container MAC address (e.g., "92:d0:c6:0a:29:33")
#[must_use]
pub fn mac_address(mut self, mac: impl Into<String>) -> Self {
self.mac_address = Some(mac.into());
self
}
// Logging & Drivers
/// Set logging driver for the container
#[must_use]
pub fn log_driver(mut self, driver: impl Into<String>) -> Self {
self.log_driver = Some(driver.into());
self
}
/// Set optional volume driver for the container
#[must_use]
pub fn volume_driver(mut self, driver: impl Into<String>) -> Self {
self.volume_driver = Some(driver.into());
self
}
// Namespaces
/// Set user namespace to use
#[must_use]
pub fn userns(mut self, userns: impl Into<String>) -> Self {
self.userns = Some(userns.into());
self
}
/// Set UTS namespace to use
#[must_use]
pub fn uts(mut self, uts: impl Into<String>) -> Self {
self.uts = Some(uts.into());
self
}
/// Set PID namespace to use
#[must_use]
pub fn pid(mut self, pid: impl Into<String>) -> Self {
self.pid = Some(pid.into());
self
}
/// Set IPC mode to use
#[must_use]
pub fn ipc(mut self, ipc: impl Into<String>) -> Self {
self.ipc = Some(ipc.into());
self
}
/// Set cgroup namespace to use (host|private)
#[must_use]
pub fn cgroupns(mut self, cgroupns: impl Into<String>) -> Self {
self.cgroupns = Some(cgroupns.into());
self
}
/// Set optional parent cgroup for the container
#[must_use]
pub fn cgroup_parent(mut self, parent: impl Into<String>) -> Self {
self.cgroup_parent = Some(parent.into());
self
}
// Advanced Memory & Performance
/// Set kernel memory limit
#[must_use]
pub fn kernel_memory(mut self, memory: impl Into<String>) -> Self {
self.kernel_memory = Some(memory.into());
self
}
/// Tune container memory swappiness (0 to 100)
#[must_use]
pub fn memory_swappiness(mut self, swappiness: i32) -> Self {
self.memory_swappiness = Some(swappiness);
self
}
/// Tune host's OOM preferences (-1000 to 1000)
#[must_use]
pub fn oom_score_adj(mut self, score: i32) -> Self {
self.oom_score_adj = Some(score);
self
}
/// Tune container pids limit (set -1 for unlimited)
#[must_use]
pub fn pids_limit(mut self, limit: i64) -> Self {
self.pids_limit = Some(limit);
self
}
/// Set size of /dev/shm (e.g., "64m")
#[must_use]
pub fn shm_size(mut self, size: impl Into<String>) -> Self {
self.shm_size = Some(size.into());
self
}
// Process Control
/// Set signal to stop the container (e.g., "SIGTERM", "SIGKILL")
#[must_use]
pub fn stop_signal(mut self, signal: impl Into<String>) -> Self {
self.stop_signal = Some(signal.into());
self
}
/// Set timeout (in seconds) to stop a container
#[must_use]
pub fn stop_timeout(mut self, timeout: i32) -> Self {
self.stop_timeout = Some(timeout);
self
}
/// Override the key sequence for detaching a container
#[must_use]
pub fn detach_keys(mut self, keys: impl Into<String>) -> Self {
self.detach_keys = Some(keys.into());
self
}
// Simple Flags
/// Disable proxying received signals to the process
#[must_use]
pub fn no_sig_proxy(mut self) -> Self {
self.sig_proxy = false;
self
}
/// Mount the container's root filesystem as read only
#[must_use]
pub fn read_only(mut self) -> Self {
self.read_only = true;
self
}
/// Run an init inside the container that forwards signals and reaps processes
#[must_use]
pub fn init(mut self) -> Self {
self.init = true;
self
}
/// Disable OOM Killer
#[must_use]
pub fn oom_kill_disable(mut self) -> Self {
self.oom_kill_disable = true;
self
}
/// Disable any container-specified HEALTHCHECK
#[must_use]
pub fn no_healthcheck(mut self) -> Self {
self.no_healthcheck = true;
self
}
/// Enable image verification (disable content trust is false)
#[must_use]
pub fn enable_content_trust(mut self) -> Self {
self.disable_content_trust = false;
self
}
/// Publish all exposed ports to random ports
#[must_use]
pub fn publish_all(mut self) -> Self {
self.publish_all = true;
self
}
/// Suppress the pull output
#[must_use]
pub fn quiet(mut self) -> Self {
self.quiet = true;
self