docker-wrapper 0.11.1

A Docker CLI wrapper for Rust
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
//! 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
    }

    // High-Impact List Options

    // DNS & Network
    /// Add custom DNS server
    #[must_use]
    pub fn dns(mut self, dns: impl Into<String>) -> Self {
        self.dns.push(dns.into());
        self
    }

    /// Add multiple DNS servers
    #[must_use]
    pub fn dns_servers(mut self, servers: Vec<String>) -> Self {
        self.dns.extend(servers);
        self
    }

    /// Add DNS option
    #[must_use]
    pub fn dns_option(mut self, option: impl Into<String>) -> Self {
        self.dns_option.push(option.into());
        self
    }

    /// Add DNS search domain
    #[must_use]
    pub fn dns_search(mut self, domain: impl Into<String>) -> Self {
        self.dns_search.push(domain.into());
        self
    }

    /// Add host-to-IP mapping (format: "hostname:ip")
    #[must_use]
    pub fn add_host(mut self, mapping: impl Into<String>) -> Self {
        self.add_host.push(mapping.into());
        self
    }

    // Security & Capabilities
    /// Add Linux capability
    #[must_use]
    pub fn cap_add(mut self, capability: impl Into<String>) -> Self {
        self.cap_add.push(capability.into());
        self
    }

    /// Drop Linux capability
    #[must_use]
    pub fn cap_drop(mut self, capability: impl Into<String>) -> Self {
        self.cap_drop.push(capability.into());
        self
    }

    /// Add security option
    #[must_use]
    pub fn security_opt(mut self, option: impl Into<String>) -> Self {
        self.security_opt.push(option.into());
        self
    }

    // Device & Filesystem
    /// Add host device to container
    #[must_use]
    pub fn device(mut self, device: impl Into<String>) -> Self {
        self.device.push(device.into());
        self
    }

    /// Mount tmpfs directory
    #[must_use]
    pub fn tmpfs(mut self, path: impl Into<String>) -> Self {
        self.tmpfs.push(path.into());
        self
    }

    /// Expose port without publishing it
    #[must_use]
    pub fn expose(mut self, port: impl Into<String>) -> Self {
        self.expose.push(port.into());
        self
    }

    // Environment & Labels
    /// Read environment variables from file
    #[must_use]
    pub fn env_file(mut self, file: impl Into<PathBuf>) -> Self {
        self.env_file.push(file.into());
        self
    }

    /// Add metadata label
    #[must_use]
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label.push(label.into());
        self
    }

    /// Read labels from file
    #[must_use]
    pub fn label_file(mut self, file: impl Into<PathBuf>) -> Self {
        self.label_file.push(file.into());
        self
    }

    // Additional List/Vec Options

    /// Add network alias for the container
    #[must_use]
    pub fn network_alias(mut self, alias: impl Into<String>) -> Self {
        self.network_alias.push(alias.into());
        self
    }

    /// Add supplementary group for the user
    #[must_use]
    pub fn group_add(mut self, group: impl Into<String>) -> Self {
        self.group_add.push(group.into());
        self
    }

    /// Attach to STDIN, STDOUT or STDERR
    #[must_use]
    pub fn attach(mut self, stream: impl Into<String>) -> Self {
        self.attach.push(stream.into());
        self
    }

    /// Add log driver option
    #[must_use]
    pub fn log_opt(mut self, option: impl Into<String>) -> Self {
        self.log_opt.push(option.into());
        self
    }

    /// Add storage driver option
    #[must_use]
    pub fn storage_opt(mut self, option: impl Into<String>) -> Self {
        self.storage_opt.push(option.into());
        self
    }

    /// Set ulimit option
    #[must_use]
    pub fn ulimit(mut self, limit: impl Into<String>) -> Self {
        self.ulimit.push(limit.into());
        self
    }

    /// Mount volumes from another container
    #[must_use]
    pub fn volumes_from(mut self, container: impl Into<String>) -> Self {
        self.volumes_from.push(container.into());
        self
    }

    /// Add link to another container (deprecated)
    #[must_use]
    pub fn link(mut self, link: impl Into<String>) -> Self {
        self.link.push(link.into());
        self
    }

    /// Add container IPv4/IPv6 link-local address
    #[must_use]
    pub fn link_local_ip(mut self, ip: impl Into<String>) -> Self {
        self.link_local_ip.push(ip.into());
        self
    }

    // Health Check Options

    // Health check methods
    /// Set health check command
    #[must_use]
    pub fn health_cmd(mut self, cmd: impl Into<String>) -> Self {
        self.health_cmd = Some(cmd.into());
        self
    }

    /// Set health check interval
    #[must_use]
    pub fn health_interval(mut self, interval: impl Into<String>) -> Self {
        self.health_interval = Some(interval.into());
        self
    }

    /// Set health check retries
    #[must_use]
    pub fn health_retries(mut self, retries: i32) -> Self {
        self.health_retries = Some(retries);
        self
    }

    /// Set health check timeout
    #[must_use]
    pub fn health_timeout(mut self, timeout: impl Into<String>) -> Self {
        self.health_timeout = Some(timeout.into());
        self
    }

    /// Set health check start period
    #[must_use]
    pub fn health_start_period(mut self, period: impl Into<String>) -> Self {
        self.health_start_period = Some(period.into());
        self
    }

    /// Set health check start interval
    #[must_use]
    pub fn health_start_interval(mut self, interval: impl Into<String>) -> Self {
        self.health_start_interval = Some(interval.into());
        self
    }

    // Advanced options
    /// Add advanced mount configuration
    #[must_use]
    pub fn mount(mut self, mount: impl Into<String>) -> Self {
        self.mount.push(mount.into());
        self
    }

    /// Connect to a network
    #[must_use]
    pub fn network(mut self, network: impl Into<String>) -> Self {
        self.network.push(network.into());
        self
    }

    /// Set GPU devices to add to the container
    #[must_use]
    pub fn gpus(mut self, gpus: impl Into<String>) -> Self {
        self.gpus = Some(gpus.into());
        self
    }

    /// Add custom annotation (key=value format)
    #[must_use]
    pub fn annotation(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.annotation
            .push(format!("{}={}", key.into(), value.into()));
        self
    }

    /// Set kernel parameter (key=value format)
    #[must_use]
    pub fn sysctl(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.sysctl.push(format!("{}={}", key.into(), value.into()));
        self
    }

    // Advanced System Options

    // Block I/O controls
    /// Set block IO weight (10-1000)
    #[must_use]
    pub fn blkio_weight(mut self, weight: u16) -> Self {
        self.blkio_weight = Some(weight);
        self
    }

    /// Set block IO weight for a specific device (format: DEVICE:WEIGHT)
    #[must_use]
    pub fn blkio_weight_device(mut self, device_weight: impl Into<String>) -> Self {
        self.blkio_weight_device.push(device_weight.into());
        self
    }

    /// Limit read rate (bytes per second) from a device (format: DEVICE:RATE)
    #[must_use]
    pub fn device_read_bps(mut self, device_rate: impl Into<String>) -> Self {
        self.device_read_bps.push(device_rate.into());
        self
    }

    /// Limit write rate (bytes per second) to a device (format: DEVICE:RATE)
    #[must_use]
    pub fn device_write_bps(mut self, device_rate: impl Into<String>) -> Self {
        self.device_write_bps.push(device_rate.into());
        self
    }

    /// Limit read rate (IO per second) from a device (format: DEVICE:RATE)
    #[must_use]
    pub fn device_read_iops(mut self, device_rate: impl Into<String>) -> Self {
        self.device_read_iops.push(device_rate.into());
        self
    }

    /// Limit write rate (IO per second) to a device (format: DEVICE:RATE)
    #[must_use]
    pub fn device_write_iops(mut self, device_rate: impl Into<String>) -> Self {
        self.device_write_iops.push(device_rate.into());
        self
    }

    // Real-time CPU scheduling
    /// Limit CPU real-time period in microseconds
    #[must_use]
    pub fn cpu_rt_period(mut self, period: i64) -> Self {
        self.cpu_rt_period = Some(period);
        self
    }

    /// Limit CPU real-time runtime in microseconds
    #[must_use]
    pub fn cpu_rt_runtime(mut self, runtime: i64) -> Self {
        self.cpu_rt_runtime = Some(runtime);
        self
    }

    // Advanced networking
    /// Set container IPv4 address (e.g., "172.30.100.104")
    #[must_use]
    pub fn ip(mut self, ip: impl Into<String>) -> Self {
        self.ip = Some(ip.into());
        self
    }

    /// Set container IPv6 address (e.g., "`2001:db8::33`")
    #[must_use]
    pub fn ip6(mut self, ip6: impl Into<String>) -> Self {
        self.ip6 = Some(ip6.into());
        self
    }

    /// Add device cgroup rule
    #[must_use]
    pub fn device_cgroup_rule(mut self, rule: impl Into<String>) -> Self {
        self.device_cgroup_rule.push(rule.into());
        self
    }
}

#[async_trait]
impl DockerCommand for RunCommand {
    type Output = ContainerId;

    fn get_executor(&self) -> &CommandExecutor {
        &self.executor
    }

    fn get_executor_mut(&mut self) -> &mut CommandExecutor {
        &mut self.executor
    }

    #[allow(clippy::too_many_lines)]
    fn build_command_args(&self) -> Vec<String> {
        let mut args = vec!["run".to_string()];

        // Add flags
        if self.detach {
            args.push("--detach".to_string());
        }
        if self.interactive {
            args.push("--interactive".to_string());
        }
        if self.tty {
            args.push("--tty".to_string());
        }
        if self.remove {
            args.push("--rm".to_string());
        }

        // Add container name
        if let Some(ref name) = self.name {
            args.push("--name".to_string());
            args.push(name.clone());
        }

        // Add working directory
        if let Some(ref workdir) = self.workdir {
            args.push("--workdir".to_string());
            args.push(workdir.to_string_lossy().to_string());
        }

        // Add entrypoint
        if let Some(ref entrypoint) = self.entrypoint {
            args.push("--entrypoint".to_string());
            args.push(entrypoint.clone());
        }

        // Add environment variables
        args.extend(self.environment.build_args());

        // Add port mappings
        args.extend(self.ports.build_args());

        // Add volume mounts
        for volume in &self.volumes {
            args.push("--volume".to_string());
            args.push(volume.to_string());
        }

        // Resource Limits
        if let Some(ref memory) = self.memory {
            args.push("--memory".to_string());
            args.push(memory.clone());
        }
        if let Some(ref cpus) = self.cpus {
            args.push("--cpus".to_string());
            args.push(cpus.clone());
        }
        if let Some(cpu_shares) = self.cpu_shares {
            args.push("--cpu-shares".to_string());
            args.push(cpu_shares.to_string());
        }
        if let Some(cpu_period) = self.cpu_period {
            args.push("--cpu-period".to_string());
            args.push(cpu_period.to_string());
        }
        if let Some(cpu_quota) = self.cpu_quota {
            args.push("--cpu-quota".to_string());
            args.push(cpu_quota.to_string());
        }
        if let Some(ref cpuset_cpus) = self.cpuset_cpus {
            args.push("--cpuset-cpus".to_string());
            args.push(cpuset_cpus.clone());
        }
        if let Some(ref cpuset_mems) = self.cpuset_mems {
            args.push("--cpuset-mems".to_string());
            args.push(cpuset_mems.clone());
        }
        if let Some(ref memory_swap) = self.memory_swap {
            args.push("--memory-swap".to_string());
            args.push(memory_swap.clone());
        }
        if let Some(ref memory_reservation) = self.memory_reservation {
            args.push("--memory-reservation".to_string());
            args.push(memory_reservation.clone());
        }

        // Security & User Context
        if let Some(ref user) = self.user {
            args.push("--user".to_string());
            args.push(user.clone());
        }
        if self.privileged {
            args.push("--privileged".to_string());
        }
        if let Some(ref hostname) = self.hostname {
            args.push("--hostname".to_string());
            args.push(hostname.clone());
        }

        // Lifecycle Management
        if let Some(ref restart) = self.restart {
            args.push("--restart".to_string());
            args.push(restart.clone());
        }

        // System Integration
        if let Some(ref platform) = self.platform {
            args.push("--platform".to_string());
            args.push(platform.clone());
        }
        if let Some(ref runtime) = self.runtime {
            args.push("--runtime".to_string());
            args.push(runtime.clone());
        }
        if let Some(ref isolation) = self.isolation {
            args.push("--isolation".to_string());
            args.push(isolation.clone());
        }
        if let Some(ref pull) = self.pull {
            args.push("--pull".to_string());
            args.push(pull.clone());
        }
        if let Some(ref cidfile) = self.cidfile {
            args.push("--cidfile".to_string());
            args.push(cidfile.clone());
        }
        if let Some(ref domainname) = self.domainname {
            args.push("--domainname".to_string());
            args.push(domainname.clone());
        }
        if let Some(ref mac_address) = self.mac_address {
            args.push("--mac-address".to_string());
            args.push(mac_address.clone());
        }

        // Logging & Drivers
        if let Some(ref log_driver) = self.log_driver {
            args.push("--log-driver".to_string());
            args.push(log_driver.clone());
        }
        if let Some(ref volume_driver) = self.volume_driver {
            args.push("--volume-driver".to_string());
            args.push(volume_driver.clone());
        }

        // Namespaces
        if let Some(ref userns) = self.userns {
            args.push("--userns".to_string());
            args.push(userns.clone());
        }
        if let Some(ref uts) = self.uts {
            args.push("--uts".to_string());
            args.push(uts.clone());
        }
        if let Some(ref pid) = self.pid {
            args.push("--pid".to_string());
            args.push(pid.clone());
        }
        if let Some(ref ipc) = self.ipc {
            args.push("--ipc".to_string());
            args.push(ipc.clone());
        }
        if let Some(ref cgroupns) = self.cgroupns {
            args.push("--cgroupns".to_string());
            args.push(cgroupns.clone());
        }
        if let Some(ref cgroup_parent) = self.cgroup_parent {
            args.push("--cgroup-parent".to_string());
            args.push(cgroup_parent.clone());
        }

        // Advanced Memory & Performance
        if let Some(ref kernel_memory) = self.kernel_memory {
            args.push("--kernel-memory".to_string());
            args.push(kernel_memory.clone());
        }
        if let Some(memory_swappiness) = self.memory_swappiness {
            args.push("--memory-swappiness".to_string());
            args.push(memory_swappiness.to_string());
        }
        if let Some(oom_score_adj) = self.oom_score_adj {
            args.push("--oom-score-adj".to_string());
            args.push(oom_score_adj.to_string());
        }
        if let Some(pids_limit) = self.pids_limit {
            args.push("--pids-limit".to_string());
            args.push(pids_limit.to_string());
        }
        if let Some(ref shm_size) = self.shm_size {
            args.push("--shm-size".to_string());
            args.push(shm_size.clone());
        }

        // Process Control
        if let Some(ref stop_signal) = self.stop_signal {
            args.push("--stop-signal".to_string());
            args.push(stop_signal.clone());
        }
        if let Some(stop_timeout) = self.stop_timeout {
            args.push("--stop-timeout".to_string());
            args.push(stop_timeout.to_string());
        }
        if let Some(ref detach_keys) = self.detach_keys {
            args.push("--detach-keys".to_string());
            args.push(detach_keys.clone());
        }

        // Simple Flags
        if !self.sig_proxy {
            args.push("--sig-proxy=false".to_string());
        }
        if self.read_only {
            args.push("--read-only".to_string());
        }
        if self.init {
            args.push("--init".to_string());
        }
        if self.oom_kill_disable {
            args.push("--oom-kill-disable".to_string());
        }
        if self.no_healthcheck {
            args.push("--no-healthcheck".to_string());
        }
        if !self.disable_content_trust {
            args.push("--disable-content-trust=false".to_string());
        }
        if self.publish_all {
            args.push("--publish-all".to_string());
        }
        if self.quiet {
            args.push("--quiet".to_string());
        }

        // High-Impact List Options
        // DNS & Network
        for dns in &self.dns {
            args.push("--dns".to_string());
            args.push(dns.clone());
        }
        for dns_option in &self.dns_option {
            args.push("--dns-option".to_string());
            args.push(dns_option.clone());
        }
        for dns_search in &self.dns_search {
            args.push("--dns-search".to_string());
            args.push(dns_search.clone());
        }
        for add_host in &self.add_host {
            args.push("--add-host".to_string());
            args.push(add_host.clone());
        }

        // Security & Capabilities
        for cap_add in &self.cap_add {
            args.push("--cap-add".to_string());
            args.push(cap_add.clone());
        }
        for cap_drop in &self.cap_drop {
            args.push("--cap-drop".to_string());
            args.push(cap_drop.clone());
        }
        for security_opt in &self.security_opt {
            args.push("--security-opt".to_string());
            args.push(security_opt.clone());
        }

        // Device & Filesystem
        for device in &self.device {
            args.push("--device".to_string());
            args.push(device.clone());
        }
        for tmpfs in &self.tmpfs {
            args.push("--tmpfs".to_string());
            args.push(tmpfs.clone());
        }
        for expose in &self.expose {
            args.push("--expose".to_string());
            args.push(expose.clone());
        }

        // Environment & Labels
        for env_file in &self.env_file {
            args.push("--env-file".to_string());
            args.push(env_file.to_string_lossy().to_string());
        }
        for label in &self.label {
            args.push("--label".to_string());
            args.push(label.clone());
        }
        for label_file in &self.label_file {
            args.push("--label-file".to_string());
            args.push(label_file.to_string_lossy().to_string());
        }

        // Additional List/Vec Options
        for network_alias in &self.network_alias {
            args.push("--network-alias".to_string());
            args.push(network_alias.clone());
        }
        for group_add in &self.group_add {
            args.push("--group-add".to_string());
            args.push(group_add.clone());
        }
        for attach in &self.attach {
            args.push("--attach".to_string());
            args.push(attach.clone());
        }
        for log_opt in &self.log_opt {
            args.push("--log-opt".to_string());
            args.push(log_opt.clone());
        }
        for storage_opt in &self.storage_opt {
            args.push("--storage-opt".to_string());
            args.push(storage_opt.clone());
        }
        for ulimit in &self.ulimit {
            args.push("--ulimit".to_string());
            args.push(ulimit.clone());
        }
        for volumes_from in &self.volumes_from {
            args.push("--volumes-from".to_string());
            args.push(volumes_from.clone());
        }
        for link in &self.link {
            args.push("--link".to_string());
            args.push(link.clone());
        }
        for link_local_ip in &self.link_local_ip {
            args.push("--link-local-ip".to_string());
            args.push(link_local_ip.clone());
        }

        // Health Check Options
        // Health checks
        if let Some(ref health_cmd) = self.health_cmd {
            args.push("--health-cmd".to_string());
            args.push(health_cmd.clone());
        }
        if let Some(ref health_interval) = self.health_interval {
            args.push("--health-interval".to_string());
            args.push(health_interval.clone());
        }
        if let Some(health_retries) = self.health_retries {
            args.push("--health-retries".to_string());
            args.push(health_retries.to_string());
        }
        if let Some(ref health_timeout) = self.health_timeout {
            args.push("--health-timeout".to_string());
            args.push(health_timeout.clone());
        }
        if let Some(ref health_start_period) = self.health_start_period {
            args.push("--health-start-period".to_string());
            args.push(health_start_period.clone());
        }
        if let Some(ref health_start_interval) = self.health_start_interval {
            args.push("--health-start-interval".to_string());
            args.push(health_start_interval.clone());
        }

        // Advanced options
        for mount in &self.mount {
            args.push("--mount".to_string());
            args.push(mount.clone());
        }
        for network in &self.network {
            args.push("--network".to_string());
            args.push(network.clone());
        }
        if let Some(ref gpus) = self.gpus {
            args.push("--gpus".to_string());
            args.push(gpus.clone());
        }

        // Map-based options
        for annotation in &self.annotation {
            args.push("--annotation".to_string());
            args.push(annotation.clone());
        }
        for sysctl in &self.sysctl {
            args.push("--sysctl".to_string());
            args.push(sysctl.clone());
        }

        // Advanced System Options
        // Block I/O controls
        if let Some(blkio_weight) = self.blkio_weight {
            args.push("--blkio-weight".to_string());
            args.push(blkio_weight.to_string());
        }
        for blkio_weight_device in &self.blkio_weight_device {
            args.push("--blkio-weight-device".to_string());
            args.push(blkio_weight_device.clone());
        }
        for device_read_bps in &self.device_read_bps {
            args.push("--device-read-bps".to_string());
            args.push(device_read_bps.clone());
        }
        for device_write_bps in &self.device_write_bps {
            args.push("--device-write-bps".to_string());
            args.push(device_write_bps.clone());
        }
        for device_read_iops in &self.device_read_iops {
            args.push("--device-read-iops".to_string());
            args.push(device_read_iops.clone());
        }
        for device_write_iops in &self.device_write_iops {
            args.push("--device-write-iops".to_string());
            args.push(device_write_iops.clone());
        }

        // Real-time CPU scheduling
        if let Some(cpu_rt_period) = self.cpu_rt_period {
            args.push("--cpu-rt-period".to_string());
            args.push(cpu_rt_period.to_string());
        }
        if let Some(cpu_rt_runtime) = self.cpu_rt_runtime {
            args.push("--cpu-rt-runtime".to_string());
            args.push(cpu_rt_runtime.to_string());
        }

        // Advanced networking
        if let Some(ref ip) = self.ip {
            args.push("--ip".to_string());
            args.push(ip.clone());
        }
        if let Some(ref ip6) = self.ip6 {
            args.push("--ip6".to_string());
            args.push(ip6.clone());
        }

        // Advanced system options
        for device_cgroup_rule in &self.device_cgroup_rule {
            args.push("--device-cgroup-rule".to_string());
            args.push(device_cgroup_rule.clone());
        }

        // Add image
        args.push(self.image.clone());

        // Add command if specified
        if let Some(ref command) = self.command {
            args.extend(command.clone());
        }

        // Add raw arguments from executor
        args.extend(self.executor.raw_args.clone());

        args
    }

    async fn execute(&self) -> Result<Self::Output> {
        let args = self.build_command_args();
        let output = self.execute_command(args).await?;

        // Parse container ID from output
        let container_id = output.stdout.trim().to_string();
        if container_id.is_empty() {
            return Err(Error::parse_error(
                "No container ID returned from docker run",
            ));
        }

        Ok(ContainerId(container_id))
    }
}

// Streaming support for RunCommand
#[async_trait]
impl StreamableCommand for RunCommand {
    async fn stream<F>(&self, handler: F) -> Result<StreamResult>
    where
        F: FnMut(OutputLine) + Send + 'static,
    {
        // Don't stream if running detached
        if self.detach {
            return Err(Error::custom(
                "Cannot stream output for detached containers",
            ));
        }

        let mut cmd = TokioCommand::new("docker");
        cmd.arg("run");

        for arg in self.build_command_args() {
            cmd.arg(arg);
        }

        crate::stream::stream_command(cmd, handler).await
    }

    async fn stream_channel(&self) -> Result<(mpsc::Receiver<OutputLine>, StreamResult)> {
        // Don't stream if running detached
        if self.detach {
            return Err(Error::custom(
                "Cannot stream output for detached containers",
            ));
        }

        let mut cmd = TokioCommand::new("docker");
        cmd.arg("run");

        for arg in self.build_command_args() {
            cmd.arg(arg);
        }

        crate::stream::stream_command_channel(cmd).await
    }
}

impl RunCommand {
    /// Run the container with streaming output
    ///
    /// Note: This will fail if the container is run in detached mode.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use docker_wrapper::RunCommand;
    /// use docker_wrapper::StreamHandler;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let result = RunCommand::new("alpine")
    ///     .cmd(vec!["echo".to_string(), "Hello, World!".to_string()])
    ///     .stream(StreamHandler::print())
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the container is detached or encounters an I/O error
    pub async fn stream<F>(&self, handler: F) -> Result<StreamResult>
    where
        F: FnMut(OutputLine) + Send + 'static,
    {
        <Self as StreamableCommand>::stream(self, handler).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_run_command_builder() {
        let cmd = RunCommand::new("nginx:latest")
            .name("test-nginx")
            .detach()
            .env("ENV_VAR", "value")
            .port(8080, 80)
            .volume("data", "/var/data")
            .workdir("/app")
            .remove();

        let args = cmd.build_command_args();

        assert!(args.contains(&"--detach".to_string()));
        assert!(args.contains(&"--name".to_string()));
        assert!(args.contains(&"test-nginx".to_string()));
        assert!(args.contains(&"--env".to_string()));
        assert!(args.contains(&"ENV_VAR=value".to_string()));
        assert!(args.contains(&"--publish".to_string()));
        assert!(args.contains(&"8080:80".to_string()));
        assert!(args.contains(&"--volume".to_string()));
        assert!(args.contains(&"data:/var/data".to_string()));
        assert!(args.contains(&"--workdir".to_string()));
        assert!(args.contains(&"/app".to_string()));
        assert!(args.contains(&"--rm".to_string()));
        assert!(args.contains(&"nginx:latest".to_string()));
    }

    #[test]
    fn test_run_command_with_cmd() {
        let cmd =
            RunCommand::new("alpine:latest").cmd(vec!["echo".to_string(), "hello".to_string()]);

        let args = cmd.build_command_args();
        assert!(args.contains(&"alpine:latest".to_string()));
        assert!(args.contains(&"echo".to_string()));
        assert!(args.contains(&"hello".to_string()));
    }

    #[test]
    fn test_run_command_extensibility() {
        let mut cmd = RunCommand::new("test:latest");
        cmd.flag("privileged")
            .option("memory", "1g")
            .arg("--custom-option");

        // The extensibility is tested via the trait methods
        // Full integration testing would require actual Docker execution
    }

    #[test]
    fn test_volume_mount_display() {
        let volume = VolumeMount {
            source: "data".to_string(),
            target: "/var/data".to_string(),
            mount_type: MountType::Volume,
            readonly: false,
        };
        assert_eq!(volume.to_string(), "data:/var/data");

        let readonly_volume = VolumeMount {
            source: "/host/path".to_string(),
            target: "/container/path".to_string(),
            mount_type: MountType::Bind,
            readonly: true,
        };
        assert_eq!(readonly_volume.to_string(), "/host/path:/container/path:ro");
    }

    #[test]
    fn test_run_command_resource_limits() {
        let cmd = RunCommand::new("alpine:latest")
            .memory("1g")
            .cpus("2.0")
            .cpu_shares(1024)
            .cpu_period(100_000)
            .cpu_quota(50_000)
            .cpuset_cpus("0-3")
            .cpuset_mems("0,1")
            .memory_swap("2g")
            .memory_reservation("500m");

        let args = cmd.build_command_args();

        assert!(args.contains(&"--memory".to_string()));
        assert!(args.contains(&"1g".to_string()));
        assert!(args.contains(&"--cpus".to_string()));
        assert!(args.contains(&"2.0".to_string()));
        assert!(args.contains(&"--cpu-shares".to_string()));
        assert!(args.contains(&"1024".to_string()));
        assert!(args.contains(&"--cpu-period".to_string()));
        assert!(args.contains(&"100000".to_string()));
        assert!(args.contains(&"--cpu-quota".to_string()));
        assert!(args.contains(&"50000".to_string()));
        assert!(args.contains(&"--cpuset-cpus".to_string()));
        assert!(args.contains(&"0-3".to_string()));
        assert!(args.contains(&"--cpuset-mems".to_string()));
        assert!(args.contains(&"0,1".to_string()));
        assert!(args.contains(&"--memory-swap".to_string()));
        assert!(args.contains(&"2g".to_string()));
        assert!(args.contains(&"--memory-reservation".to_string()));
        assert!(args.contains(&"500m".to_string()));
    }

    #[test]
    fn test_run_command_security_and_user() {
        let cmd = RunCommand::new("alpine:latest")
            .user("1000:1000")
            .privileged()
            .hostname("test-host");

        let args = cmd.build_command_args();

        assert!(args.contains(&"--user".to_string()));
        assert!(args.contains(&"1000:1000".to_string()));
        assert!(args.contains(&"--privileged".to_string()));
        assert!(args.contains(&"--hostname".to_string()));
        assert!(args.contains(&"test-host".to_string()));
    }

    #[test]
    fn test_run_command_lifecycle_management() {
        let cmd = RunCommand::new("alpine:latest").restart("always");

        let args = cmd.build_command_args();

        assert!(args.contains(&"--restart".to_string()));
        assert!(args.contains(&"always".to_string()));
    }

    #[test]
    fn test_run_command_system_integration() {
        let cmd = RunCommand::new("alpine:latest")
            .platform("linux/amd64")
            .runtime("runc")
            .isolation("default")
            .pull("always")
            .cidfile("/tmp/container.cid")
            .domainname("example.com")
            .mac_address("92:d0:c6:0a:29:33");

        let args = cmd.build_command_args();

        assert!(args.contains(&"--platform".to_string()));
        assert!(args.contains(&"linux/amd64".to_string()));
        assert!(args.contains(&"--runtime".to_string()));
        assert!(args.contains(&"runc".to_string()));
        assert!(args.contains(&"--isolation".to_string()));
        assert!(args.contains(&"default".to_string()));
        assert!(args.contains(&"--pull".to_string()));
        assert!(args.contains(&"always".to_string()));
        assert!(args.contains(&"--cidfile".to_string()));
        assert!(args.contains(&"/tmp/container.cid".to_string()));
        assert!(args.contains(&"--domainname".to_string()));
        assert!(args.contains(&"example.com".to_string()));
        assert!(args.contains(&"--mac-address".to_string()));
        assert!(args.contains(&"92:d0:c6:0a:29:33".to_string()));
    }

    #[test]
    fn test_run_command_logging_and_drivers() {
        let cmd = RunCommand::new("alpine:latest")
            .log_driver("json-file")
            .volume_driver("local");

        let args = cmd.build_command_args();

        assert!(args.contains(&"--log-driver".to_string()));
        assert!(args.contains(&"json-file".to_string()));
        assert!(args.contains(&"--volume-driver".to_string()));
        assert!(args.contains(&"local".to_string()));
    }

    #[test]
    fn test_run_command_namespaces() {
        let cmd = RunCommand::new("alpine:latest")
            .userns("host")
            .uts("host")
            .pid("host")
            .ipc("host")
            .cgroupns("private")
            .cgroup_parent("/docker");

        let args = cmd.build_command_args();

        assert!(args.contains(&"--userns".to_string()));
        assert!(args.contains(&"host".to_string()));
        assert!(args.contains(&"--uts".to_string()));
        assert!(args.contains(&"--pid".to_string()));
        assert!(args.contains(&"--ipc".to_string()));
        assert!(args.contains(&"--cgroupns".to_string()));
        assert!(args.contains(&"private".to_string()));
        assert!(args.contains(&"--cgroup-parent".to_string()));
        assert!(args.contains(&"/docker".to_string()));
    }

    #[test]
    fn test_run_command_advanced_memory_performance() {
        let cmd = RunCommand::new("alpine:latest")
            .kernel_memory("100m")
            .memory_swappiness(60)
            .oom_score_adj(-500)
            .pids_limit(100)
            .shm_size("64m");

        let args = cmd.build_command_args();

        assert!(args.contains(&"--kernel-memory".to_string()));
        assert!(args.contains(&"100m".to_string()));
        assert!(args.contains(&"--memory-swappiness".to_string()));
        assert!(args.contains(&"60".to_string()));
        assert!(args.contains(&"--oom-score-adj".to_string()));
        assert!(args.contains(&"-500".to_string()));
        assert!(args.contains(&"--pids-limit".to_string()));
        assert!(args.contains(&"100".to_string()));
        assert!(args.contains(&"--shm-size".to_string()));
        assert!(args.contains(&"64m".to_string()));
    }

    #[test]
    fn test_run_command_process_control() {
        let cmd = RunCommand::new("alpine:latest")
            .stop_signal("SIGTERM")
            .stop_timeout(10)
            .detach_keys("ctrl-p,ctrl-q");

        let args = cmd.build_command_args();

        assert!(args.contains(&"--stop-signal".to_string()));
        assert!(args.contains(&"SIGTERM".to_string()));
        assert!(args.contains(&"--stop-timeout".to_string()));
        assert!(args.contains(&"10".to_string()));
        assert!(args.contains(&"--detach-keys".to_string()));
        assert!(args.contains(&"ctrl-p,ctrl-q".to_string()));
    }

    #[test]
    fn test_run_command_simple_flags() {
        let cmd = RunCommand::new("alpine:latest")
            .no_sig_proxy()
            .read_only()
            .init()
            .oom_kill_disable()
            .no_healthcheck()
            .enable_content_trust()
            .publish_all()
            .quiet();

        let args = cmd.build_command_args();

        assert!(args.contains(&"--sig-proxy=false".to_string()));
        assert!(args.contains(&"--read-only".to_string()));
        assert!(args.contains(&"--init".to_string()));
        assert!(args.contains(&"--oom-kill-disable".to_string()));
        assert!(args.contains(&"--no-healthcheck".to_string()));
        assert!(args.contains(&"--disable-content-trust=false".to_string()));
        assert!(args.contains(&"--publish-all".to_string()));
        assert!(args.contains(&"--quiet".to_string()));
    }

    #[test]
    fn test_run_command_comprehensive_builder() {
        let cmd = RunCommand::new("nginx:latest")
            .name("production-nginx")
            .detach()
            .memory("2g")
            .cpus("4.0")
            .user("nginx:nginx")
            .privileged()
            .restart("unless-stopped")
            .hostname("web-server")
            .platform("linux/amd64")
            .env("NGINX_PORT", "8080")
            .port(80, 8080)
            .volume("nginx-data", "/var/lib/nginx")
            .workdir("/usr/share/nginx/html")
            .read_only()
            .init()
            .remove();

        let args = cmd.build_command_args();

        // Verify comprehensive options are all present
        assert!(args.contains(&"--name".to_string()));
        assert!(args.contains(&"production-nginx".to_string()));
        assert!(args.contains(&"--detach".to_string()));
        assert!(args.contains(&"--memory".to_string()));
        assert!(args.contains(&"2g".to_string()));
        assert!(args.contains(&"--cpus".to_string()));
        assert!(args.contains(&"4.0".to_string()));
        assert!(args.contains(&"--user".to_string()));
        assert!(args.contains(&"nginx:nginx".to_string()));
        assert!(args.contains(&"--privileged".to_string()));
        assert!(args.contains(&"--restart".to_string()));
        assert!(args.contains(&"unless-stopped".to_string()));
        assert!(args.contains(&"--hostname".to_string()));
        assert!(args.contains(&"web-server".to_string()));
        assert!(args.contains(&"--platform".to_string()));
        assert!(args.contains(&"linux/amd64".to_string()));
        assert!(args.contains(&"--read-only".to_string()));
        assert!(args.contains(&"--init".to_string()));
        assert!(args.contains(&"--rm".to_string()));
        assert!(args.contains(&"nginx:latest".to_string()));

        // Image should be at the end (before any command args)
        let image_pos = args.iter().position(|x| x == "nginx:latest").unwrap();
        assert!(image_pos > 10); // Should be after all the options
    }

    #[test]
    fn test_run_command_default_flag_values() {
        let cmd = RunCommand::new("alpine:latest");
        let args = cmd.build_command_args();

        // Default flags should not appear in args unless explicitly changed
        assert!(!args.contains(&"--sig-proxy=false".to_string()));
        assert!(!args.contains(&"--disable-content-trust=false".to_string()));
        assert!(!args.contains(&"--read-only".to_string()));
        assert!(!args.contains(&"--privileged".to_string()));
        assert!(!args.contains(&"--init".to_string()));
    }

    #[test]
    fn test_container_id() {
        let id = ContainerId("abcdef123456789".to_string());
        assert_eq!(id.as_str(), "abcdef123456789");
        assert_eq!(id.short(), "abcdef123456");
        assert_eq!(id.to_string(), "abcdef123456789");

        let short_id = ContainerId("abc".to_string());
        assert_eq!(short_id.short(), "abc");
    }

    #[test]
    fn test_it_convenience_method() {
        let cmd = RunCommand::new("alpine:latest").it();
        let args = cmd.build_command_args();
        assert!(args.contains(&"--interactive".to_string()));
        assert!(args.contains(&"--tty".to_string()));
    }

    #[test]
    fn test_run_command_dns_network_options() {
        let cmd = RunCommand::new("alpine:latest")
            .dns("8.8.8.8")
            .dns("8.8.4.4")
            .dns_servers(vec!["1.1.1.1".to_string(), "1.0.0.1".to_string()])
            .dns_option("ndots:2")
            .dns_option("timeout:1")
            .dns_search("example.com")
            .dns_search("test.local")
            .add_host("api.example.com:127.0.0.1")
            .add_host("db.example.com:192.168.1.100");

        let args = cmd.build_command_args();

        // DNS servers
        assert!(args.contains(&"--dns".to_string()));
        assert!(args.contains(&"8.8.8.8".to_string()));
        assert!(args.contains(&"8.8.4.4".to_string()));
        assert!(args.contains(&"1.1.1.1".to_string()));
        assert!(args.contains(&"1.0.0.1".to_string()));

        // DNS options
        assert!(args.contains(&"--dns-option".to_string()));
        assert!(args.contains(&"ndots:2".to_string()));
        assert!(args.contains(&"timeout:1".to_string()));

        // DNS search domains
        assert!(args.contains(&"--dns-search".to_string()));
        assert!(args.contains(&"example.com".to_string()));
        assert!(args.contains(&"test.local".to_string()));

        // Host mappings
        assert!(args.contains(&"--add-host".to_string()));
        assert!(args.contains(&"api.example.com:127.0.0.1".to_string()));
        assert!(args.contains(&"db.example.com:192.168.1.100".to_string()));
    }

    #[test]
    fn test_run_command_security_capabilities() {
        let cmd = RunCommand::new("alpine:latest")
            .cap_add("NET_ADMIN")
            .cap_add("SYS_TIME")
            .cap_drop("CHOWN")
            .cap_drop("DAC_OVERRIDE")
            .security_opt("no-new-privileges:true")
            .security_opt("seccomp=unconfined");

        let args = cmd.build_command_args();

        // Capabilities to add
        assert!(args.contains(&"--cap-add".to_string()));
        assert!(args.contains(&"NET_ADMIN".to_string()));
        assert!(args.contains(&"SYS_TIME".to_string()));

        // Capabilities to drop
        assert!(args.contains(&"--cap-drop".to_string()));
        assert!(args.contains(&"CHOWN".to_string()));
        assert!(args.contains(&"DAC_OVERRIDE".to_string()));

        // Security options
        assert!(args.contains(&"--security-opt".to_string()));
        assert!(args.contains(&"no-new-privileges:true".to_string()));
        assert!(args.contains(&"seccomp=unconfined".to_string()));
    }

    #[test]
    fn test_run_command_device_filesystem() {
        let cmd = RunCommand::new("alpine:latest")
            .device("/dev/sda:/dev/xvda:rwm")
            .device("/dev/zero")
            .tmpfs("/tmp:rw,size=100m")
            .tmpfs("/var/tmp:ro")
            .expose("80")
            .expose("443")
            .expose("8080/tcp");

        let args = cmd.build_command_args();

        // Devices
        assert!(args.contains(&"--device".to_string()));
        assert!(args.contains(&"/dev/sda:/dev/xvda:rwm".to_string()));
        assert!(args.contains(&"/dev/zero".to_string()));

        // Tmpfs mounts
        assert!(args.contains(&"--tmpfs".to_string()));
        assert!(args.contains(&"/tmp:rw,size=100m".to_string()));
        assert!(args.contains(&"/var/tmp:ro".to_string()));

        // Exposed ports
        assert!(args.contains(&"--expose".to_string()));
        assert!(args.contains(&"80".to_string()));
        assert!(args.contains(&"443".to_string()));
        assert!(args.contains(&"8080/tcp".to_string()));
    }

    #[test]
    fn test_run_command_environment_labels() {
        use std::path::PathBuf;

        let cmd = RunCommand::new("alpine:latest")
            .env_file(PathBuf::from("/etc/environment"))
            .env_file(PathBuf::from("./app.env"))
            .label("version=1.0.0")
            .label("maintainer=team@example.com")
            .label("app=myapp")
            .label_file(PathBuf::from("/etc/labels"))
            .label_file(PathBuf::from("./metadata.labels"));

        let args = cmd.build_command_args();

        // Environment files
        assert!(args.contains(&"--env-file".to_string()));
        assert!(args.contains(&"/etc/environment".to_string()));
        assert!(args.contains(&"./app.env".to_string()));

        // Labels
        assert!(args.contains(&"--label".to_string()));
        assert!(args.contains(&"version=1.0.0".to_string()));
        assert!(args.contains(&"maintainer=team@example.com".to_string()));
        assert!(args.contains(&"app=myapp".to_string()));

        // Label files
        assert!(args.contains(&"--label-file".to_string()));
        assert!(args.contains(&"/etc/labels".to_string()));
        assert!(args.contains(&"./metadata.labels".to_string()));
    }

    #[test]
    fn test_run_command_all_high_impact_options() {
        use std::path::PathBuf;

        let cmd = RunCommand::new("nginx:latest")
            .name("production-nginx")
            // DNS & Network
            .dns("8.8.8.8")
            .dns_option("ndots:2")
            .dns_search("example.com")
            .add_host("api.example.com:127.0.0.1")
            // Security & Capabilities
            .cap_add("NET_ADMIN")
            .cap_drop("CHOWN")
            .security_opt("no-new-privileges:true")
            // Device & Filesystem
            .device("/dev/null")
            .tmpfs("/tmp:rw,size=100m")
            .expose("80")
            // Environment & Labels
            .env_file(PathBuf::from(".env"))
            .label("version=1.0.0")
            .label_file(PathBuf::from("labels"));

        let args = cmd.build_command_args();

        // Verify all option types are present
        assert!(args.contains(&"--dns".to_string()));
        assert!(args.contains(&"--dns-option".to_string()));
        assert!(args.contains(&"--dns-search".to_string()));
        assert!(args.contains(&"--add-host".to_string()));
        assert!(args.contains(&"--cap-add".to_string()));
        assert!(args.contains(&"--cap-drop".to_string()));
        assert!(args.contains(&"--security-opt".to_string()));
        assert!(args.contains(&"--device".to_string()));
        assert!(args.contains(&"--tmpfs".to_string()));
        assert!(args.contains(&"--expose".to_string()));
        assert!(args.contains(&"--env-file".to_string()));
        assert!(args.contains(&"--label".to_string()));
        assert!(args.contains(&"--label-file".to_string()));

        // Verify image is still at the end
        let image_pos = args.iter().position(|x| x == "nginx:latest").unwrap();
        assert!(image_pos > 0); // Should not be first
        assert!(image_pos < args.len() - 1 || args.len() == image_pos + 1); // Should be near end
    }

    #[test]
    fn test_run_command_empty_lists_not_added() {
        let cmd = RunCommand::new("alpine:latest");
        let args = cmd.build_command_args();

        // Ensure empty lists don't add any arguments
        assert!(!args.contains(&"--dns".to_string()));
        assert!(!args.contains(&"--dns-option".to_string()));
        assert!(!args.contains(&"--dns-search".to_string()));
        assert!(!args.contains(&"--add-host".to_string()));
        assert!(!args.contains(&"--cap-add".to_string()));
        assert!(!args.contains(&"--cap-drop".to_string()));
        assert!(!args.contains(&"--security-opt".to_string()));
        assert!(!args.contains(&"--device".to_string()));
        assert!(!args.contains(&"--tmpfs".to_string()));
        assert!(!args.contains(&"--expose".to_string()));
        assert!(!args.contains(&"--env-file".to_string()));
        assert!(!args.contains(&"--label".to_string()));
        assert!(!args.contains(&"--label-file".to_string()));

        // Additional list options should also not be present
        assert!(!args.contains(&"--network-alias".to_string()));
        assert!(!args.contains(&"--group-add".to_string()));
        assert!(!args.contains(&"--attach".to_string()));
        assert!(!args.contains(&"--log-opt".to_string()));
        assert!(!args.contains(&"--storage-opt".to_string()));
        assert!(!args.contains(&"--ulimit".to_string()));
        assert!(!args.contains(&"--volumes-from".to_string()));
        assert!(!args.contains(&"--link".to_string()));
        assert!(!args.contains(&"--link-local-ip".to_string()));

        // But image should still be there
        assert!(args.contains(&"alpine:latest".to_string()));
    }

    #[test]
    fn test_run_command_additional_list_options() {
        let cmd = RunCommand::new("alpine:latest")
            .network_alias("web")
            .network_alias("frontend")
            .group_add("staff")
            .group_add("docker")
            .attach("stdout")
            .attach("stderr")
            .log_opt("max-size=10m")
            .log_opt("max-file=3")
            .storage_opt("size=20G")
            .ulimit("nofile=1024:65536")
            .ulimit("nproc=1024")
            .volumes_from("data-container")
            .volumes_from("config-container:ro")
            .link("db:database")
            .link("cache:redis")
            .link_local_ip("169.254.1.1")
            .link_local_ip("fe80::1");

        let args = cmd.build_command_args();

        // Network aliases
        assert!(args.contains(&"--network-alias".to_string()));
        assert!(args.contains(&"web".to_string()));
        assert!(args.contains(&"frontend".to_string()));

        // Group additions
        assert!(args.contains(&"--group-add".to_string()));
        assert!(args.contains(&"staff".to_string()));
        assert!(args.contains(&"docker".to_string()));

        // Stream attachments
        assert!(args.contains(&"--attach".to_string()));
        assert!(args.contains(&"stdout".to_string()));
        assert!(args.contains(&"stderr".to_string()));

        // Log options
        assert!(args.contains(&"--log-opt".to_string()));
        assert!(args.contains(&"max-size=10m".to_string()));
        assert!(args.contains(&"max-file=3".to_string()));

        // Storage options
        assert!(args.contains(&"--storage-opt".to_string()));
        assert!(args.contains(&"size=20G".to_string()));

        // Ulimits
        assert!(args.contains(&"--ulimit".to_string()));
        assert!(args.contains(&"nofile=1024:65536".to_string()));
        assert!(args.contains(&"nproc=1024".to_string()));

        // Volumes from containers
        assert!(args.contains(&"--volumes-from".to_string()));
        assert!(args.contains(&"data-container".to_string()));
        assert!(args.contains(&"config-container:ro".to_string()));

        // Container links
        assert!(args.contains(&"--link".to_string()));
        assert!(args.contains(&"db:database".to_string()));
        assert!(args.contains(&"cache:redis".to_string()));

        // Link-local IPs
        assert!(args.contains(&"--link-local-ip".to_string()));
        assert!(args.contains(&"169.254.1.1".to_string()));
        assert!(args.contains(&"fe80::1".to_string()));
    }

    #[test]
    fn test_run_command_additional_list_individual_options() {
        // Test each additional list option individually for proper argument generation
        let network_cmd = RunCommand::new("alpine:latest").network_alias("api");
        let network_args = network_cmd.build_command_args();
        assert!(network_args.contains(&"--network-alias".to_string()));
        assert!(network_args.contains(&"api".to_string()));

        let group_cmd = RunCommand::new("alpine:latest").group_add("wheel");
        let group_args = group_cmd.build_command_args();
        assert!(group_args.contains(&"--group-add".to_string()));
        assert!(group_args.contains(&"wheel".to_string()));

        let attach_cmd = RunCommand::new("alpine:latest").attach("stdin");
        let attach_args = attach_cmd.build_command_args();
        assert!(attach_args.contains(&"--attach".to_string()));
        assert!(attach_args.contains(&"stdin".to_string()));

        let log_cmd = RunCommand::new("alpine:latest").log_opt("compress=true");
        let log_args = log_cmd.build_command_args();
        assert!(log_args.contains(&"--log-opt".to_string()));
        assert!(log_args.contains(&"compress=true".to_string()));

        let storage_cmd =
            RunCommand::new("alpine:latest").storage_opt("dm.thinpooldev=/dev/mapper/thin-pool");
        let storage_args = storage_cmd.build_command_args();
        assert!(storage_args.contains(&"--storage-opt".to_string()));
        assert!(storage_args.contains(&"dm.thinpooldev=/dev/mapper/thin-pool".to_string()));

        let ulimit_cmd = RunCommand::new("alpine:latest").ulimit("memlock=-1:-1");
        let ulimit_args = ulimit_cmd.build_command_args();
        assert!(ulimit_args.contains(&"--ulimit".to_string()));
        assert!(ulimit_args.contains(&"memlock=-1:-1".to_string()));

        let volumes_cmd = RunCommand::new("alpine:latest").volumes_from("shared-data");
        let volumes_args = volumes_cmd.build_command_args();
        assert!(volumes_args.contains(&"--volumes-from".to_string()));
        assert!(volumes_args.contains(&"shared-data".to_string()));

        let link_cmd = RunCommand::new("alpine:latest").link("mysql:db");
        let link_args = link_cmd.build_command_args();
        assert!(link_args.contains(&"--link".to_string()));
        assert!(link_args.contains(&"mysql:db".to_string()));

        let ip_cmd = RunCommand::new("alpine:latest").link_local_ip("169.254.100.1");
        let ip_args = ip_cmd.build_command_args();
        assert!(ip_args.contains(&"--link-local-ip".to_string()));
        assert!(ip_args.contains(&"169.254.100.1".to_string()));
    }

    #[test]
    fn test_run_command_health_check_options() {
        let cmd = RunCommand::new("nginx:latest")
            .health_cmd("curl -f http://localhost/ || exit 1")
            .health_interval("30s")
            .health_retries(3)
            .health_timeout("5s")
            .health_start_period("60s")
            .health_start_interval("5s");

        let args = cmd.build_command_args();

        // Health check options
        assert!(args.contains(&"--health-cmd".to_string()));
        assert!(args.contains(&"curl -f http://localhost/ || exit 1".to_string()));
        assert!(args.contains(&"--health-interval".to_string()));
        assert!(args.contains(&"30s".to_string()));
        assert!(args.contains(&"--health-retries".to_string()));
        assert!(args.contains(&"3".to_string()));
        assert!(args.contains(&"--health-timeout".to_string()));
        assert!(args.contains(&"5s".to_string()));
        assert!(args.contains(&"--health-start-period".to_string()));
        assert!(args.contains(&"60s".to_string()));
        assert!(args.contains(&"--health-start-interval".to_string()));
        // Note: "5s" appears twice, so we can't easily test for this specific one
    }

    #[test]
    fn test_run_command_advanced_mount_network_options() {
        let cmd = RunCommand::new("alpine:latest")
            .mount("type=bind,source=/host/path,target=/container/path")
            .mount("type=volume,source=data-vol,target=/data")
            .network("frontend")
            .network("backend")
            .gpus("all")
            .annotation("io.kubernetes.cri-o.Devices", "/dev/fuse")
            .annotation("io.kubernetes.cri-o.ShmSize", "64m")
            .sysctl("net.core.somaxconn", "1024")
            .sysctl("kernel.shm_rmid_forced", "1");

        let args = cmd.build_command_args();

        // Mount options
        assert!(args.contains(&"--mount".to_string()));
        assert!(args.contains(&"type=bind,source=/host/path,target=/container/path".to_string()));
        assert!(args.contains(&"type=volume,source=data-vol,target=/data".to_string()));

        // Network options
        assert!(args.contains(&"--network".to_string()));
        assert!(args.contains(&"frontend".to_string()));
        assert!(args.contains(&"backend".to_string()));

        // GPU options
        assert!(args.contains(&"--gpus".to_string()));
        assert!(args.contains(&"all".to_string()));

        // Annotation options
        assert!(args.contains(&"--annotation".to_string()));
        assert!(args.contains(&"io.kubernetes.cri-o.Devices=/dev/fuse".to_string()));
        assert!(args.contains(&"io.kubernetes.cri-o.ShmSize=64m".to_string()));

        // Sysctl options
        assert!(args.contains(&"--sysctl".to_string()));
        assert!(args.contains(&"net.core.somaxconn=1024".to_string()));
        assert!(args.contains(&"kernel.shm_rmid_forced=1".to_string()));
    }

    #[test]
    fn test_run_command_health_advanced_individual_options() {
        // Test each health check and advanced option individually
        let health_cmd = RunCommand::new("alpine:latest").health_cmd("ping -c 1 localhost");
        let health_args = health_cmd.build_command_args();
        assert!(health_args.contains(&"--health-cmd".to_string()));
        assert!(health_args.contains(&"ping -c 1 localhost".to_string()));

        let health_interval = RunCommand::new("alpine:latest").health_interval("10s");
        let interval_args = health_interval.build_command_args();
        assert!(interval_args.contains(&"--health-interval".to_string()));
        assert!(interval_args.contains(&"10s".to_string()));

        let health_retries = RunCommand::new("alpine:latest").health_retries(5);
        let retries_args = health_retries.build_command_args();
        assert!(retries_args.contains(&"--health-retries".to_string()));
        assert!(retries_args.contains(&"5".to_string()));

        let mount_cmd = RunCommand::new("alpine:latest").mount("type=tmpfs,destination=/app");
        let mount_args = mount_cmd.build_command_args();
        assert!(mount_args.contains(&"--mount".to_string()));
        assert!(mount_args.contains(&"type=tmpfs,destination=/app".to_string()));

        let network_cmd = RunCommand::new("alpine:latest").network("my-network");
        let network_args = network_cmd.build_command_args();
        assert!(network_args.contains(&"--network".to_string()));
        assert!(network_args.contains(&"my-network".to_string()));

        let gpu_cmd = RunCommand::new("alpine:latest").gpus("device=0");
        let gpu_args = gpu_cmd.build_command_args();
        assert!(gpu_args.contains(&"--gpus".to_string()));
        assert!(gpu_args.contains(&"device=0".to_string()));

        let annotation_cmd = RunCommand::new("alpine:latest").annotation("key", "value");
        let annotation_args = annotation_cmd.build_command_args();
        assert!(annotation_args.contains(&"--annotation".to_string()));
        assert!(annotation_args.contains(&"key=value".to_string()));

        let sysctl_cmd = RunCommand::new("alpine:latest").sysctl("net.ipv4.ip_forward", "1");
        let sysctl_args = sysctl_cmd.build_command_args();
        assert!(sysctl_args.contains(&"--sysctl".to_string()));
        assert!(sysctl_args.contains(&"net.ipv4.ip_forward=1".to_string()));
    }

    #[test]
    fn test_run_command_comprehensive_health_advanced_integration() {
        let cmd = RunCommand::new("web-app:latest")
            .name("production-web-app")
            // Health checks for production readiness
            .health_cmd("curl -f http://localhost:8080/health || exit 1")
            .health_interval("30s")
            .health_retries(3)
            .health_timeout("10s")
            .health_start_period("120s")
            // Advanced mounting and networking
            .mount("type=bind,source=/var/log/app,target=/app/logs")
            .mount("type=volume,source=app-data,target=/app/data")
            .network("frontend")
            .network("backend")
            // GPU support for ML workloads
            .gpus("device=0,1")
            // Kubernetes annotations
            .annotation(
                "io.kubernetes.container.apparmor.security.beta.kubernetes.io/app",
                "runtime/default",
            )
            .annotation(
                "io.kubernetes.container.seccomp.security.alpha.kubernetes.io/app",
                "runtime/default",
            )
            // System tuning
            .sysctl("net.core.somaxconn", "65535")
            .sysctl("net.ipv4.tcp_keepalive_time", "600")
            // Additional standard options
            .port(8080, 8080)
            .env("NODE_ENV", "production")
            .memory("2g")
            .cpus("2.0")
            .restart("unless-stopped")
            .detach();

        let args = cmd.build_command_args();

        // Verify all health check and advanced options are present
        assert!(args.contains(&"--health-cmd".to_string()));
        assert!(args.contains(&"--health-interval".to_string()));
        assert!(args.contains(&"--health-retries".to_string()));
        assert!(args.contains(&"--health-timeout".to_string()));
        assert!(args.contains(&"--health-start-period".to_string()));
        assert!(args.contains(&"--mount".to_string()));
        assert!(args.contains(&"--network".to_string()));
        assert!(args.contains(&"--gpus".to_string()));
        assert!(args.contains(&"--annotation".to_string()));
        assert!(args.contains(&"--sysctl".to_string()));

        // Verify image is still at the end
        let image_pos = args.iter().position(|x| x == "web-app:latest").unwrap();
        assert!(image_pos > 0);
    }

    #[test]
    fn test_run_command_block_io_controls() {
        let cmd = RunCommand::new("alpine:latest")
            .blkio_weight(500)
            .blkio_weight_device("/dev/sda:300")
            .blkio_weight_device("/dev/sdb:700")
            .device_read_bps("/dev/sda:50mb")
            .device_write_bps("/dev/sda:30mb")
            .device_read_iops("/dev/sda:1000")
            .device_write_iops("/dev/sda:800");

        let args = cmd.build_command_args();

        // Block I/O weight
        assert!(args.contains(&"--blkio-weight".to_string()));
        assert!(args.contains(&"500".to_string()));

        // Block I/O weight per device
        assert!(args.contains(&"--blkio-weight-device".to_string()));
        assert!(args.contains(&"/dev/sda:300".to_string()));
        assert!(args.contains(&"/dev/sdb:700".to_string()));

        // Device read/write BPS
        assert!(args.contains(&"--device-read-bps".to_string()));
        assert!(args.contains(&"/dev/sda:50mb".to_string()));
        assert!(args.contains(&"--device-write-bps".to_string()));
        assert!(args.contains(&"/dev/sda:30mb".to_string()));

        // Device read/write IOPS
        assert!(args.contains(&"--device-read-iops".to_string()));
        assert!(args.contains(&"/dev/sda:1000".to_string()));
        assert!(args.contains(&"--device-write-iops".to_string()));
        assert!(args.contains(&"/dev/sda:800".to_string()));
    }

    #[test]
    fn test_run_command_realtime_cpu_networking() {
        let cmd = RunCommand::new("alpine:latest")
            .cpu_rt_period(1_000_000)
            .cpu_rt_runtime(950_000)
            .ip("172.30.100.104")
            .ip6("2001:db8::33")
            .device_cgroup_rule("c 1:3 mr")
            .device_cgroup_rule("a 7:* rmw");

        let args = cmd.build_command_args();

        // Real-time CPU scheduling
        assert!(args.contains(&"--cpu-rt-period".to_string()));
        assert!(args.contains(&"1000000".to_string()));
        assert!(args.contains(&"--cpu-rt-runtime".to_string()));
        assert!(args.contains(&"950000".to_string()));

        // Advanced networking
        assert!(args.contains(&"--ip".to_string()));
        assert!(args.contains(&"172.30.100.104".to_string()));
        assert!(args.contains(&"--ip6".to_string()));
        assert!(args.contains(&"2001:db8::33".to_string()));

        // Device cgroup rules
        assert!(args.contains(&"--device-cgroup-rule".to_string()));
        assert!(args.contains(&"c 1:3 mr".to_string()));
        assert!(args.contains(&"a 7:* rmw".to_string()));
    }

    #[test]
    fn test_run_command_advanced_system_individual_options() {
        // Test each advanced system option individually
        let blkio_cmd = RunCommand::new("alpine:latest").blkio_weight(100);
        let blkio_args = blkio_cmd.build_command_args();
        assert!(blkio_args.contains(&"--blkio-weight".to_string()));
        assert!(blkio_args.contains(&"100".to_string()));

        let weight_device_cmd =
            RunCommand::new("alpine:latest").blkio_weight_device("/dev/sda:500");
        let weight_device_args = weight_device_cmd.build_command_args();
        assert!(weight_device_args.contains(&"--blkio-weight-device".to_string()));
        assert!(weight_device_args.contains(&"/dev/sda:500".to_string()));

        let read_bps_cmd = RunCommand::new("alpine:latest").device_read_bps("/dev/sda:1mb");
        let read_bps_args = read_bps_cmd.build_command_args();
        assert!(read_bps_args.contains(&"--device-read-bps".to_string()));
        assert!(read_bps_args.contains(&"/dev/sda:1mb".to_string()));

        let write_bps_cmd = RunCommand::new("alpine:latest").device_write_bps("/dev/sda:1mb");
        let write_bps_args = write_bps_cmd.build_command_args();
        assert!(write_bps_args.contains(&"--device-write-bps".to_string()));
        assert!(write_bps_args.contains(&"/dev/sda:1mb".to_string()));

        let read_iops_cmd = RunCommand::new("alpine:latest").device_read_iops("/dev/sda:100");
        let read_iops_args = read_iops_cmd.build_command_args();
        assert!(read_iops_args.contains(&"--device-read-iops".to_string()));
        assert!(read_iops_args.contains(&"/dev/sda:100".to_string()));

        let write_iops_cmd = RunCommand::new("alpine:latest").device_write_iops("/dev/sda:100");
        let write_iops_args = write_iops_cmd.build_command_args();
        assert!(write_iops_args.contains(&"--device-write-iops".to_string()));
        assert!(write_iops_args.contains(&"/dev/sda:100".to_string()));

        let rt_period_cmd = RunCommand::new("alpine:latest").cpu_rt_period(100_000);
        let rt_period_args = rt_period_cmd.build_command_args();
        assert!(rt_period_args.contains(&"--cpu-rt-period".to_string()));
        assert!(rt_period_args.contains(&"100000".to_string()));

        let rt_runtime_cmd = RunCommand::new("alpine:latest").cpu_rt_runtime(95_000);
        let rt_runtime_args = rt_runtime_cmd.build_command_args();
        assert!(rt_runtime_args.contains(&"--cpu-rt-runtime".to_string()));
        assert!(rt_runtime_args.contains(&"95000".to_string()));

        let ip_cmd = RunCommand::new("alpine:latest").ip("192.168.1.100");
        let ip_args = ip_cmd.build_command_args();
        assert!(ip_args.contains(&"--ip".to_string()));
        assert!(ip_args.contains(&"192.168.1.100".to_string()));

        let ipv6_cmd = RunCommand::new("alpine:latest").ip6("fe80::1");
        let ipv6_args = ipv6_cmd.build_command_args();
        assert!(ipv6_args.contains(&"--ip6".to_string()));
        assert!(ipv6_args.contains(&"fe80::1".to_string()));

        let cgroup_rule_cmd = RunCommand::new("alpine:latest").device_cgroup_rule("c 1:1 rwm");
        let cgroup_rule_args = cgroup_rule_cmd.build_command_args();
        assert!(cgroup_rule_args.contains(&"--device-cgroup-rule".to_string()));
        assert!(cgroup_rule_args.contains(&"c 1:1 rwm".to_string()));
    }

    #[test]
    #[allow(clippy::too_many_lines)]
    fn test_run_command_complete_100_percent_coverage() {
        use std::path::PathBuf;

        // Test demonstrating ALL 96 Docker run options are now implemented
        let cmd = RunCommand::new("enterprise-app:latest")
            .name("production-enterprise")
            // Basic options
            .detach()
            .interactive()
            .tty()
            .remove()
            // Environment and ports
            .env("NODE_ENV", "production")
            .port(8080, 8080)
            .volume("/data", "/app/data")
            .workdir("/app")
            .entrypoint("/app/start.sh")
            // Resource limits (Phase 2)
            .memory("4g")
            .cpus("2.0")
            .cpu_shares(1024)
            .cpu_period(100_000)
            .cpu_quota(50000)
            .cpuset_cpus("0-1")
            .cpuset_mems("0")
            .memory_swap("8g")
            .memory_reservation("2g")
            // Security & User Context (Phase 2)
            .user("app:app")
            .privileged()
            .hostname("enterprise-app")
            // Lifecycle Management (Phase 2)
            .restart("unless-stopped")
            // System Integration (Phase 2)
            .platform("linux/amd64")
            .runtime("runc")
            .isolation("default")
            .pull("always")
            .cidfile("/tmp/container.cid")
            .domainname("enterprise.local")
            .mac_address("02:42:ac:11:00:02")
            // Logging & Drivers (Phase 2)
            .log_driver("json-file")
            .volume_driver("local")
            // Namespaces (Phase 2)
            .userns("host")
            .uts("host")
            .pid("host")
            .ipc("host")
            .cgroupns("host")
            .cgroup_parent("/docker")
            // Advanced Memory & Performance (Phase 2)
            .kernel_memory("1g")
            .memory_swappiness(10)
            .oom_score_adj(-500)
            .pids_limit(1000)
            .shm_size("64m")
            // Process Control (Phase 2)
            .stop_signal("SIGTERM")
            .stop_timeout(30)
            .detach_keys("ctrl-p,ctrl-q")
            // Simple Flags (Phase 2)
            .no_sig_proxy()
            .read_only()
            .init()
            .oom_kill_disable()
            .no_healthcheck()
            .enable_content_trust()
            .publish_all()
            .quiet()
            // High-Impact List Options (Phase 3)
            .dns("8.8.8.8")
            .dns_option("ndots:2")
            .dns_search("enterprise.local")
            .add_host("api.enterprise.local:10.0.1.100")
            .cap_add("NET_ADMIN")
            .cap_drop("ALL")
            .security_opt("no-new-privileges:true")
            .device("/dev/null")
            .tmpfs("/tmp:size=100m")
            .expose("9090")
            .env_file(PathBuf::from(".env.production"))
            .label("app=enterprise")
            .label_file(PathBuf::from("labels.txt"))
            // Batch 1: Additional List/Vec Options
            .network_alias("enterprise-primary")
            .group_add("staff")
            .attach("stdout")
            .log_opt("max-size=10m")
            .storage_opt("size=100G")
            .ulimit("nofile=65536:65536")
            .volumes_from("data-container")
            .link("db:database")
            .link_local_ip("169.254.1.1")
            // Batch 2: Medium Complexity Options
            .health_cmd("curl -f http://localhost:8080/health || exit 1")
            .health_interval("30s")
            .health_retries(3)
            .health_timeout("10s")
            .health_start_period("60s")
            .health_start_interval("5s")
            .mount("type=bind,source=/host/config,target=/app/config")
            .network("enterprise-net")
            .gpus("device=0")
            .annotation("io.kubernetes.cri-o.TTY", "true")
            .sysctl("net.core.somaxconn", "65535")
            // Batch 3: Complex/Advanced Options
            .blkio_weight(500)
            .blkio_weight_device("/dev/sda:300")
            .device_read_bps("/dev/sda:100mb")
            .device_write_bps("/dev/sda:50mb")
            .device_read_iops("/dev/sda:1000")
            .device_write_iops("/dev/sda:500")
            .cpu_rt_period(1_000_000)
            .cpu_rt_runtime(950_000)
            .ip("10.0.1.50")
            .ip6("2001:db8::50")
            .device_cgroup_rule("c 1:1 rwm");

        let args = cmd.build_command_args();

        // Verify we have a substantial command with all option types
        assert!(args.len() > 150); // Should be a very long command

        // Verify key options from each batch are present
        assert!(args.contains(&"--detach".to_string()));
        assert!(args.contains(&"--memory".to_string()));
        assert!(args.contains(&"--dns".to_string()));
        assert!(args.contains(&"--network-alias".to_string()));
        assert!(args.contains(&"--health-cmd".to_string()));
        assert!(args.contains(&"--blkio-weight".to_string()));

        // Verify image is still at the end
        let image_pos = args
            .iter()
            .position(|x| x == "enterprise-app:latest")
            .unwrap();
        assert!(image_pos > 100); // Should be very far into the command
        assert_eq!(args[args.len() - 1], "enterprise-app:latest");

        println!("COMPLETE! All 96 Docker run options implemented and tested!");
    }
}